-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSMRloggerAPI.ino
490 lines (408 loc) · 17.3 KB
/
DSMRloggerAPI.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
***************************************************************************
** Program : DSMRloggerAPI (restAPI)
*/
#define _FW_VERSION "v2.0.1 (04-07-2020)"
/*
** Copyright (c) 2020 Willem Aandewiel / Martijn Hendriks
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*
* TODO
* - v1/dev/settings .js file (opslaan settings)
* Backlog
* - ringfile : fout bestaande file - oude renamen en nieuwe maken
* - api: laatste 10 errors/excepties
* - telegram message bij drempelwaardes
* - telegram verbruiksrapport einde dag/week/maand
* - telegram bot gebruiken als UI
Arduino-IDE settings for DSMR-logger Version 1 (ESP-M3):
- Board: "Generic ESP8266 Module"
- Flash mode: "DOUT" | "DIO" // change only after power-off and on again!
- Flash size: "1MB (FS: 128KB OAT:~438KB)"
- DebugT port: "Disabled"
- DebugT Level: "None"
- IwIP Variant: "v2 Lower Memory"
- Reset Method: "none" // but will depend on the programmer!
- Crystal Frequency: "26 MHz"
- VTables: "Flash"
- Flash Frequency: "40MHz"
- CPU Frequency: "80 MHz"
- Buildin Led: "2" // GPIO02
- Upload Speed: "115200"
- Erase Flash: "Only Sketch"
- Port: <select correct port>
*/
/*
** You can find more info in the following links (all in Dutch):
** https://willem.aandewiel.nl/index.php/2020/02/28/restapis-zijn-hip-nieuwe-firmware-voor-de-dsmr-logger/
** https://mrwheel-docs.gitbook.io/dsmrloggerapi/
** https://mrwheel.github.io/DSMRloggerWS/
*/
/******************** compiler options ********************************************/
#define USE_REQUEST_PIN // define if it's a esp8266 with GPIO 12 connected to SM DTR pin
//#define USE_UPDATE_SERVER // define if there is enough memory and updateServer to be used
// #define USE_BELGIUM_PROTOCOL // define if Slimme Meter is a Belgium Smart Meter
// #define USE_PRE40_PROTOCOL // define if Slimme Meter is pre DSMR 4.0 (2.2 .. 3.0)
// #define USE_NTP_TIME // define to generate Timestamp from NTP (Only Winter Time for now)
// #define HAS_NO_SLIMMEMETER // define for testing only!
#define USE_MQTT // define if you want to use MQTT (configure through webinterface)
//#define USE_MINDERGAS // define if you want to update mindergas (configure through webinterface)
// #define USE_SYSLOGGER // define if you want to use the sysLog library for debugging
// #define SHOW_PASSWRDS // well .. show the PSK key and MQTT password, what else?
/******************** don't change anything below this comment **********************/
#include "DSMRloggerAPI.h"
struct showValues {
template<typename Item>
void apply(Item &i) {
TelnetStream.print(F("showValues: "));
if (i.present())
{
TelnetStream.print(Item::name);
TelnetStream.print(F(": "));
TelnetStream.print(i.val());
TelnetStream.print(Item::unit());
//} else
//{
// TelnetStream.print(F("<no value>"));
}
TelnetStream.println();
}
};
#ifdef USE_SYSLOGGER
//===========================================================================================
void openSysLog(bool empty)
{
if (sysLog.begin(500, 100, empty)) // 500 lines use existing sysLog file
{
DebugTln(F("Succes opening sysLog!"));
}
else
{
DebugTln(F("Error opening sysLog!"));
}
sysLog.setDebugLvl(1);
sysLog.setOutput(&TelnetStream);
sysLog.status();
sysLog.write("\r\n");
for (int q=0;q<3;q++)
{
sysLog.write("******************************************************************************************************");
}
writeToSysLog(F("Last Reset Reason [%s]"), ESP.getResetReason().c_str());
writeToSysLog("actTimestamp[%s], nrReboots[%u], Errors[%u]", actTimestamp
, nrReboots
, slotErrors);
sysLog.write(" ");
} // openSysLog()
#endif
//===========================================================================================
void setup()
{
#ifdef USE_PRE40_PROTOCOL //PRE40
Serial.begin(9600, SERIAL_7E1); //PRE40
#else // not use_dsmr_30 //PRE40
Serial.begin(115200, SERIAL_8N1);
#endif // use_dsmr_30
pinMode(LED_BUILTIN, OUTPUT);
pinMode(FLASH_BUTTON, INPUT);
swSer1.begin(115200);
#ifdef DTR_ENABLE
pinMode(DTR_ENABLE, OUTPUT);
#endif
//--- setup randomseed the right way
//--- This is 8266 HWRNG used to seed the Random PRNG
//--- Read more: https://config9.com/arduino/getting-a-truly-random-number-in-arduino/
randomSeed(RANDOM_REG32);
snprintf(settingHostname, sizeof(settingHostname), "%s", _DEFAULT_HOSTNAME);
Serial.printf("\n\nBooting....[%s]\r\n\r\n", String(_FW_VERSION).c_str());
for(int I=0; I<8; I++)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
}
digitalWrite(LED_BUILTIN, LED_OFF); // HIGH is OFF
lastReset = ESP.getResetReason();
startTelnet();
Serial.println("The board name is: " ARDUINO_BOARD);
//================ SPIFFS ===========================================
if (SPIFFS.begin())
{
DebugTln(F("SPIFFS Mount succesfull\r"));
SPIFFSmounted = true;
} else {
DebugTln(F("SPIFFS Mount failed\r")); // Serious problem with SPIFFS
SPIFFSmounted = false;
}
//------ read status file for last Timestamp --------------------
//==========================================================//
// writeLastStatus(); // only for firsttime initialization //
//==========================================================//
readLastStatus(); // place it in actTimestamp
// set the time to actTimestamp!
actT = epoch(actTimestamp, strlen(actTimestamp), true);
DebugTf("===> actTimestamp[%s]-> nrReboots[%u] - Errors[%u]\r\n\n", actTimestamp
, nrReboots++
, slotErrors);
readSettings(true);
//=============start Networkstuff==================================
digitalWrite(LED_BUILTIN, LED_ON);
startWiFi(settingHostname, 240); // timeout 4 minuten
digitalWrite(LED_BUILTIN, LED_OFF);
Debugln();
Debug (F("Connected to " )); Debugln (WiFi.SSID());
Debug (F("IP address: " )); Debugln (WiFi.localIP());
Debug (F("IP gateway: " )); Debugln (WiFi.gatewayIP());
Debugln();
for (int L=0; L < 10; L++) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(200);
}
digitalWrite(LED_BUILTIN, LED_OFF);
//-----------------------------------------------------------------
#ifdef USE_SYSLOGGER
openSysLog(false);
snprintf(cMsg, sizeof(cMsg), "SSID:[%s], IP:[%s], Gateway:[%s]", WiFi.SSID().c_str()
, WiFi.localIP().toString().c_str()
, WiFi.gatewayIP().toString().c_str());
writeToSysLog("%s", cMsg);
#endif
startMDNS(settingHostname);
//=============end Networkstuff======================================
#if defined(USE_NTP_TIME) //USE_NTP
//================ startNTP =========================================
//USE_NTP
//USE_NTP
if (!startNTP()) //USE_NTP
{ //USE_NTP
DebugTln(F("ERROR!!! No NTP server reached!\r\n\r")); //USE_NTP
//USE_NTP
delay(2000); //USE_NTP
ESP.restart(); //USE_NTP
delay(3000); //USE_NTP
} //USE_NTP
//USE_NTP
prevNtpHour = hour(); //USE_NTP
//USE_NTP
#endif //USE_NTP_TIME //USE_NTP
//================ end NTP =========================================
snprintf(cMsg, sizeof(cMsg), "Last reset reason: [%s]\r", ESP.getResetReason().c_str());
DebugTln(cMsg);
Serial.print(F("\nGebruik 'telnet "));
Serial.print (WiFi.localIP());
Serial.println(F("' voor verdere debugging\r\n"));
//=============now test if SPIFFS is correct populated!============
if (DSMRfileExist(settingIndexPage, false) )
{
if (strcmp(settingIndexPage, "DSMRindexEDGE.html") != 0)
{
if (settingIndexPage[0] != '/')
{
char tempPage[50] = "/";
strConcat(tempPage, 49, settingIndexPage);
strCopy(settingIndexPage, sizeof(settingIndexPage), tempPage);
}
hasAlternativeIndex = true;
}
else hasAlternativeIndex = false;
}
if (!hasAlternativeIndex && !DSMRfileExist("/DSMRindexEDGE.html", false) )
{
spiffsNotPopulated = true;
}
if (!DSMRfileExist("/FSexplorer.html", true))
{
spiffsNotPopulated = true;
}
//=============end SPIFFS =========================================
#ifdef USE_SYSLOGGER
if (spiffsNotPopulated)
{
sysLog.write(F("SPIFFS is not correct populated (files are missing)"));
}
#endif
#if defined(USE_NTP_TIME) //USE_NTP
time_t t = now(); // store the current time in time variable t //USE_NTP
snprintf(cMsg, sizeof(cMsg), "%02d%02d%02d%02d%02d%02dW\0\0" //USE_NTP
, (year(t) - 2000), month(t), day(t) //USE_NTP
, hour(t), minute(t), second(t)); //USE_NTP
pTimestamp = cMsg; //USE_NTP
DebugTf("Time is set to [%s] from NTP\r\n", cMsg); //USE_NTP
#endif // use_dsmr_30
//================ Start MQTT ======================================
#ifdef USE_MQTT //USE_MQTT
connectMQTT(); //USE_MQTT
#endif //USE_MQTT
//================ End of Start MQTT ===============================
//================ Start HTTP Server ================================
if (!spiffsNotPopulated) {
DebugTln(F("SPIFFS correct populated -> normal operation!\r"));
if (hasAlternativeIndex)
{
httpServer.serveStatic("/", SPIFFS, settingIndexPage);
httpServer.serveStatic("/index", SPIFFS, settingIndexPage);
httpServer.serveStatic("/index.html", SPIFFS, settingIndexPage);
httpServer.serveStatic("/DSMRindex.html", SPIFFS, settingIndexPage);
}
else
{
httpServer.serveStatic("/", SPIFFS, "/DSMRindexEDGE.html");
httpServer.serveStatic("/DSMRindex.html", SPIFFS, "/DSMRindexEDGE.html");
httpServer.serveStatic("/index", SPIFFS, "/DSMRindexEDGE.html");
httpServer.serveStatic("/index.html", SPIFFS, "/DSMRindexEDGE.html");
}
} else {
DebugTln(F("Oeps! not all files found on SPIFFS -> present FSexplorer!\r"));
spiffsNotPopulated = true;
}
setupFSexplorer();
httpServer.serveStatic("/FSexplorer.png", SPIFFS, "/FSexplorer.png");
httpServer.on("/api", HTTP_GET, processAPI);
// all other api calls are catched in FSexplorer onNotFounD!
httpServer.begin();
DebugTln( F("HTTP server gestart\r") );
for (int i = 0; i< 10; i++)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(250);
}
//================ Start HTTP Server ================================
//test(); monthTabel
#ifdef USE_MINDERGAS
handleMindergas();
#endif
DebugTf("Startup complete! actTimestamp[%s]\r\n", actTimestamp);
writeToSysLog("Startup complete! actTimestamp[%s]", actTimestamp);
//================ End of Slimmer Meter ============================
//================ The final part of the Setup =====================
snprintf(cMsg, sizeof(cMsg), "Last reset reason: [%s]\r", ESP.getResetReason().c_str());
DebugTln(cMsg);
//================ Start Slimme Meter ===============================
DebugTln(F("Enable slimmeMeter..\r"));
//martijn
//#if defined( USE_REQUEST_PIN ) && !defined( HAS_NO_SLIMMEMETER )
// DebugTf("Swapping serial port to Smart Meter, debug output will continue on telnet\r\n");
// DebugFlush();
// Serial.swap();
//#endif // is_esp12
//martijn
delay(100);
slimmeMeter.enable(true);
} // setup()
//===[ no-blocking delay with running background tasks in ms ]============================
DECLARE_TIMER_MS(timer_delay_ms, 1);
void delayms(unsigned long delay_ms)
{
CHANGE_INTERVAL_MS(timer_delay_ms, delay_ms);
RESTART_TIMER(timer_delay_ms);
while (!DUE(timer_delay_ms))
{
doSystemTasks();
}
} // delayms()
//========================================================================================
//==[ Do Telegram Processing ]===============================================================
void doTaskTelegram()
{
if (Verbose1) DebugTln("doTaskTelegram");
#if defined(HAS_NO_SLIMMEMETER)
handleTestdata();
#else
//-- enable DTR to read a telegram from the Slimme Meter
slimmeMeter.enable(true);
slimmeMeter.loop();
handleSlimmemeter();
#endif
if (WiFi.status() != WL_CONNECTED)
{
for(int b=0; b<10; b++) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); delay(75);}
}
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
//===[ Do System tasks ]=============================================================
void doSystemTasks()
{
#ifndef HAS_NO_SLIMMEMETER
slimmeMeter.loop();
#endif
#ifdef USE_MQTT
MQTTclient.loop();
#endif
httpServer.handleClient();
MDNS.update();
handleKeyInput();
yield();
} // doSystemTasks()
void loop ()
{
//--- do the tasks that has to be done
//--- as often as possible
doSystemTasks();
loopCount++;
//--- verwerk volgend telegram
if DUE(nextTelegram)
{
doTaskTelegram();
}
//--- update upTime counter
if DUE(updateSeconds)
{
upTimeSeconds++;
}
//--- if mindergas then check
#ifdef USE_MINDERGAS
if ( DUE(minderGasTimer) )
{
handleMindergas();
}
#endif
//--- if connection lost, try to reconnect to WiFi
if ( DUE(reconnectWiFi) && (WiFi.status() != WL_CONNECTED) )
{
writeToSysLog("Restart wifi with [%s]...", settingHostname);
startWiFi(settingHostname, 10);
if (WiFi.status() != WL_CONNECTED)
writeToSysLog("%s", "Wifi still not connected!");
else {
snprintf(cMsg, sizeof(cMsg), "IP:[%s], Gateway:[%s]", WiFi.localIP().toString().c_str()
, WiFi.gatewayIP().toString().c_str());
writeToSysLog("%s", cMsg);
}
}
//--- if NTP set, see if it needs synchronizing
#if defined(USE_NTP_TIME) //USE_NTP
if DUE(synchrNTP) //USE_NTP
{
if (timeStatus() == timeNeedsSync || prevNtpHour != hour()) //USE_NTP
{
prevNtpHour = hour(); //USE_NTP
setSyncProvider(getNtpTime); //USE_NTP
setSyncInterval(600); //USE_NTP
}
}
#endif //USE_NTP
yield();
} // loop()
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/