forked from CapnBry/Powermon433
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPowermon433.ino
384 lines (320 loc) · 7.91 KB
/
Powermon433.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Powermon433
Monitoring 433MHz power monitor products (both the same internally)
-- Black and Decker EM100B
-- BlueLine PowerCost Monitor
Protocol decoding by "redgreen"
https://github.com/merbanan/rtl_433
via comments on Stewart C. Russell's blog
http://scruss.com/blog/2013/12/03/blueline-black-decker-power-monitor-rf-packets/
Additional work (aka "most of it" - SCR)
and porting to ATmega by Bryan Mayland
https://github.com/CapnBry/Powermon433
Original OOK interrupt and decoder based on jeelib
"ookRelay2" project https://github.com/jcw/jeelib
General messing about and reformatting
by Stewart C. Russell, scruss.com
https://github.com/scruss/Powermon433
*/
#include <util/atomic.h>
#include "rf69_ook.h"
#include "temp_lerp.h"
// the pin connected to the receiver output
#define DPIN_OOK_RX 8
/*
The default ID of the transmitter to decode from
- it is likely that yours is different.
see README.md on how to check yours and set it here.
*/
#define DEFAULT_TX_ID 0xfff8
/*
TX_ID_LOCK -
Uncomment this #define if you want to lock the decoder ID.
This will prevent nearby stations hijacking your power log
*/
//#define TX_ID_LOCK
/*
TEMPERATURE_F -
Uncomment this #define if you believe in the power of
D. G. Farenheit's armpit, and want your temperature in °F.
Otherwise, you get °C, like you should.
[I'd include an option for K, but it doesn't fit into a byte.]
*/
//#define TEMPERATURE_F
static uint16_t g_TxId;
static struct tagWireVal
{
uint8_t hdr;
union {
uint8_t raw[2];
uint16_t val16;
}
data;
uint8_t crc;
}
wireval;
static struct tagDecoder
{
uint8_t state;
uint8_t pos;
uint8_t bit;
uint8_t data[4];
}
decoder;
static int8_t g_RxTemperature;
static uint8_t g_RxFlags;
static uint16_t g_RxWatts;
static uint16_t g_RxWattHours;
// Watt-hour counter rolls over every 65.536 kWh,
// or roughly 2½ days of my use.
// This should be good for > 4 GWh; enough for everyone ...
static unsigned long g_TotalRxWattHours;
// need this too
static uint16_t g_PrevRxWattHours;
// better stats on time between reports
// delta is long as packets are appx ½ range of uint16_t
// so we roll if we miss >2
static unsigned long g_PrintTime_ms;
static unsigned long g_PrevPrintTime_ms;
static unsigned long g_PrintTimeDelta_ms;
static bool g_RxDirty;
static uint32_t g_RxLast;
static uint8_t g_RxRssi;
#if DPIN_OOK_RX >= 14
#define VECT PCINT1_vect
#elif DPIN_OOK_RX >= 8
#define VECT PCINT0_vect
#else
#define VECT PCINT2_vect
#endif
volatile uint16_t pulse_433;
static void pinChange(void)
{
static uint16_t last_433;
uint16_t now = micros();
uint16_t cnt = now - last_433;
if (cnt > 10)
pulse_433 = cnt;
last_433 = now;
}
ISR(VECT) {
pinChange();
}
static void setupPinChangeInterrupt ()
{
pinMode(DPIN_OOK_RX, INPUT);
#if DPIN_OOK_RX >= 14
bitSet(PCMSK1, DPIN_OOK_RX - 14);
bitSet(PCICR, PCIE1);
#elif DPIN_OOK_RX >= 8
bitSet(PCMSK0, DPIN_OOK_RX - 8);
bitSet(PCICR, PCIE0);
#else
PCMSK2 = bit(DPIN_OOK_RX);
bitSet(PCICR, PCIE2);
#endif
}
// Short burst in uSec
#define OOK_TX_SHORT 500
// Long burst in uSec
#define OOK_TX_LONG 1000
// Inter-packet delay in msec
#define OOK_TX_DELAY 65
// Inter-ID-packet delay in msec
#define OOK_ID_DELAY 225
#define OOK_PACKET_INSTANT 1
#define OOK_PACKET_TEMP 2
#define OOK_PACKET_TOTAL 3
/* crc8 from chromimum project */
__attribute__((noinline)) uint8_t crc8(uint8_t const *data, uint8_t len)
{
uint16_t crc = 0;
for (uint8_t j=0; j<len; ++j)
{
crc ^= (data[j] << 8);
for (uint8_t i=8; i>0; --i)
{
if (crc & 0x8000)
crc ^= (0x1070 << 3);
crc <<= 1;
}
}
return crc >> 8;
}
static void resetDecoder(void)
{
decoder.pos = 0;
decoder.bit = 0;
decoder.state = 0;
}
static void decoderAddBit(uint8_t bit)
{
decoder.data[decoder.pos] = (decoder.data[decoder.pos] << 1) | bit;
if (++decoder.bit > 7)
{
decoder.bit = 0;
if (++decoder.pos >= sizeof(decoder.data))
resetDecoder();
}
}
static bool decodeRxPulse(uint16_t width)
{
// 500,1000,1500 usec pulses with 25% tolerance
if (width > 375 && width < 1875)
{
// The only "extra long" long signals the end of the preamble
if (width > 1200)
{
rf69ook_startRssi();
resetDecoder();
return false;
}
bool isShort = width < 750;
if (decoder.state == 0)
{
// expecting a short to start a bit
if (isShort)
{
decoder.state = 1;
return false;
}
}
else if (decoder.state == 1)
{
decoder.state = 0;
if (isShort)
decoderAddBit(1);
else
decoderAddBit(0);
// If we have all 3 bytes, we're done
if (decoder.pos > 2)
return true;
return false;
}
} // if proper width
resetDecoder();
return false;
}
static void decodePowermon(uint16_t val16)
{
switch (decoder.data[0] & 3)
{
case OOK_PACKET_INSTANT:
// val16 is the number of milliseconds between blinks
// Each blink is one watt hour consumed
g_RxWatts = 3600000UL / val16;
break;
case OOK_PACKET_TEMP:
#if defined(TEMPERATURE_F)
g_RxTemperature = (int8_t)(temp_lerp(decoder.data[1]));
#else
g_RxTemperature = (int8_t)(fudged_f_to_c(temp_lerp(decoder.data[1])));
#endif /* TEMPERATURE_F */
g_RxFlags = decoder.data[0];
break;
case OOK_PACKET_TOTAL:
g_PrevRxWattHours = g_RxWattHours;
g_RxWattHours = val16;
// prevent rollover through the power of unsigned arithmetic
g_TotalRxWattHours += (g_RxWattHours - g_PrevRxWattHours);
break;
}
}
static void decodeRxPacket(void)
{
uint16_t val16 = *(uint16_t *)decoder.data;
#ifndef TX_ID_LOCK
if (crc8(decoder.data, 3) == 0)
{
g_TxId = decoder.data[1] << 8 | decoder.data[0];
Serial.print(F("# New ID: 0x"));
Serial.println(val16, HEX);
return;
}
#endif /* ifndef TX_ID_LOCK */
val16 -= g_TxId;
decoder.data[0] = val16 & 0xff;
decoder.data[1] = val16 >> 8;
if (crc8(decoder.data, 3) == 0)
{
decodePowermon(val16 & 0xfffc);
g_RxDirty = true;
g_RxLast = millis();
}
else
{
Serial.println(F("# CRC ERR"));
}
}
static void rxSetup(void)
{
setupPinChangeInterrupt();
}
static void ookRx(void)
{
uint16_t v;
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
v = pulse_433;
pulse_433 = 0;
}
if (v != 0)
{
if (decodeRxPulse(v) == 1)
{
g_RxRssi = rf69ook_Rssi();
decodeRxPacket();
resetDecoder();
}
}
// If it has been more than 250ms since the last receive, dump the data
else if (g_RxDirty && (millis() - g_RxLast) > 250U)
{
/*
track duration since last report
If > ~31.8 s (318nn ms), we have missed a packet,
and the instantaneous Power reading
isn't continuous.
*/
g_PrevPrintTime_ms = g_PrintTime_ms;
g_PrintTime_ms = millis();
g_PrintTimeDelta_ms = g_PrintTime_ms - g_PrevPrintTime_ms;
Serial.print(F("PrintDelta_ms: "));
Serial.print(g_PrintTimeDelta_ms, DEC);
// this can roll over, so don't print
// Serial.print(F(" Energy_Wh: "));
// Serial.print(g_RxWattHours, DEC);
Serial.print(F(" Total_Energy_Wh: "));
Serial.print(g_TotalRxWattHours, DEC);
Serial.print(F(" Power_W: "));
Serial.print(g_RxWatts, DEC);
#if defined(TEMPERATURE_F)
Serial.print(F(" Temp_F: "));
#else
Serial.print(F(" Temp_C: "));
#endif /* TEMPERATURE_F */
Serial.println(g_RxTemperature, DEC);
g_RxDirty = false;
}
else if (g_RxLast != 0 && (millis() - g_RxLast) > 32000U) {
Serial.println(F("# Missed Packet"));
g_RxLast = millis();
}
}
void setup() {
Serial.begin(38400);
Serial.println(F("# Powermon433 built "__DATE__" "__TIME__));
Serial.print(F("# Listening for Sensor ID: 0x"));
Serial.println(DEFAULT_TX_ID, HEX);
if (rf69ook_init())
Serial.println(F("# RF69 initialized"));
rxSetup();
g_TxId = DEFAULT_TX_ID;
g_TotalRxWattHours = 0;
g_PrintTimeDelta_ms = 0;
g_PrintTime_ms = 0;
}
void loop()
{
ookRx();
}