3 // ** Acurite 5n1 functions **
 
   5 const float acurite_winddirections[] =
 
   6     { 337.5, 315.0, 292.5, 270.0, 247.5, 225.0, 202.5, 180,
 
   7       157.5, 135.0, 112.5, 90.0, 67.5, 45.0, 22.5, 0.0 };
 
   9 static int acurite_raincounter = 0;
 
  11 static int acurite_crc(uint8_t row[BITBUF_COLS], int cols) {
 
  12     // sum of first n-1 bytes modulo 256 should equal nth byte
 
  15     for ( i=0; i < cols; i++)
 
  17     if ( sum % 256 == row[cols] )
 
  23 static int acurite_detect(uint8_t *pRow) {
 
  25     if ( pRow[0] != 0x00 ) {
 
  26         // invert bits due to wierd issue
 
  27         for (i = 0; i < 8; i++)
 
  28             pRow[i] = ~pRow[i] & 0xFF;
 
  29         pRow[0] |= pRow[8];  // fix first byte that has mashed leading bit
 
  31         if (acurite_crc(pRow, 7))
 
  32             return 1;  // passes crc check
 
  37 static float acurite_getTemp (uint8_t highbyte, uint8_t lowbyte) {
 
  39     int highbits = (highbyte & 0x0F) << 7 ;
 
  40     int lowbits = lowbyte & 0x7F;
 
  41     int rawtemp = highbits | lowbits;
 
  42     float temp = (rawtemp - 400) / 10.0;
 
  46 static int acurite_getWindSpeed (uint8_t highbyte, uint8_t lowbyte) {
 
  47     // range: 0 to 159 kph
 
  48         // TODO: sensor does not seem to be in kph, e.g.,
 
  49         // a value of 49 here was registered as 41 kph on base unit
 
  50         // value could be rpm, etc which may need (polynomial) scaling factor??
 
  51         int highbits = ( highbyte & 0x1F) << 3;
 
  52     int lowbits = ( lowbyte & 0x70 ) >> 4;
 
  53     int speed = highbits | lowbits;
 
  57 static float acurite_getWindDirection (uint8_t byte) {
 
  58     // 16 compass points, ccw from (NNW) to 15 (N)
 
  59     int direction = byte & 0x0F;
 
  60     return acurite_winddirections[direction];
 
  63 static int acurite_getHumidity (uint8_t byte) {
 
  65     int humidity = byte & 0x7F;
 
  69 static int acurite_getRainfallCounter (uint8_t hibyte, uint8_t lobyte) {
 
  70     // range: 0 to 99.99 in, 0.01 in incr., rolling counter?
 
  71         int raincounter = ((hibyte & 0x7f) << 7) | (lobyte & 0x7F);
 
  75 static int acurite5n1_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS],int16_t bits_per_row[BITBUF_ROWS]) {
 
  76     // acurite 5n1 weather sensor decoding for rtl_433
 
  80     // run through rows til we find one with good crc (brute force)
 
  81     for (i=0; i < BITBUF_ROWS; i++) {
 
  82         if (acurite_detect(bb[i])) {
 
  90         fprintf(stdout, "SENSOR:TYPE=ACURITE_5IN1,");
 
  93                 fprintf(stderr, "%02X ", buf[i]);
 
  94             fprintf(stderr, "CRC OK\n");
 
  97         if ((buf[2] & 0x0F) == 1) {
 
  98             // wind speed, wind direction, rainfall
 
 100             float rainfall = 0.00;
 
 101             int raincounter = acurite_getRainfallCounter(buf[5], buf[6]);
 
 102             if (acurite_raincounter > 0) {
 
 103                 // track rainfall difference after first run
 
 104                 rainfall = ( raincounter - acurite_raincounter ) * 0.01;
 
 106                 // capture starting counter
 
 107                 acurite_raincounter = raincounter;
 
 110             fprintf(stdout, "WINDSPEED=%d,",
 
 111                 acurite_getWindSpeed(buf[3], buf[4]));
 
 112             fprintf(stdout, "WINDDIRECTION=%0.1f,",
 
 113                 acurite_getWindDirection(buf[4]));
 
 114             fprintf(stdout, "RAINGAUGE=%0.2f\n", rainfall);
 
 116         } else if ((buf[2] & 0x0F) == 8) {
 
 117             // wind speed, temp, RH
 
 118             fprintf(stdout, "WINDSPEED=%d,",
 
 119                 acurite_getWindSpeed(buf[3], buf[4]));
 
 120             fprintf(stdout, "TEMPERATURE=%2.1f,",
 
 121                 acurite_getTemp(buf[4], buf[5]));
 
 122             fprintf(stdout, "HUMIDITY=%d\n",
 
 123                 acurite_getHumidity(buf[6]));
 
 130         debug_callback(bb, bits_per_row);
 
 135 static int acurite_rain_gauge_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS], int16_t bits_per_row[BITBUF_ROWS]) {
 
 136     // This needs more validation to positively identify correct sensor type, but it basically works if message is really from acurite raingauge and it doesn't have any errors
 
 137     if ((bb[0][0] != 0) && (bb[0][1] != 0) && (bb[0][2]!=0) && (bb[0][3] == 0) && (bb[0][4] == 0)) {
 
 138             float total_rain = ((bb[0][1]&0xf)<<8)+ bb[0][2];
 
 139                 total_rain /= 2; // Sensor reports number of bucket tips.  Each bucket tip is .5mm
 
 140                 fprintf(stdout, "SENSOR:TYPE=ACURITE_RAIN_GAUGE,RAIN=%2.1f\n", total_rain);
 
 141                 fprintf(stderr, "Raw Message: %02x %02x %02x %02x %02x\n",bb[0][0],bb[0][1],bb[0][2],bb[0][3],bb[0][4]);
 
 147 static int acurite_th_detect(uint8_t *buf){
 
 148     if(buf[5] != 0) return 0;
 
 149     uint8_t sum = (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff;
 
 150     if(sum == 0) return 0;
 
 151     return sum == buf[4];
 
 153 static float acurite_th_temperature(uint8_t *s){
 
 154     uint16_t shifted = (((s[1] & 0x0f) << 8) | s[2]) << 4; // Logical left shift
 
 155     return (((int16_t)shifted) >> 4) / 10.0; // Arithmetic right shift
 
 157 static int acurite_th_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS], int16_t bits_per_row[BITBUF_ROWS]) {
 
 160     for(i = 0; i < BITBUF_ROWS; i++){
 
 161         if(acurite_th_detect(bb[i])){
 
 167         fprintf(stdout, "SENSOR:TYPE=ACURITE_TEMP,");
 
 168         fprintf(stdout, "TEMPERATURE=%.1f,", acurite_th_temperature(buf));
 
 169         fprintf(stderr, "HUMIDITY=%d\n", buf[3]);
 
 176 r_device acurite5n1 = {
 
 178     /* .name           = */ "Acurite 5n1 Weather Station",
 
 179     /* .modulation     = */ OOK_PWM_P,
 
 180     /* .short_limit    = */ 70,
 
 181     /* .long_limit     = */ 240,
 
 182     /* .reset_limit    = */ 21000,
 
 183     /* .json_callback  = */ &acurite5n1_callback,
 
 186 r_device acurite_rain_gauge = {
 
 188     /* .name           = */ "Acurite 896 Rain Gauge",
 
 189     /* .modulation     = */ OOK_PWM_D,
 
 190     /* .short_limit    = */ 1744/4,
 
 191     /* .long_limit     = */ 3500/4,
 
 192     /* .reset_limit    = */ 5000/4,
 
 193     /* .json_callback  = */ &acurite_rain_gauge_callback,
 
 196 r_device acurite_th = {
 
 198     /* .name           = */ "Acurite Temperature and Humidity Sensor",
 
 199     /* .modulation     = */ OOK_PWM_D,
 
 200     /* .short_limit    = */ 300,
 
 201     /* .long_limit     = */ 550,
 
 202     /* .reset_limit    = */ 2500,
 
 203     /* .json_callback  = */ &acurite_th_callback,