- Merged with upstream version
[rtl-433.git] / src / devices / rubicson.c
1 #include "rtl_433.h"
2
3 /* Currently this can decode the temperature and id from Rubicson sensors
4  *
5  * the sensor sends 36 bits 12 times pwm modulated
6  * the data is grouped into 9 nibles
7  * [id0] [id1], [unk0] [temp0], [temp1] [temp2], [unk1] [unk2], [unk3]
8  *
9  * The id changes when the battery is changed in the sensor.
10  * unk0 is always 1 0 0 0, most likely 2 channel bits as the sensor can recevice 3 channels
11  * unk1-3 changes and the meaning is unknown
12  * temp is 12 bit signed scaled by 10
13  *
14  * The sensor can be bought at Kjell&Co
15  */
16
17 static int rubicson_callback(uint8_t bb[BITBUF_ROWS][BITBUF_COLS],int16_t bits_per_row[BITBUF_ROWS]) {
18     int temperature_before_dec;
19     int temperature_after_dec;
20     int16_t temp;
21
22     /* FIXME validate the received message better, figure out crc */
23     if (bb[1][0] == bb[2][0] && bb[2][0] == bb[3][0] && bb[3][0] == bb[4][0] &&
24         bb[4][0] == bb[5][0] && bb[5][0] == bb[6][0] && bb[6][0] == bb[7][0] && bb[7][0] == bb[8][0] &&
25         bb[8][0] == bb[9][0] && (bb[5][0] != 0 && bb[5][1] != 0 && bb[5][2] != 0)) {
26
27         /* Nible 3,4,5 contains 12 bits of temperature
28          * The temerature is signed and scaled by 10 */
29         temp = (int16_t)((uint16_t)(bb[0][1] << 12) | (bb[0][2] << 4));
30         temp = temp >> 4;
31
32         temperature_before_dec = abs(temp / 10);
33         temperature_after_dec = abs(temp % 10);
34
35         fprintf(stdout, "SENSOR:TYPE=RUBICSON,");
36         fprintf(stdout, "ID=%x,",bb[0][0]);
37         fprintf(stdout, "TEMPERATURE=%s%d.%d\n",temp<0?"-":"",temperature_before_dec, temperature_after_dec);
38         fprintf(stderr, "%02x %02x %02x %02x %02x\n",bb[1][0],bb[0][1],bb[0][2],bb[0][3],bb[0][4]);
39
40         if (debug_output)
41             debug_callback(bb, bits_per_row);
42
43         return 1;
44     }
45     return 0;
46 }
47
48 // timings based on samp_rate=1024000
49 r_device rubicson = {
50     /* .id             = */ 1,
51     /* .name           = */ "Rubicson Temperature Sensor",
52     /* .modulation     = */ OOK_PWM_D,
53     /* .short_limit    = */ 1744/4,
54     /* .long_limit     = */ 3500/4,
55     /* .reset_limit    = */ 5000/4,
56     /* .json_callback  = */ &rubicson_callback,
57 };
58