-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDSMR-API-V2.ino
244 lines (203 loc) · 8.41 KB
/
DSMR-API-V2.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
/*
***************************************************************************
** Program : DSMRloggerAPI (restAPI)
**
** Copyright (c) 2021 Martijn Hendriks
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*
TODO
- telnet update via windows ... invoeren lukt niet
- Jos: zou je de dag totalen zoals op het dashboard ook via MQTT kunnen exporteren? Gas_dag, Water_dag, Afname_dag, Teruglevering_dag, Afname-Terug_dag
- data voor dag tabel eens per dag
- data voor maand tabel eens per maand
- bugje : 3.5.7 spanning in dash werkt niet meer (ben)
- actueel display teruglevering kleuren (ben)
Arduino-IDE settings for DSMR-logger hardware ESP12S module:
- Board: "Generic ESP8266 Module" //https://arduino.esp8266.com/stable/package_esp8266com_index.json
- Flash mode: "QIO"
- Flash size: "4MB (FS: 1MB OAT:~1019KB)"
- 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: "160MHz"
- Buildin Led: 2
- Upload Speed: "460800"
- Erase Flash: "Only Sketch"
- Port: <select correct port>
*/
/******************** compiler options ********************************************/
//#define SHOW_PASSWRDS // well .. show the PSK key and MQTT password, what else?
//#define DEBUG_MODE
//#define V2_COMPATIBLE // Spiffs version firmware 2.x compatible ESP-M(2/3) HARDWARE
//#define NO_HIST
//----- EXTENSIONS
#define HA_DISCOVER
#define ALL_OPTIONS "[CORE]"
#include "DSMRloggerAPI.h"
//===========================================================================================
void setup()
{
Serial.begin(115200, SERIAL_8N1);
pinMode(DTR_ENABLE, OUTPUT);
pinMode(LED, OUTPUT); //LED ESP12S
// sign of life
digitalWrite(LED, LOW); //ON
delay(1500);
digitalWrite(LED, HIGH); //OFF
//--- 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);
Debugln(F("\n\n ----> BOOTING....[" _VERSION "] <-----\n"));
DebugTln(F("The board name is: " ARDUINO_BOARD));
strcpy(lastReset , ESP.getResetReason().c_str());
DebugT(F("Last reset reason: ")); Debugln(lastReset);
startTelnet();
//================ Read Persistance Settings ====================
P1StatusBegin();
if (P1StatusAvailable()) P1StatusRead(); //load persistant data
//================ FS ===========================================
if (FS.begin())
{
DebugTln(F("File System Mount succesful"));
FSmounted = true;
} else {
DebugTln(F("/!\\File System Mount failed/!\\")); // Serious problem with FS
}
// set the time to actTimestamp!
actT = epoch(actTimestamp, strlen(actTimestamp), true);
P1Status.reboots++;
// DebugTf("===> actTimestamp[%s]-> nrReboots[%u] - Errors[%u]\r\n\n", actTimestamp, P1Status.reboots, P1Status.sloterrors);
P1StatusWrite();
P1StatusPrint(); //print latest values
readSettings(true);
LogFile("",false); // write reboot status to file
//=============start Networkstuff==================================
startWiFi(settingHostname, 240); // timeout 4 minuten
startMDNS(settingHostname);
//================ Start MQTT ======================================
if ( (strlen(settingMQTTbroker) > 3) && settingMQTTinterval) connectMQTT();
//================ Start HTTP Server ================================
if (!DSMRfileExist(settingIndexPage, false) ) CreateIndexHtml();
if (DSMRfileExist(settingIndexPage, false) ) {
DebugTln(F("File System correct populated -> normal operation!\r"));
httpServer.serveStatic("/", FS, settingIndexPage);
httpServer.serveStatic("/DSMRindex.html", FS, settingIndexPage);
httpServer.serveStatic(_DEFAULT_HOMEPAGE, FS, settingIndexPage);
} else DebugTln(F("Oeps! not all files found on File System -> present FSexplorer!\r"));
setupFSexplorer();
// delay(500);
// DebugTf("Startup complete! actTimestamp[%s]\r\n", actTimestamp);
//================ Start Slimme Meter ===============================
#ifndef V2_COMPATIBLE
ConvRing3_2_0(); //only new modules
#else
FS.remove("/Reboot.log"); //pre 3.1.1
FS.remove("/Reboot.old"); //pre 3.1.1
FS.remove("/DSMRstatus.json"); //pre 3.1.1
#endif
#ifdef NO_HIST
if ( EnableHistory ) {
EnableHistory = false;
FS.remove(RingFiles[0].filename);
FS.remove(RingFiles[1].filename);
FS.remove(RingFiles[2].filename);
FS.remove("/P1_log.old");
FS.remove("/Reboot.old"); //pre 3.1.1
writeSettings(); //write to settings file
}
#endif
if (EnableHistory) CheckRingExists();
#ifndef DEBUG_MODE
DebugTln(F("Swapping serial port to Smart Meter, debug output will continue on telnet"));
Debug(F("\nGebruik 'telnet ")); Debug (WiFi.localIP()); Debugln(F("' voor verdere debugging"));
DebugFlush();
delay(200);
Serial.swap();
delay(200);
DebugTln(F("Enable slimmeMeter...\n"));
slimmeMeter.enable(true);
#else
Debug(F("\n!!! DEBUG MODE AAN !!!\n\n"));
#endif
//check old ota url and update with new --- only once at startup
if( strcmp(BaseOTAurl,"http://smart-stuff.nl/ota/") == 0 ) {
Debugln(F("Old OTA url available"));
strcpy(BaseOTAurl, "http://ota.smart-stuff.nl/");
writeSettings();
}
} // 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");
slimmeMeter.loop(); //voorkomen dat de buffer nog vol zit met andere data
//-- enable DTR to read a telegram from the Slimme Meter
slimmeMeter.enable(true);
}
//===[ Do System tasks ]=============================================================
void doSystemTasks()
{
slimmeMeter.loop();
handleSlimmemeter();
MQTTclient.loop();
httpServer.handleClient();
MDNS.update();
handleKeyInput();
yield();
} // doSystemTasks()
void loop ()
{
//--- verwerk volgend telegram
if DUE(nextTelegram) doTaskTelegram();
//--- update upTime counter
if DUE(updateSeconds) upTimeSeconds++;
//--- do the tasks that has to be done as often as possible
doSystemTasks();
if ( DUE(StatusTimer) && (telegramCount > 2) ) {
P1StatusWrite();
MQTTSentStaticInfo();
CHANGE_INTERVAL_MIN(StatusTimer, 23);
}
if (UpdateRequested) RemoteUpdate(UpdateVersion, true);
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.
*
***************************************************************************/