Редизайн на основе текущей ветки мейнстрима + новые устройства.
[rtl-433.git] / src / devices / emontx.c
1 /* OpenEnergyMonitor.org emonTx sensor protocol
2  *
3  * Copyright © 2016 Tommy Vestermark
4  * Copyright © 2016 David Woodhouse
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #include "rtl_433.h"
12 #include "util.h"
13
14 // We don't really *use* this because there's no endianness support
15 // for just using le16_to_cpu(pkt.ct1) etc. A task for another day...
16 struct emontx {
17         uint8_t syn, group, node, len;
18         uint16_t ct1, ct2, ct3, ct4, Vrms, temp[6];
19         uint32_t pulse;
20         uint16_t crc;
21         uint8_t postamble;
22 } __attribute__((packed));
23
24 // This is the JeeLibs RF12 packet format as described at
25 // http://jeelabs.org/2011/06/09/rf12-packet-format-and-design/
26 //
27 // The RFM69 chip misses out the zero bit at the end of the
28 // 0xAA 0xAA 0xAA preamble; the receivers only use it to set
29 // up the bit timing, and they look for the 0x2D at the start
30 // of the packet. So we'll do the same — except since we're
31 // specifically looking for emonTx packets, we can require a
32 // little bit more. We look for a group of 0xD2, and we
33 // expect the CDA bits in the header to all be zero:
34 static unsigned char preamble[3] = { 0xaa, 0xaa, 0xaa };
35 static unsigned char pkt_hdr_inverted[3] = { 0xd2, 0x2d, 0xc0 };
36 static unsigned char pkt_hdr[3] = { 0x2d, 0xd2, 0x00 };
37
38 static int emontx_callback(bitbuffer_t *bitbuffer) {
39         bitrow_t *bb = bitbuffer->bb;
40         unsigned bitpos = 0;
41         unsigned bits = bitbuffer->bits_per_row[0];
42         int events = 0;
43
44         // Search for only 22 bits to cope with inverted frames and
45         // the missing final preamble bit with RFM69 transmissions.
46         while ((bitpos = bitbuffer_search(bitbuffer, 0, bitpos,
47                                           preamble, 22)) < bitbuffer->bits_per_row[0]) {
48                 int inverted = 0;
49                 unsigned pkt_pos;
50                 uint16_t crc;
51                 data_t *data;
52                 char time_str[LOCAL_TIME_BUFLEN];
53                 union {
54                         struct emontx p;
55                         uint8_t b[sizeof(struct emontx)];
56                 } pkt;
57                 uint16_t words[14];
58                 double vrms;
59                 unsigned i;
60
61                 bitpos += 22;
62
63                 // Eat any additional 101010 sequences (which might be attributed
64                 // to noise at the start of the packet which coincidentally matches).
65                 while (bitbuffer_search(bitbuffer, 0, bitpos, preamble, 2) == bitpos)
66                         bitpos += 2;
67
68                 // Account for RFM69 bug which drops a zero bit at the end of the
69                 // preamble before the 0x2d SYN byte. And for inversion.
70                 bitpos--;
71
72                 // Check for non-inverted packet header...
73                 pkt_pos = bitbuffer_search(bitbuffer, 0, bitpos,
74                                            pkt_hdr, 11);
75
76                 // And for inverted, if it's not found close enough...
77                 if (pkt_pos > bitpos + 5) {
78                         pkt_pos = bitbuffer_search(bitbuffer, 0, bitpos,
79                                                    pkt_hdr_inverted, 11);
80                         if (pkt_pos > bitpos + 5)
81                                 continue;
82                         inverted = 1;
83                 }
84
85                 // Need enough data for a full packet (including postamble)
86                 if (pkt_pos + sizeof(pkt)*8 > bitbuffer->bits_per_row[0])
87                         break;
88
89                 // Extract the group even though we matched on it; the CRC
90                 // covers it too. And might as well have the 0x2d too for
91                 // alignment.
92                 bitbuffer_extract_bytes(bitbuffer, 0, pkt_pos,
93                                         (uint8_t *)&pkt, sizeof(pkt) * 8);
94                 if (inverted) {
95                         for (i=0; i<sizeof(pkt); i++)
96                                 pkt.b[i] ^= 0xff;
97                 }
98                 if (pkt.p.len != 0x1a || pkt.p.postamble != 0xaa)
99                         continue;
100                 crc = crc16((uint8_t *)&pkt.p.group, 0x1d, 0xa001, 0xffff);
101
102                 // Ick. If we could just do le16_to_cpu(pkt.p.ct1) we wouldn't need this.
103                 for (i=0; i<14; i++)
104                         words[i] = pkt.b[4 + (i * 2)] | (pkt.b[5 + i * 2] << 8);
105                 if (crc != words[13])
106                         continue;
107
108                 vrms = (double)words[4] / 100.0;
109
110                 local_time_str(0, time_str);
111                 data = data_make("time", "", DATA_STRING, time_str,
112                                  "model", "", DATA_STRING, "emonTx",
113                                  "node", "", DATA_FORMAT, "%02x", DATA_INT, pkt.p.node & 0x1f,
114                                  "ct1", "", DATA_FORMAT, "%d", DATA_INT, (int16_t)words[0],
115                                  "ct2", "", DATA_FORMAT, "%d", DATA_INT, (int16_t)words[1],
116                                  "ct3", "", DATA_FORMAT, "%d", DATA_INT, (int16_t)words[2],
117                                  "ct4", "", DATA_FORMAT, "%d", DATA_INT, (int16_t)words[3],
118                                  "Vrms/batt", "", DATA_FORMAT, "%.2f", DATA_DOUBLE, vrms,
119                                  "pulse", "", DATA_FORMAT, "%u", DATA_INT, words[11] | ((uint32_t)words[12] << 16),
120                                  // Slightly horrid... a value of 300.0°C means 'no reading'. So omit them completely.
121                                  words[5] == 3000 ? NULL : "temp1_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[5] / 10.0,
122                                  words[6] == 3000 ? NULL : "temp2_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[6] / 10.0,
123                                  words[7] == 3000 ? NULL : "temp3_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[7] / 10.0,
124                                  words[8] == 3000 ? NULL : "temp4_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[8] / 10.0,
125                                  words[9] == 3000 ? NULL : "temp5_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[9] / 10.0,
126                                  words[10] == 3000 ? NULL : "temp6_C", "", DATA_FORMAT, "%.1f", DATA_DOUBLE, (double)words[10] / 10.0,
127                                  NULL);
128                 data_acquired_handler(data);
129                 events++;
130         }
131         return events;
132 }
133
134 static char *output_fields[] = {
135         "time", "model", "node", "ct1", "ct2", "ct3", "ct4", "Vrms/batt",
136         "temp1_C", "temp2_C", "temp3_C", "temp4_C", "temp5_C", "temp6_C",
137         "pulse", NULL
138 };
139
140 r_device emontx = {
141         .name           = "emonTx OpenEnergyMonitor",
142         .modulation     = FSK_PULSE_PCM,
143         .short_limit    = 2000000.0 / (49230 + 49261), // 49261kHz for RFM69, 49230kHz for RFM12B
144         .long_limit     = 2000000.0 / (49230 + 49261),
145         .reset_limit    = 1200, // 600 zeros...
146         .json_callback  = &emontx_callback,
147         .disabled       = 0,
148         .demod_arg      = 0,
149         .fields         = output_fields,
150 };