1 /* OpenEnergyMonitor.org emonTx sensor protocol
3 * Copyright © 2016 Tommy Vestermark
4 * Copyright © 2016 David Woodhouse
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.
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...
17 uint8_t syn, group, node, len;
18 uint16_t ct1, ct2, ct3, ct4, Vrms, temp[6];
22 } __attribute__((packed));
24 // This is the JeeLibs RF12 packet format as described at
25 // http://jeelabs.org/2011/06/09/rf12-packet-format-and-design/
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 };
38 static int emontx_callback(bitbuffer_t *bitbuffer) {
39 bitrow_t *bb = bitbuffer->bb;
41 unsigned bits = bitbuffer->bits_per_row[0];
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]) {
52 char time_str[LOCAL_TIME_BUFLEN];
55 uint8_t b[sizeof(struct emontx)];
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)
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.
72 // Check for non-inverted packet header...
73 pkt_pos = bitbuffer_search(bitbuffer, 0, bitpos,
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)
85 // Need enough data for a full packet (including postamble)
86 if (pkt_pos + sizeof(pkt)*8 > bitbuffer->bits_per_row[0])
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
92 bitbuffer_extract_bytes(bitbuffer, 0, pkt_pos,
93 (uint8_t *)&pkt, sizeof(pkt) * 8);
95 for (i=0; i<sizeof(pkt); i++)
98 if (pkt.p.len != 0x1a || pkt.p.postamble != 0xaa)
100 crc = crc16((uint8_t *)&pkt.p.group, 0x1d, 0xa001, 0xffff);
102 // Ick. If we could just do le16_to_cpu(pkt.p.ct1) we wouldn't need this.
104 words[i] = pkt.b[4 + (i * 2)] | (pkt.b[5 + i * 2] << 8);
105 if (crc != words[13])
108 vrms = (double)words[4] / 100.0;
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,
128 data_acquired_handler(data);
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",
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,
149 .fields = output_fields,