Browse Source

Added Oregon Rain Sensor Rx

Thomas Chef 5 years ago
parent
commit
bc5970f2c4
5 changed files with 136 additions and 1 deletions
  1. 1 1
      main/CMakeLists.txt
  2. 107 0
      main/Sensors/oregon.c
  3. 11 0
      main/Sensors/oregon.h
  4. 1 0
      main/main.c
  5. 16 0
      main/receiver.c

+ 1 - 1
main/CMakeLists.txt

@@ -1,5 +1,5 @@
 idf_component_register(SRCS "main.c" "led.c" "rxTimer.c" "transceiver.c" "receiver.c"
-                        "Sensors/ClasOSensor.c" "Sensors/proovesmartSensor.c" "Sensors/nexa.c"
+                        "Sensors/ClasOSensor.c" "Sensors/proovesmartSensor.c" "Sensors/nexa.c" "Sensors/oregon.c"
                         "nexaTransmit.c"
                         "wifi.c" "udp_client.c"
                     INCLUDE_DIRS ".")

+ 107 - 0
main/Sensors/oregon.c

@@ -0,0 +1,107 @@
+#include "oregon.h"
+#include <stdint.h>
+#include <stdio.h>
+#include "../rxTimer.h"
+#include "../led.h"
+#include <string.h>
+#include "esp_log.h"
+
+static void done ();
+
+static unsigned char total_bits, bits, flip, state, pos, data[25];
+char result_str[25];
+
+enum { UNKNOWN, T0, T1, T2, T3, SENSOR_OK, DONE };
+
+void Oregon_ResetDecoder () {
+    total_bits = bits = pos = flip = 0;
+    state = UNKNOWN;
+    memset(result_str,'\0',sizeof(result_str));
+}
+
+
+// V3-Version of the gitBit-function
+static void gotBit (char value) {
+    data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
+    total_bits++;
+    pos = total_bits >> 3;
+    if (pos >= sizeof data) {
+        Oregon_ResetDecoder();
+        return;
+    }
+    state = SENSOR_OK;
+}
+
+// store a bit using Manchester encoding
+static void manchester (char value) {
+    flip ^= value; // manchester code, long pulse flips the bit
+    gotBit(flip);
+}
+
+static void done () {
+    while (bits)
+        gotBit(0); // padding
+    state = DONE;
+}
+
+static int rx_decode( int width ) {
+    if( (480-200) < width && width < (980+200) ) {
+        char w = width >= 730;
+        switch (state) {
+            case UNKNOWN:
+                if (w == 0)
+                    ++flip;
+                else if (32 <= flip) {
+                    flip = 1;
+                    manchester(1);  // Width 950
+                } else
+                    return -1;
+                break;
+            case SENSOR_OK:
+                if (w == 0)
+                    state = T0;
+                else 
+                    manchester(1);  // Width 980
+                break;
+            case T0:
+                if (w == 0)
+                    manchester(0);  // Width 480
+                else
+                    return -1;
+                break;
+        }
+    } else {
+        return -1;
+    }
+    return  total_bits == 80 ? 1: 0;
+}
+
+ //10 bytes data: 2A 19 04 CE 00 00 60 12 00 30
+
+char * nextPulseOregonSensor(uint32_t width) {
+    if( state != DONE ) {
+    
+        switch (rx_decode(width)) {
+            case -1:
+                Oregon_ResetDecoder();
+                break;
+            case 1:
+                done();
+                break;
+        }
+    }
+    if( state == DONE ) {
+        if( pos == 10 ) {
+            for( int i=0; i<pos; i++ ) {
+                sprintf(result_str+(i*2),"%02X",data[i] );
+            }
+            return result_str;
+        }
+        else {
+            Oregon_ResetDecoder();
+            return  (char *)NULL;
+        }
+    }
+    else 
+        return (char *)NULL;
+}

+ 11 - 0
main/Sensors/oregon.h

@@ -0,0 +1,11 @@
+#ifndef __OREGON__
+#define __OREGON__
+
+#include <stdbool.h>
+#include <stdint.h>
+
+void Oregon_ResetDecoder();
+
+char * nextPulseOregonSensor(uint32_t width);
+
+#endif

+ 1 - 0
main/main.c

@@ -34,6 +34,7 @@ void app_main(void)
 #ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
     udpClientInit();
+    //while(1) vTaskDelay(2000 / portTICK_RATE_MS);
 #endif
     initTransceiver();      // Init the 433-transceiver-HW
     initLed();              // Init LED-Blink

+ 16 - 0
main/receiver.c

@@ -21,6 +21,7 @@
 #include "Sensors/ClasOSensor.h"
 #include "Sensors/proovesmartSensor.h"
 #include "Sensors/nexa.h"
+#include "Sensors/oregon.h"
 #include "led.h"
 #include "udp_client.h"
 #include "wifi.h"
@@ -91,12 +92,27 @@ void receiverTask(void *pvParameter)
                 sendUDPMessage(dataStr);
 #endif
             }
+
+            // Receive the Oregon (Rain) Sensor
+            char *ch_data_p = nextPulseOregonSensor(width);
+            if( ch_data_p != NULL ) {
+
+                char ch_data[21];
+                strncpy(ch_data,ch_data_p, sizeof(ch_data));
+                ESP_LOGI("RX", "OREGON: <RR%s>", ch_data );
+
+                Oregon_ResetDecoder();
+            }
         }
     }
     vTaskDelete(NULL);
 }
 
 void initReceiver() {
+
+    Oregon_ResetDecoder();
+    ClasO_ResetDecoder();
+    ProovesmartSensor_ResetDecoder();
             
     ESP_LOGI("RX", "Receiver has been initialized.");