- Merged with upstream version
[rtl-433.git] / src / devices / wh2.c~
diff --git a/src/devices/wh2.c~ b/src/devices/wh2.c~
new file mode 100644 (file)
index 0000000..d4c6163
--- /dev/null
@@ -0,0 +1,73 @@
+#include "rtl_433.h"
+
+// ** WH2 Temperature and Humidity Outdoor Sensor **
+
+uint8_t crc8( uint8_t *addr, uint8_t len)
+{
+  uint8_t crc = 0;
+  
+  // Indicated changes are from reference CRC-8 function in OneWire library
+  while (len--) {
+    uint8_t inbyte = *addr++;
+    int i;
+    for (i = 8; i; i--) {
+      uint8_t mix = (crc ^ inbyte) & 0x80; // changed from & 0x01
+      crc <<= 1; // changed from right shift
+      if (mix) crc ^= 0x31;// changed from 0x8C;
+        inbyte <<= 1; // changed from right shift
+    }
+  }
+  return crc;
+}
+                                                      
+static int wh2_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS],int16_t bits_per_row[BITBUF_ROWS]) {
+    int i,j,k;
+
+    uint8_t payload[4];
+    int received_crc8,payload_crc8;
+
+    int wh2_id;
+    float wh2_temp;
+    float wh2_humidity;
+
+    if (bb[0][0] != 0xFE) return 0;
+
+    payload[0] = bb[0][1]>>1;
+    payload[1] = bb[0][2]>>1 | ((bb[0][1]&1) << 7 );
+    payload[2] = bb[0][3]>>1 | ((bb[0][2]&1) << 7 );
+    payload[3] = bb[0][4]>>1 | ((bb[0][3]&1) << 7 );
+    
+    received_crc8 = (bb[0][5]>>1) | ((bb[0][4]&1) << 7 );
+
+    payload_crc8 = crc8(payload,4);
+    
+    if (payload_crc8 != received_crc8) {
+        fprintf(stderr,"Bad WH2 payload CRC, skipping...\n");
+        return 0;
+    }
+    
+    wh2_id = (payload[0] << 4) + (payload[1] >> 4);
+    wh2_temp = ((payload[1] & 0x7) << 8) + payload[2];
+    if (payload[1] & 0x8) {
+        wh2_temp = -wh2_temp;
+    }
+    wh2_temp = wh2_temp/10;
+    
+    wh2_humidity = payload[3];
+    
+    fprintf(stdout, "SENSOR:TYPE=WH2,ID=%X,HUMIDITY=%g,TEMPERATURE=%g\n",wh2_id,wh2_humidity,wh2_temp);
+    fprintf(stderr, "SENSOR:TYPE=WH2,ID=%X,HUMIDITY=%g,TEMPERATURE=%g\n",wh2_id,wh2_humidity,wh2_temp);
+
+    return 1;
+        
+}
+
+r_device wh2 = {
+    /* .id             = */ 19,
+    /* .name           = */ "WH2 Weather Station",
+    /* .modulation     = */ OOK_PWM_P,
+    /* .short_limit    = */ 150,
+    /* .long_limit     = */ 400, 
+    /* .reset_limit    = */ 20000,
+    /* .json_callback  = */ &wh2_callback,
+};