-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmartmeter.cpp
417 lines (351 loc) · 14.9 KB
/
smartmeter.cpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "espMQTT.h"
#include "smartmeter.h"
#define SMARTMETER_BUFFER_SIZE 400
/*
* CRC Calculation inherited from: https://github.com/jantenhove/P1-Meter-ESP8266/blob/master/P1Meter.ino
*
*/
unsigned int currentCRC=0;
static struct
{
struct
{
struct
{
int32_t total = UINT32_MAX;
struct
{
uint32_t t1 = UINT32_MAX;
uint32_t t2 = UINT32_MAX;
} fromgrid;
struct
{
uint32_t t1 = UINT32_MAX;
uint32_t t2 = UINT32_MAX;
} togrid;
} kWh;
struct
{
int16_t total = UINT16_MAX; // Watt
struct
{
uint16_t total = UINT16_MAX; // Watt
uint16_t l1 = UINT16_MAX; // Watt
uint16_t l2 = UINT16_MAX; // Watt
uint16_t l3 = UINT16_MAX; // Watt
} fromgrid;
struct
{
uint16_t total = UINT16_MAX; // Watt
uint16_t l1 = UINT16_MAX; // Watt
uint16_t l2 = UINT16_MAX; // Watt
uint16_t l3 = UINT16_MAX; // Watt
} togrid;
} kW;
struct
{
uint16_t l1 = UINT16_MAX; // deciVolt
uint16_t l2 = UINT16_MAX; // deciVolt
uint16_t l3 = UINT16_MAX; // deciVolt
} V;
struct
{
uint16_t l1 = UINT16_MAX; // centiAmps
uint16_t l2 = UINT16_MAX; // centiAmps
uint16_t l3 = UINT16_MAX; // centiAmps
} A;
} electricity;
struct {
uint32_t m3 = UINT32_MAX; // liter
uint32_t oldm3 = UINT32_MAX; // liter
uint16_t m3h = UINT16_MAX; // liter/hour
char datetime[20] = ""; // date time
} gas;
} smartmetervalues;
void(*_smartmeter_callback)(const char*,const String&);
String valuetodecimalstring(uint64_t value, uint8_t decimals)
{
if (UINT64_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(uint32_t value, uint8_t decimals)
{
if (UINT32_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(uint16_t value, uint8_t decimals)
{
if (UINT16_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(uint8_t value, uint8_t decimals)
{
if (UINT8_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(int64_t value, uint8_t decimals)
{
if (INT64_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(int32_t value, uint8_t decimals)
{
if (INT32_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(int16_t value, uint8_t decimals)
{
if (INT16_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
String valuetodecimalstring(int8_t value, uint8_t decimals)
{
if (INT8_MAX == value) return "-";
return String(double(value) / pow(10,decimals), decimals);
}
unsigned int CRC16(unsigned int crc, unsigned char *buf, int len)
{
for (int pos = 0; pos < len; pos++)
{
crc ^= (unsigned int)buf[pos]; // XOR byte into least sig. byte of crc
for (int i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1; // Shift right and XOR 0xA001
crc ^= 0xA001;
}
else // Else LSB is not set
crc >>= 1; // Just shift right
}
}
return crc;
}
void smartmeter_init(void(*callback)(const char *, const String&))
{
_smartmeter_callback = callback;
Serial.setRxBufferSize(400);
Serial.begin(115200, SERIAL_8N1);
U0C0 = BIT(UCRXI) | BIT(UCBN) | BIT(UCBN + 1) | BIT(UCSBN); // Inverse RX
}
int8_t smartmeter_handle()
/*
* Return value:
* 0 = Complete packet received ok, data is complete and ready to be used
* 1 = Line received ok, packet not completed yet
* 2 = CRC Error
* 3 = Wrong serial characters received
* 4 = Line not recognized
* 5 = Buffer overflow
*/
{
float value = 0;
int8_t returnvalue = 0;
static char buffer[SMARTMETER_BUFFER_SIZE];
static uint16_t bufpos = 0;
while (Serial.available())
{
char input = Serial.read() & 127;
// Fill buffer up to and including a new line (\n)
if (bufpos < SMARTMETER_BUFFER_SIZE - 1)
{
buffer[bufpos++] = input;
}
else
{
if (Debug.isActive(Debug.ERROR)) Debug.printf("SERIAL BUFFER OVERFLOW\n");
bufpos = 0;
buffer[bufpos] = 0;
return 5;
}
buffer[bufpos] = 0;
if ((input == '\n') && (bufpos > 1))
{ // We received a new line (data up to \n)
bool validCRCFound = false;
uint8_t nroflinefeeds = 0;
if (strchr(buffer,'\r')) nroflinefeeds++;
if (strchr(buffer,'\n')) nroflinefeeds++;
if (Debug.isActive(Debug.VERBOSE)) Debug.printf(cF("RECEIVED FROM SERIAL:'%.*s'\n"), strlen(buffer) - nroflinefeeds, buffer);
if(buffer[0] == '/')
{
//start found. Reset CRC calculation
currentCRC=CRC16(0x0000,(unsigned char *) buffer, bufpos);
_smartmeter_callback(cF("status"), sF("receiving"));
buffer[0] = 0;
bufpos = 0;
return 1;
}
else if(buffer[0] == '!')
{
//add to crc calc
currentCRC=CRC16(currentCRC,(unsigned char*)buffer, 1);
char messageCRC[5];
strncpy(messageCRC, buffer + 1, 4);
messageCRC[4]=0; //thanks to HarmOtten (issue 5)
validCRCFound = (strtoul(messageCRC, NULL, 16) == currentCRC);
bufpos = 0;
buffer[bufpos] = 0;
currentCRC = 0;
if (validCRCFound)
{
if (Debug.isActive(Debug.VERBOSE)) Debug.printf(cF("SMARTMETER CRC RECEIVE OK\n"));
smartmetervalues.electricity.kWh.total = (smartmetervalues.electricity.kWh.fromgrid.t1 != UINT16_MAX ? smartmetervalues.electricity.kWh.fromgrid.t1 : 0) +
(smartmetervalues.electricity.kWh.fromgrid.t2 != UINT16_MAX ? smartmetervalues.electricity.kWh.fromgrid.t2 : 0) -
(smartmetervalues.electricity.kWh.togrid.t1 != UINT16_MAX ? smartmetervalues.electricity.kWh.togrid.t1 : 0) -
(smartmetervalues.electricity.kWh.togrid.t2 != UINT16_MAX ? smartmetervalues.electricity.kWh.togrid.t2 : 0);
smartmetervalues.electricity.kW.total = (smartmetervalues.electricity.kW.fromgrid.total != UINT16_MAX ? smartmetervalues.electricity.kW.fromgrid.total : 0) -
(smartmetervalues.electricity.kW.togrid.total != UINT16_MAX ? smartmetervalues.electricity.kW.togrid.total : 0);
_smartmeter_callback(cF("electricity/kWh/total"), valuetodecimalstring(smartmetervalues.electricity.kWh.total, 3));
_smartmeter_callback(cF("electricity/kWh/import/t1"), valuetodecimalstring(smartmetervalues.electricity.kWh.fromgrid.t1, 3));
_smartmeter_callback(cF("electricity/kWh/import/t2"), valuetodecimalstring(smartmetervalues.electricity.kWh.fromgrid.t2, 3));
_smartmeter_callback(cF("electricity/kWh/export/t1"), valuetodecimalstring(smartmetervalues.electricity.kWh.togrid.t1, 3));
_smartmeter_callback(cF("electricity/kWh/export/t2"), valuetodecimalstring(smartmetervalues.electricity.kWh.togrid.t2, 3));
_smartmeter_callback(cF("electricity/W"), valuetodecimalstring(smartmetervalues.electricity.kW.total, 0));
_smartmeter_callback(cF("electricity/kW/import"), valuetodecimalstring(smartmetervalues.electricity.kW.fromgrid.total, 3));
_smartmeter_callback(cF("electricity/kW/import/l1"), valuetodecimalstring(smartmetervalues.electricity.kW.fromgrid.l1, 3));
_smartmeter_callback(cF("electricity/kW/import/l2"), valuetodecimalstring(smartmetervalues.electricity.kW.fromgrid.l2, 3));
_smartmeter_callback(cF("electricity/kW/import/l3"), valuetodecimalstring(smartmetervalues.electricity.kW.fromgrid.l3, 3));
_smartmeter_callback(cF("electricity/kW/export"), valuetodecimalstring(smartmetervalues.electricity.kW.togrid.total, 3));
_smartmeter_callback(cF("electricity/kW/export/l1"), valuetodecimalstring(smartmetervalues.electricity.kW.togrid.l1, 3));
_smartmeter_callback(cF("electricity/kW/export/l2"), valuetodecimalstring(smartmetervalues.electricity.kW.togrid.l2, 3));
_smartmeter_callback(cF("electricity/kW/export/l3"), valuetodecimalstring(smartmetervalues.electricity.kW.togrid.l3, 3));
_smartmeter_callback(cF("electricity/V/l1"), valuetodecimalstring(smartmetervalues.electricity.V.l1, 1));
_smartmeter_callback(cF("electricity/V/l2"), valuetodecimalstring(smartmetervalues.electricity.V.l2, 1));
_smartmeter_callback(cF("electricity/V/l3"), valuetodecimalstring(smartmetervalues.electricity.V.l3, 1));
_smartmeter_callback(cF("electricity/A/l1"), valuetodecimalstring(smartmetervalues.electricity.A.l1, 2));
_smartmeter_callback(cF("electricity/A/l2"), valuetodecimalstring(smartmetervalues.electricity.A.l2, 2));
_smartmeter_callback(cF("electricity/A/l3"), valuetodecimalstring(smartmetervalues.electricity.A.l3, 2));
_smartmeter_callback(cF("gas/m3"), valuetodecimalstring(smartmetervalues.gas.m3, 3));
_smartmeter_callback(cF("gas/m3h"), valuetodecimalstring(smartmetervalues.gas.m3h, 3));
_smartmeter_callback(cF("gas/datetime"), smartmetervalues.gas.datetime);
_smartmeter_callback(cF("status"), sF("ready"));
return 0;
}
else
{
if (Debug.isActive(Debug.ERROR)) Debug.printf(cF("SMARTMETER CRC RECEIVE ERROR\n"));
_smartmeter_callback(cF("status"), sF("crcerror"));
return 2;
}
}
else
{
int day, month, year, hour, minute, second;
char summerwinter;
currentCRC=CRC16(currentCRC, (unsigned char*)buffer, bufpos);
// 1-0:1.8.1 = Electricity low tarif used
if (sscanf(buffer, cF("1-0:1.8.1(%f") , &value) == 1)
{
smartmetervalues.electricity.kWh.fromgrid.t1 = value * 1000;
}
// 1-0:1.8.2 = Electricity high tarif used (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:1.8.2(%f") , &value) == 1)
{
smartmetervalues.electricity.kWh.fromgrid.t2 = value * 1000;
}
// 1-0:2.8.1 = Electricity low tarif provided
else if (sscanf(buffer, cF("1-0:2.8.1(%f"), &value) == 1)
{
smartmetervalues.electricity.kWh.togrid.t1 = value * 1000;
}
// 1-0:2.8.2 = Electricity high tarif provided (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:2.8.2(%f"), &value) == 1)
{
smartmetervalues.electricity.kWh.togrid.t2 = value * 1000;
}
// 1-0:1.7.0 = Electricity actual usage from grid (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:1.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.fromgrid.total = value * 1000;
}
// 1-0:21.7.0 = Electricity actual usage from grid l1(DSMR v4.0)
else if (sscanf(buffer, cF("1-0:21.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.fromgrid.l1 = value * 1000;
}
// 1-0:41.7.0 = Electricity actual usage from grid l2(DSMR v4.0)
else if (sscanf(buffer, cF("1-0:41.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.fromgrid.l2 = value * 1000;
}
// 1-0:61.7.0 = Electricity actual usage from grid l3(DSMR v4.0)
else if (sscanf(buffer, cF("1-0:61.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.fromgrid.l3 = value * 1000;
}
// 1-0:2.7.0 = Electricity actual providing to grid (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:2.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.togrid.total = value * 1000;
}
// 1-0:22.7.0 = Electricity actual providing to grid l1 (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:22.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.togrid.l1 = value * 1000;
}
// 1-0:42.7.0 = Electricity actual providing to grid l1 (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:42.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.togrid.l2 = value * 1000;
}
// 1-0:62.7.0 = Electricity actual providing to grid l1 (DSMR v4.0)
else if (sscanf(buffer, cF("1-0:62.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.kW.togrid.l3 = value * 1000;
}
// 1-0:32.7.0 = Electricity voltage l1 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:32.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.V.l1 = value * 10;
}
// 1-0:52.7.0 = Electricity voltage l2 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:52.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.V.l2 = value * 10;
}
// 1-0:72.7.0 = Electricity voltage l3 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:72.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.V.l3 = value * 10;
}
// 1-0:31.7.0 = Electricity current l1 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:31.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.A.l1 = value * 100;
}
// 1-0:51.7.0 = Electricity current l2 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:51.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.A.l2 = value * 100;
}
// 1-0:71.7.0 = Electricity current l3 (DSMR v5.0)
else if (sscanf(buffer, cF("1-0:71.7.0(%f"), &value) == 1)
{
smartmetervalues.electricity.A.l3 = value * 100;
}
// 0-1:24.2.1 = Gas (DSMR v4.0)
else if (sscanf(buffer, cF("0-1:24.2.1(%2d%2d%2d%2d%2d%2d%c)(%f*m3)"), &day, &month, &year, &hour, &minute, &second, &summerwinter, &value) == 8)
{
smartmetervalues.gas.m3 = value * 1000;
sprintf(smartmetervalues.gas.datetime, cF("%02d-%02d-%02d %d:%02d:%02d"), day, month, year, hour, minute, second);
static uint8_t oldhour = 255;
if (oldhour != hour)
{
oldhour = hour;
if (smartmetervalues.gas.oldm3 != UINT32_MAX) smartmetervalues.gas.m3h = (smartmetervalues.gas.m3 - smartmetervalues.gas.oldm3);
smartmetervalues.gas.oldm3 = smartmetervalues.gas.m3;
}
}
else
{
buffer[0] = 0;
bufpos = 0;
return 4;
}
buffer[0] = 0;
bufpos = 0;
return 1;
}
}
yield();
}
return returnvalue;
}