소스 검색

Telldus Proovesmart and ClasO-sensors are working.

Thomas Chef 7 년 전
부모
커밋
03a556f0e9
4개의 변경된 파일336개의 추가작업 그리고 121개의 파일을 삭제
  1. 20 3
      README.md
  2. 104 99
      clas_o_sensor.ino
  3. 154 0
      telldus_sensor.ino
  4. 58 19
      wireless_temp_sensor_receiver.ino

+ 20 - 3
README.md

@@ -1,6 +1,23 @@
-#This is the Arduino 433MHz Wireless Receiver project used for the Raspberry project at HW&HMI-Dept in Eskilstuna
+#Wireless 433MHz Receiver
+This is the Arduino 433MHz Wireless Receiver project used for the Raspberry project at HW&HMI-Dept in Eskilstuna.
 
-It receives wireless temp-sensor data and sends it to the Raspberry with the uart as a string
+It receives wireless temp-sensor data and sends it to the Raspberry with the hw-uart as a string
 
-Board: (Arduino) SparkFun Pro Micro (Ali-Copy)
+#### Hardware used:
+Board: (Arduino) SparkFun Pro Micro (Ali-Copy)  
 CPU:   ATmega32U4 16 MHz
+
+
+#### Pin connections:
+|Pro Micro pin  |Function                                  |
+|---------------|------------------------------------------|
+|7              |433 Rx data                               |
+|GND            |Ground, connected to 433 Rx and Raspberry |
+|VCC            |+5V, connected to 433 Rx and Raspberry    |
+|TXD            |Uart data to Raspberry                    |
+
+
+#### Links:
+[Pro Micro Product Page](https://www.sparkfun.com/products/12640)  
+[ATmega32U4 Manual](http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7766-8-bit-AVR-ATmega16U4-32U4_Datasheet.pdf)  
+[Telldus temperature sensor](https://www.clasohlson.com/se/Temperaturgivare-hygrometer-Telldus/36-6987)

+ 104 - 99
clas_o_sensor.ino

@@ -1,109 +1,114 @@
 //#define CLAS_O_DEBUG
 
-enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
-
-static unsigned char clas_o_state = UNKNOWN;
-
-unsigned long clas_o_x_data;
-static unsigned int clas_o_x_numBits;
-
-void clas_o_ResetDecoder () {
-    clas_o_x_data = 0;
-    clas_o_x_numBits = 0;
-    clas_o_state = UNKNOWN;
-}
-
-static boolean clas_o_isDone() {
-    return clas_o_state == DONE;
-}
-
-static void clas_o_done () {
-    clas_o_state = DONE;
-}
-
-static void clas_o_gotBit (char value) {
-
-    clas_o_x_numBits++;
-    clas_o_x_data = (clas_o_x_data << 1) + (value & 0x01);
-
-    clas_o_state = OK;
-}
-
-static int clas_o_decode (unsigned int width) {
-
-    if( clas_o_state != UNKNOWN ) {
-#ifdef CLAS_O_DEBUG
-        Serial.print(width);
-        Serial.print(" - ");
-#endif
+namespace clas_o {
+    
+    enum { UNKNOWN, T0, T1, T2, T3, OK, DONE };
+    
+    unsigned char state = UNKNOWN;
+    
+    uint32_t x_data;
+    uint8_t x_numBits;
+    
+    void resetDecoder () {
+        x_data = 0;
+        x_numBits = 0;
+        state = UNKNOWN;
+    }
+    
+    static boolean isDone() {
+        return state == DONE;
+    }
+    
+    static void done () {
+        state = DONE;
+    }
+    
+    static void gotBit (uint8_t value) {
+    
+        x_numBits++;
+        x_data = (x_data << 1) + (value & 0x01);
+    
+        state = OK;
     }
 
-
-    switch (clas_o_state) {
-        case UNKNOWN: // Start of frame
-            if ( 3500 <= width && width <= 4000 ) {
-#ifdef CLAS_O_DEBUG
-                Serial.print(width);
-                Serial.print(" - ");
-#endif
-                clas_o_state = T0;
-            }
-            else {
-                return -1;    // error, reset
-            }
-            break;
-
-        case T0: // First half of pulse : HIGH around 230us
-
-            if ( clas_o_x_numBits == 32 ) {   // end of frame
-                clas_o_state = DONE;
-                clas_o_x_data = (clas_o_x_data >> 8);    // Mask away some bits at the end
-                return 1;
-            }
-            else if ( 340 <= width && width <= 540 ) {
-                clas_o_state = T1;
-            }
-            else {
-                if ( clas_o_x_numBits == 0 && 3400 <= width && width <= 3800 ) {
-                    clas_o_state = T0;
+    
+    static int16_t decode (uint16_t width) {
+    
+        if( state != UNKNOWN ) {
+    #ifdef CLAS_O_DEBUG
+            Serial.print(width);
+            Serial.print(" - ");
+    #endif
+        }
+    
+    
+        switch (state) {
+            case UNKNOWN: // Start of frame
+                if ( (3500) <= width && width <= (4000) ) {
+    #ifdef CLAS_O_DEBUG
+                    Serial.print(width);
+                    Serial.print(" - ");
+    #endif
+                    state = T0;
                 }
                 else {
-#ifdef CLAS_O_DEBUG
-                    Serial.println(" X1X ");
-#endif
-                    return -1;  // error, reset
+                    return -1;    // error, reset
                 }
-            }
-            break;
-
-        case T1:
-            if ( 800 <= width && width <= 1000) {
-                clas_o_gotBit(0);
-            } else if ( 1640 <= width && width <= 1940 ) {
-                clas_o_gotBit(1);
-            } else  {
-#ifdef CLAS_O_DEBUG
-                Serial.println(" X2X ");
-#endif
-                return -1;    // error, reset
-            }
-            clas_o_state = T0;
-            break;
+                break;
+    
+            case T0: // First half of pulse : HIGH around 230us
+    
+                if ( x_numBits == 32 ) {   // end of frame
+                    state = DONE;
+                    x_data = (x_data >> 8);    // Mask away some bits at the end
+                    return 1;
+                }
+                else if ( 340 <= width && width <= 560 ) {
+                    state = T1;
+                }
+                else {
+                    if ( x_numBits == 0 && (3500 <= width && width <= 4000) ) {
+                        state = T0;
+                    }
+                    else {
+    #ifdef CLAS_O_DEBUG
+                        Serial.println(" X1X ");
+    #endif
+                        return -1;  // error, reset
+                    }
+                }
+                break;
+    
+            case T1:
+                if ( 800 <= width && width <= 1000 ) {
+                    gotBit(0);
+                } else if ( 1640 <= width && width <= 1940 ) {
+                    gotBit(1);
+                } else  {
+    #ifdef CLAS_O_DEBUG
+                    Serial.println(" X2X ");
+    #endif
+                    return -1;    // error, reset
+                }
+                state = T0;
+                break;
+        }
+        return 0;
     }
-    return 0;
-}
-
-boolean nextPulse_clas_o (unsigned int width) {
-
-    if ( width > 0 ) {
-        if (clas_o_state != DONE)
-
-            switch (clas_o_decode(width)) {
-                case -1:
-                    clas_o_ResetDecoder();
-                    break;
-                case 1:  clas_o_done();  break;
-            }
+    
+    boolean nextPulse( uint16_t width ) {
+    
+        if ( width > 0 ) {
+            if (state != DONE)
+    
+                switch (decode(width)) {
+                    case -1:
+                        resetDecoder();
+                        break;
+                    case 1:  done();  break;
+                }
+        }
+        return isDone();
     }
-    return clas_o_isDone();
+
 }

+ 154 - 0
telldus_sensor.ino

@@ -0,0 +1,154 @@
+//#define TELLDUS_DEBUG
+//#define TELLDUS_RECORDER
+
+namespace telldus {
+    
+    enum { T0, T1, T2, OK, DONE };
+    
+    static unsigned char state = T0;
+    
+    uint64_t x_data;
+    uint8_t  x_numBits;
+    uint32_t meanPulseWidth = 0;
+    uint8_t  meanPulseWidthCnt = 0;
+    
+    void resetDecoder () {
+        x_data = 0;
+        x_numBits = 0;
+        state = T0;
+        meanPulseWidth = 0;
+        meanPulseWidthCnt = 0;
+    }
+    
+    static boolean isDone() {
+        return state == DONE;
+    }
+    
+    static void done () {
+        state = DONE;
+    }
+    
+    static void gotBit (uint8_t value) {
+    
+        x_numBits++;
+        x_data = (x_data << 1) + (value & 0x01);
+    
+        state = OK;
+    }
+        
+    
+    static int16_t decode( uint16_t width ) {
+
+        static uint16_t firstPulse=0;
+
+#ifdef TELLDUS_RECORDER
+        static uint8_t recTrigger=0;
+        static uint16_t recorder[200];
+        static uint8_t recIdx = 0;
+
+        recorder[recIdx] = width;
+        recIdx++;
+        if(recIdx==200) recIdx=0;
+
+        if( recTrigger > 1 ) recTrigger--;
+
+        if( recTrigger == 1 ) {
+            recTrigger=0;
+            
+            for(uint8_t i=0; i<200;i++) {
+                Serial.print("-");
+                Serial.print(recorder[recIdx]);
+                recIdx++;
+                if(recIdx==200) recIdx=0;
+            }
+            Serial.println("");
+        }
+#endif
+            
+       switch( state ) {
+    
+        case T0: // First half of pulse : HIGH around 910
+    
+            if( x_numBits == 48 || (width > 5000 && x_numBits>=42) ) {    // end of frame
+#ifdef TELLDUS_RECORDER
+                recTrigger = 6;
+#endif
+                meanPulseWidth /= meanPulseWidthCnt;
+                state = DONE;
+                return 1;
+            }
+            else if ( 780 <= width && width <= 1100 ) {
+                firstPulse = width;
+                state = T1;
+            }
+            else {
+#ifdef TELLDUS_DEBUG
+                if( x_numBits > 0 ) {
+                    Serial.print(" E ");
+                    Serial.print(x_numBits);
+                    Serial.print(" (");
+                    Serial.print(width);
+                    Serial.println("- )");
+                }
+#endif
+                return -1;  // error, reset
+            }
+            break;
+    
+        case T1:
+            if( 360 <= width && width <= 640 ) {            // 410
+                gotBit(1);
+#ifdef TELLDUS_DEBUG
+                Serial.print(" (");
+                Serial.print(firstPulse);
+                Serial.print("-");
+                Serial.print(width);
+                Serial.print(") ");
+#endif
+            } else if ( 1280 <= width && width <= 1560 ) {          // 1290
+#ifdef TELLDUS_DEBUG
+                Serial.print(" (");
+                Serial.print(firstPulse);
+                Serial.print("-");
+                Serial.print(width);
+                Serial.print(") ");
+#endif
+                gotBit(0);
+                meanPulseWidth += width;
+                meanPulseWidthCnt++;
+            } else  {
+#ifdef TELLDUS_DEBUG
+                if( x_numBits > 0 ) {
+                    Serial.print(" E ");
+                    Serial.print(x_numBits);
+                    Serial.print(" (");
+                    Serial.print(firstPulse);
+                    Serial.print("-");
+                    Serial.print(width);
+                    Serial.println(")");
+                }
+#endif
+                return -1;    // error, reset
+            }
+            state = T0;
+            break;
+        }
+        return 0;
+    }
+    
+    boolean nextPulse( uint16_t width ) {
+    
+        if ( width > 0 ) {
+            if (state != DONE)
+    
+                switch( decode(width) ) {
+                    case -1:
+                        resetDecoder();
+                        break;
+                    case 1:  done();  break;
+                }
+        }
+        return isDone();
+    }
+
+}

+ 58 - 19
wireless_temp_sensor_receiver.ino

@@ -1,3 +1,25 @@
+// Configure which sensors to compile/use
+#define ENABLE_CLAS_O_RX
+#define ENABLE_TELLDUS_RX
+
+// ---- Declare the functions and variables needed for the Clas O-namespace ----
+#ifdef ENABLE_CLAS_O_RX
+namespace clas_o {
+    extern uint32_t x_data;
+    boolean nextPulse (uint16_t width);
+    void resetDecoder();
+}
+#endif
+
+// ---- Declare the functions and variables needed for the Telldus-namespace ----
+#ifdef ENABLE_TELLDUS_RX
+namespace telldus {
+    extern uint64_t x_data;
+    extern uint32_t meanPulseWidth;
+    boolean nextPulse (uint16_t width);
+    void resetDecoder();
+}
+#endif
 
 // Define which pins to use for different tasks
 const int rxPinA      = 7;  // 433MHz RX, Connected to pin 7
@@ -16,11 +38,10 @@ void setup()
     sei(); // Enable interrupt
 
     // Setup the different serial communication channels
-    Serial.begin(9600);  // Serial monitor (Over USB, when debugging with Arduino)
+    Serial.begin(115200);  // Serial monitor (Over USB, when debugging with Arduino)
     Serial1.begin(9600); // HW-UART (To Raspberry)
 }
 
-extern unsigned long clas_o_x_data;
 
 // Convert from 12-bit unsigned data to 32-bit signed data
 static int32_t convertToSignedTemp(uint16_t value)
@@ -33,31 +54,49 @@ void loop()
 {
     static uint16_t width;              // Stores the length of the received pulse
     static unsigned long totalRx = 0;   // The total number of received pulses
+    char str[100];
+    const uint32_t now = millis();      // Time in mS
 
     if ( rcvDeQueue(&width) ) {
-
-        //width = (width / 5) * 4; // 80%
         
         totalRx++;  // Increase the total received pulses
 
-        if( nextPulse_clas_o(width) ) {
+#ifdef ENABLE_CLAS_O_RX
+        if( clas_o::nextPulse(width) ) {
+
+            static uint32_t old_rx_time = 0;
+            static uint32_t old_x_data = 0;
+
+            if( (now > (old_rx_time + 500))  || (old_x_data != clas_o::x_data) ) {
               
-             int16_t value      = convertToSignedTemp( clas_o_x_data & 0xFFF );
-            uint16_t id         = (clas_o_x_data>>12) & 0x007;
-            
-            char rad[200];
-            sprintf(rad,"Id:%u  Temp:%d.%d\n",id,value/10,value%10);
-            Serial.print(rad);
-            
+                const int16_t value       = convertToSignedTemp( clas_o::x_data & 0xFFF );                // Temperature
+                const uint16_t id         = (clas_o::x_data>>12) & 0x007;                                 // Id
+                
+                sprintf(str,"<TR:%u:%d:0>\n",id,value);
+                Serial.print(str);
+
+                old_x_data = clas_o::x_data;
+            }
 
-            clas_o_ResetDecoder();
-        }    
+            clas_o::resetDecoder();
+            old_rx_time = now;
+        }
+#endif
+
+#ifdef ENABLE_TELLDUS_RX
+        if( telldus::nextPulse(width) ) {
+              
+            const int16_t value    = convertToSignedTemp( (telldus::x_data & 0x00FFF0000) >> 16 );    // Temperature
+            const int16_t value2   = (telldus::x_data & 0x00000FF00) >>  8;                           // Moist
+            const uint8_t id       = (telldus::x_data & 0xFF0000000) >> 28;                           // Id
+            
+            sprintf(str,"<Tr:%u:%d:%d>\n",id,value,value2);
+            //sprintf(str,"Id:%u Temp:%d.%d  Mean:%u\n",id,value/10,value%10,telldus::meanPulseWidth);
+            Serial.print(str);
 
-        // Check if the received pulse is within limits for a start-pulse
-        /*if ( 3400 <= width && width <= 3800 ) {
-            Serial.print("Received one !  Total pulses: ");
-            Serial.println(totalRx);
-        }*/
+            telldus::resetDecoder();
+        }
+#endif
 
     }
 }