-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1WG2022current.ino
732 lines (653 loc) · 24.7 KB
/
P1WG2022current.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
* Copyright (c) 2022 Ronald Leenes
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* @file P1WG2022current.ino
* @author Ronald Leenes
* Hans Schneider
* @date 22.05.2023
* @version 1.2d
*
* @brief This file contains the main file for the P1 wifi gatewway
*
* @see http://esp8266thingies.nl
*/
/*
* P1 wifi gateway 2022
*
* Deze software brengt verschillende componenten bij elkaar in een handzaam pakket.
*
* De software draait op een ESP8285 of ESP8266 en heeft een bescheiden voetafdruk.
*
* De data processing is gebaseerd op: http://domoticx.com/p1-poort-slimme-meter-data-naar-domoticz-esp8266/
* De captive portal is gebaseerd op een van de ESP8266 voorbeelden, de OTA eenheid eveneens.
*
* De module levert data aan een host via JSON of MQTT messages, maar kan ook via de webinterface van de module of via telnet worden uitgelezen
*
* De module zal bij opstarten eerst de buffercondensator laden.
* Vervolgens knippert hij 2x en zal eerst proberen te koppelen aan je wifi netwerk. Gedurende dat proces zal de LED knipperen
* met een periode van 0.5 Hz.
* Wanneer geen verbinding kan worden gemaakt start de modiule een Access point onder de naam P1_Setup
* Verbind met dit netwerk en surf naar 192.168.4.1
*
* Het menuscherm van de module verschijnt dan. Vul daar SSID en wachtwoord van je wifi netwerk in.
* Daaronder vul je gegevens van de ontvangede server in (IP en poortnummer). Dit kan je DOmotociz server zijn
* In dat geval moet je ook het IDx dat Domoticz maakt voor een Gas device en een Energy device invoeren.
* Tenslotte geef je aan per hoeveel seconde je data naar de server wilt laten sturen.
* DSMR4 meters hanteren een meetinterval van 10 sec. SMR5 meters leveren iedere seconde data. Dat lijkt me wat veel en
* kost de module ook veel stroom waardoor het maar de vraag is of hij parasitair (door de meter gevoed) kan werken.
* Ik raad een interval van 15 sec of hoger aan. Het interval dat je hier invoert zal niet altijd synchroon lopen met
* het data interval van de meter, ga er dus niet van uit dat je exact iedere x seconden een meetwaarde ziet in DOmoticz.
* Tussen metingen 'slaapt' de module (de modem wordt afgeschakeld waardoor het stroomverbruik van zo'n 70mA terugvalt naar 15 mA).
* Dit geeft de bufferelco tijd omm op te laden voor de stroompiekjes die de wifi zender van de module produceert
* (en het bespaart hoe dan ook wat stroom (die weliswaar door je energieleverancier wordt betaald, maar toch). Alle kleine
* beetjes helpen..
*
* informatie, vragen, suggesties ed richten aan [email protected]
*
*
*
* versie: 1.2d
* datum: 15 January 2025
* auteur: Ronald Leenes
* Hans Schneider
*
* 1.2d after changing cFos Power Brain password from it's default the new password isn't now forgotten anymore
* 1.2c measurement page refreshes every 20 seconds now
* P1meter now sends positive values for actualElectricityPowerDelivered and negative values for
* actualElectricityPowerReturned to cFos Power Brain wallbox. That is important to calculate correct
* values for feeding energy into the power grid.
* 1.2b changes for cFos Power Brain FW 1.19.4: power_va is now power_w, is_va doesn't exist anymore
* improvements of Netherlands, French and Swedish language
* 1.2ab display of module voltage in footer of webserver pages (re)activated, bug fixes
* 1.2a: debugging French and Swedish versions and improvements of translations (needs to be checked)
* 1.2: bug fixes
* improvements for T211 SmartMeter in Belgium
* implemented support for HTTP input meter in cFos Power Brain wallbox
* note: for this to work your cFos Charging Manager firmware has to be at least 1.17.4-beta
* debugging French and Swedish versions and improvements of translations (needs to be checked)
* 1.1b cleaning up, bug fixes, cosmetic changes
* 1.1adc
* 1.1ad: bug fixes and graph improvements
* 1.1aa: bug fixes
* 1.1: implemented graphs
*
*
* ud: fixed refreshheader
* ua: fixed setup field issue
* u:
* password on Setup and Firmware Update
* made mqtt re-connect non-blocking
* incorporated "DSMR Reader" mqtt topics
* fixed hardboot daycounters reset
* fixed sending empty MQTT messages
*
* t: improvements on powermanagement, overall (minor) cleanup
* ta: fix for Telnet reporting
*
* s: added German localisation
* Added mqtt output for Swedish specific OBIS codes
*
* r: speed improvements and streamlining of the parser
* localisations for: NL, SE
*
* q: added daily values
* p: incorporated equipmentID in mqtt set
* o: fixed gas output, fixed mqtt reconnect
* n: empty call to SetupSave now redirects to main menu instead of resetting settings ;-)
* fixed kWh/Wh inversion
* m: setupsave fix, relocate to p1wifi.local na 60 sec
* mqtt - kw/W fix
* l: wifireconnect after wifi loss
* k: fixed big BUG, softAP would not produce accessible webserver.
* j: raw data on port 23
* major code rewrite
* implemented data reporting methods:
* parsed data: json, mqtt, p1wifi.local/P1
* raw data: telnet on port 23, p1wifi.local/Data
*
* i: extended max line length for TD210-D meters, which have a really loong 1-0:99.97.0 line
* h: extended mqtt set with instant powers, voltages, actual tarif group. power outages, power drops
* g: fixed mqtt
*
* Generic ESP8285 module
* Flash Size: 2mb (FS: 128Kb, OTA: –960Kb)
*
* needed files:
* this one (obv),
* vars.h in the process of moving all vars here
* lang.h
* CRC16.h
* JSON.ino
* MQTT.ino
* TELNET.ino
* debug.ino
* decoder.ino
* functions.ino
* graph.ino
* webserver.ino
* webserverDE.ino
* webserverSE.ino
* webserverNL.ino
* wifi.ino
*
*/
bool zapfiles = false; //false; //true;
#define GERMAN // GERMAN / FRENCH / NEDERLANDS / SWEDISH
#ifdef GERMAN
String sfx = "DE";
#endif
#ifdef FRENCH
String sfx = "FR";
#endif
#ifdef NEDERLANDS
String sfx = "NL";
#endif
#ifdef SWEDISH
String sfx = "SE";
#endif
String version = "1.2d – " + sfx;
#define HOSTNAME "p1meter"
#define FSystem 1 // 0 = LittleFS 1 = SPIFFS
#define GRAPH 1
#define V3
#define DEBUG 1 //0 //1 //2 //3 // 1 is on serial only, 2 is serial + telnet
#define ESMR5 1
//#define SLEEP_ENABLED
#define LED_BUILTIN 2
const uint32_t wakeTime = 90000; // stay awake wakeTime millisecs
const uint32_t sleepTime = 5000; //sleep sleepTime millisecs
#if DEBUG == 1
const char* host = "P1test";
#define BLED LED_BUILTIN
#define debug(x) Serial.print(x)
#define debugf(x) Serial.printf(x)
#define debugff(x,y) Serial.printf(x,y)
#define debugfff(x,y,z) Serial.printf(x,y,z)
#define debugln(x) Serial.println(x)
#elif DEBUG == 2
#define BLED LED_BUILTIN
#define debug(x) Serial.print(x);if(telnetConnected)telnet.print(x)
#define debugf(x) Serial.printf(x)
#define debugff(x,y) Serial.printf(x,y)
#define debugfff(x,y,z) Serial.printf(x,y,z)
#define debugln(x) Serial.println(x);if(telnetConnected)telnet.println(x)
#elif DEBUG == 3
const char* host = "P1home";
#define BLED LED_BUILTIN
#define debug(x)
#define debugln(x)
#define debugf(x)
#define debugff(x,y)
#define debugfff(x,y,z)
#else
const char* host = "P1wifi";
#define BLED LED_BUILTIN
#define debug(x)
#define debugln(x)
#define debugf(x)
#define debugff(x,y)
#define debugfff(x,y,z)
#endif
#define errorHalt(msg) {debugln(F(msg)); return;}
#define NUM(off, mult) ((P1timestamp[(off)] - '0') * (mult)) // macro for getting time out of timestamp, see decoder
#define ToggleLED digitalWrite(BLED, !digitalRead(BLED));
#define LEDoff digitalWrite(BLED, HIGH);
#define LEDon digitalWrite(BLED, LOW);
#define OE 16 //IO16 OE on the 74AHCT1G125
#define DR 4 //IO4 is Data Request
#define WLED 12 //IO12 (D6) is WiFi-LED, uncomment if it's present
#include <TZ.h>
#define MYTZ TZ_Europe_Amsterdam
#include <TimeLib.h>
#include <coredecls.h> // settimeofday_cb()
#include <MyAlarm.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include "CRC16.h"
#if GRAPH == 1
#if FSystem == 0
#include <LittleFS.h>
#define FST LittleFS
#elif FSystem == 1
#include "FS.h" //SPIFFS
#define FST SPIFFS
#endif
File file;
#endif
// van ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino
#ifndef RTC_config_data_SLOT_WIFI_STATE
#define RTC_config_data_SLOT_WIFI_STATE 33u
#endif
#include <include/WiFiState.h> // WiFiState structure details
//#include <WiFiState.h> // WiFiState structure details
WiFiState WiFistate;
//van captive portal
/* ============================================================================================================
Captive Portal based on:
https://gitlab.com/MrDIYca/code-samples/-/raw/master/esp8266_setup_portal.ino
For more info, please watch my video at https://youtu.be/1VW-z4ibMXI
MrDIY.ca
============================================================================================================== */
#include <EEPROM.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
// mqtt stuff . https://github.com/ict-one-nl/P1-Meter-ESP8266-MQTT/blob/master/P1Meter/P1Meter.ino
#include <PubSubClient.h>
WiFiClient espClient; // * Initiate WIFI client
PubSubClient mqtt_client(espClient); // * Initiate MQTT client
bool MQTT_Server_Fail = false;
long nextMQTTreconnectAttempt;
// end mqtt stuff
//// Raw data server
#include "ESPTelnet.h"
ESPTelnet telnet;
uint16_t port = 23;
IPAddress ip;
bool telnetConnected = false;
// end raw data stuff
#include "vars.h"
//#include "lang.h"
struct settings {
char dataSet[4];
char ssid[32];
char password[32];
char domoticzIP[30] = "IPaddress";
char domoticzPort[10] = "Port";
char domoticzEnergyIdx[10] = "EnergyIdx";
char domoticzGasIdx[10] = "GasIdx";
char mqttTopic[50] = "sensors/power/p1meter";
char mqttIP[30] = "";
char mqttPort[10] = "";
char mqttUser[32] = "";
char mqttPass[32] = "";
char cfosUsr[32] = "admin";
char cfosPwd[32] = "1234abcd";
char cfosIP[30] = "192.168.1.183";
char cfosPort[10] = "80";
char cfosID[5] = "M4";
char cfosModel[30] = "HTTP_Input";
char interval[3] = "20";
// char cfosVA[4] = "n";
char domo[4] = "j";
char mqtt[4] = "n";
char cfos[4] = "j";
char watt[4] = "n";
char telnet[4] = "n";
char debug[4] = "n";
char adminPassword[32];
} config_data = {};
float RFpower = 0; //20.5; // (For OdBm of output power, lowest. Supply current~70mA)
//20.5dBm (For 20.5dBm of output, highest. Supply current~85mA)
ADC_MODE(ADC_VCC); // allows you to monitor the internal VCC level;
float volts;
// Vars to store meter readings
// we capture all data in char arrays or Strings for longer hard to delineate data
String P1header;
char P1version[8];
int P1prot; // 4 or 5 based on P1version 1-3:0.2.8
char P1timestamp[30] = "\0";
char equipmentId[100] = "\0";
char electricityUsedTariff1[12];
char electricityUsedTariff2[12];
char electricityReturnedTariff1[12];
char electricityReturnedTariff2[12];
char tariffIndicatorElectricity[8];
char actualElectricityPowerDelivered[8];
char actualElectricityPowerReturned[8];
char numberPowerFailuresAny[6];
char numberLongPowerFailuresAny[6];
String longPowerFailuresLog;
char numberVoltageSagsL1[7];
char numberVoltageSagsL2[7];
char numberVoltageSagsL3[7];
char numberVoltageSwellsL1[7];
char numberVoltageSwellsL2[7];
char numberVoltageSwellsL3[7];
String textMessage;
char instantaneousVoltageL1[7];
char instantaneousVoltageL2[7];
char instantaneousVoltageL3[7];
char instantaneousCurrentL1[9];
char instantaneousCurrentL2[9];
char instantaneousCurrentL3[9];
char activePowerL1P[9];
char activePowerL2P[9];
char activePowerL3P[9];
char activePowerL1NP[9];
char activePowerL2NP[9];
char activePowerL3NP[9];
// Swedish specific
char cumulativeActiveImport[12]; // 1.8.0
char cumulativeActiveExport[12]; // 2.8.0
char cumulativeReactiveImport[12]; // 3.8.0
char cumulativeReactiveExport[12]; // 4.8.0
char momentaryActiveImport[12]; // 1.7.0
char momentaryActiveExport[12]; // 2.7.0
char momentaryReactiveImport[12]; // 3.7.0
char momentaryReactiveExport[12]; // 4.7.0
char momentaryReactiveImportL1[12]; // 23.7.0
char momentaryReactiveImportL2[12]; // 43.7.0
char momentaryReactiveImportL3[12]; // 63.7.0
char momentaryReactiveExportL1[12]; // 24.7.0
char momentaryReactiveExportL2[12]; // 44.7.0
char momentaryReactiveExportL3[12]; // 64.7.0
char reactivePowerL1P[9]; // Sweden uses these 6
char reactivePowerL2P[9];
char reactivePowerL3P[9];
char reactivePowerL1NP[9];
char reactivePowerL2NP[9];
char reactivePowerL3NP[9];
// end Swedish
char deviceType[5];
char gasId[100] = "\0";;
char gasReceived5min[12];
char gasDomoticz[12]; //Domoticz wil gas niet in decimalen?
char prevGAS[12]; // not an P1 protocol var, but holds gas value
// char dayStartGaz[12];
// char dayStartUsedT1[12];
// char dayStartUsedT2[12];
// char dayStartReturnedT1[12];
// char dayStartReturnedT2[12];
// char monthStartGaz[12];
// char monthStartUsedT1[12];
// char monthStartUsedT2[12];
// char monthStartReturnedT1[12];
// char monthStartReturnedT2[12];
void setup() {
WiFi.forceSleepBegin(sleepTime * 1000000L); //In uS. Must be same length as your delay
delay(10); // it doesn't always go to sleep unless you delay(10); yield() wasn't reliable
delay(sleepTime); //Hang out at 15mA for (sleeptime) seconds
WiFi.forceSleepWake(); // Wifi on
pinMode(BLED, OUTPUT);
Serial.begin(115200);
debugln("Serial.begin(115200);");
pinMode(OE, OUTPUT); //IO16 OE on the 74AHCT1G125
digitalWrite(OE, HIGH); // Put(Keep) OE in Tristate mode
pinMode(DR, OUTPUT); //IO4 Data Request
digitalWrite(DR, LOW); // DR low (only goes high when we want to receive data)
#ifdef WLED
pinMode(WLED, OUTPUT);
digitalWrite(WLED, LOW); // LED is on when WiFi is connected
#endif
blink(3);
debugln("Booting");
debugln("Done with Cap charging … ");
debugln("Let's go …");
EEPROM.begin(sizeof(struct settings));
EEPROM.get(0, config_data);
if (config_data.dataSet[0] != 'j') {
config_data = (settings) {"n", "", "", "0.0.0.0", "8080", "1234", "1235", "sensors/power/p1meter", "0.0.0.0", "1883", "mqtt_user", "mqtt_passwd", "admin", "1234abcd", "0.0.0.0", "80", "M4", "HTTP_Input", "30", "n", "n", "n", "n", "n", "n", "adminpwd"};
}
(config_data.watt[0] == 'j') ? reportInDecimals = false : reportInDecimals = true;
(config_data.domo[0] == 'j') ? Json = true : Json = false;
(config_data.mqtt[0] == 'j') ? Mqtt = true : Mqtt = false;
(config_data.cfos[0] == 'j') ? cfos = true : cfos = false;
(config_data.telnet[0] == 'j') ? Telnet = true : Telnet = false;
(config_data.debug[0] == 'j') ? MQTT_debug = true : MQTT_debug = false;
// if (config_data.cfosVA[0] == 'j') {
// cfosIsVA = "true";
// } else {
// cfosIsVA = "false";
// }
if (strcmp(config_data.mqttTopic, "dsmr") == 0) { // autodetect need to report in 'dsmr reader' mqtt format
mqtt_dsmr = true;
// reportInDecimals = true;
} else {
mqtt_dsmr = false;
// reportInDecimals = false;
}
debugln("EEprom read: done");
PrintConfigData();
interval = atoi(config_data.interval) * 1000;
debug("Internal Interval: ");
debugln(interval);
debugln("Trying to connect to your WiFi network:");
WiFi.mode(WIFI_STA);
WiFi.begin(config_data.ssid, config_data.password);
byte tries = 0;
while (WiFi.status() != WL_CONNECTED) {
ToggleLED
delay(300);
debug("o");
if (tries++ > 30) {
debugln("");
debugln("Setting up Captive Portal by the name 'P1_setup'");
LEDon
WiFi.mode(WIFI_AP);
softAp = WiFi.softAP("P1_Setup", "");
breaking = true;
break;
}
}
debugln("");
debugln("Set up wifi, either in STA or AP mode");
if (softAp){
debugln("running in AP mode, all handles will be initiated");
start_webservices();
}
if (WiFi.status() == WL_CONNECTED) {
WiFi.setAutoReconnect(true);
debugln("HTTP server running.");
debug("IP address: ");
debugln(WiFi.localIP());
setRFPower();
wifiSta = true;
debugln("WiFi running in STA (normal) mode");
LEDoff
#ifdef WLED
pinMode(WLED, OUTPUT);
digitalWrite(WLED, HIGH);
#endif
ESP.rtcUserMemoryWrite(RTC_config_data_SLOT_WIFI_STATE, reinterpret_cast<uint32_t *>(&WiFistate), sizeof(WiFistate));
#ifdef SLEEP_ENABLED
modemSleep();
modemWake();
#else
start_services();
#endif
debugln("All systems are ok...");
state = WAITING; // signal that we are waiting for a valid start char (aka /)
devicestate = CONFIG;
nextUpdateTime = nextMQTTreconnectAttempt = EverSoOften = millis();
monitoring = true; // start monitoring data
time_to_sleep = millis() + wakeTime; // we go to sleep wakeTime seconds from now
// handle Files
debug("Mounting file system ... ");
if (!FST.begin()) {
debugln("FST mount failed");
debug("Formatting file system ... ");
FST.format();
if (!FST.begin()) {
debugln("FST mount failed AGAIN");
} else {
debugln("done.");
if (zapfiles) zapFiles();
}
} else debugln("done.");
debugln("Reading logdata:");
File logData = FST.open("/logData.txt", "r");
if (logData) {
logData.read((byte *)&log_data, sizeof(log_data)/sizeof(byte));
logData.close();
} else {
debugln("Failed to open logData.txt for reading");
needToInitLogVars = true;
needToInitLogVarsGas = true;
}
if (zapfiles) zapFiles();
} // WL connected
//debugln("Something is terribly wrong, no network connection");
timerAlarm.stopService();
settimeofday_cb(timeIsSet_cb);
configTime(MYTZ, "pool.ntp.org");
}
void readTelegram() {
if (Serial.available()) {
memset(telegram, 0, sizeof(telegram));
while (Serial.available()) {
int len = Serial.readBytesUntil('\n', telegram, MAXLINELENGTH);
telegram[len] = '\n';
telegram[len + 1] = 0;
yield();
ToggleLED
if (decodeTelegram(len + 1)) { // test for valid CRC
break;
} else { // we don't have valid data, but still may need to report to Domo
if (dataEnd && !CRCcheckEnabled) { //this is used for dsmr 2.2 meters
// yield(); //state = WAITING;
}
}
}
LEDoff
}
}
void loop() {
if (millis() > nextUpdateTime) {//&& monitoring) {
if (!OEstate) { // OE is disabled == false
RTS_on();
Serial.flush();
}
} // update window
if (OEstate) readTelegram();
#ifdef SLEEP_ENABLED
if ((millis() > time_to_sleep) && !atsleep && wifiSta) { // not currently sleeping and sleeptime
modemSleep();
}
if (wifiSta && (millis() > time_to_wake) && (WiFi.status() != WL_CONNECTED)) { // time to wake, if we're not already awake
modemWake();
}
#endif
if (datagramValid && (state == DONE) && (WiFi.status() == WL_CONNECTED)) {
if (Mqtt) {
doMQTT();
if (MqttDelivered) {
datagramValid = false;
state = WAITING;
MqttDelivered = false;
}
}
if (Json) {
doJSON();
doCFOS();
datagramValid = false;
state = WAITING;
}
if (Telnet) {
TelnetReporter();
datagramValid = false;
state = WAITING;
}
if (MQTT_debug) MQTT_Debug();
state = WAITING;
}
if (softAp || (WiFi.status() == WL_CONNECTED)) {
server.handleClient(); //handle web requests
MDNS.update();
if (Telnet) telnet.loop();
}
if (millis() > EverSoOften){
checkCounters();
resetFlags();
doWatchDogs();
EverSoOften = millis() + 22000;
}
timerAlarm.update();
}
void initTimers(){
#if DEBUG == 1
timerAlarm.createHour(59, 0, doHourlyLog);
timerAlarm.createHour(15, 0, doHourlyLog);
timerAlarm.createHour(30, 0, doHourlyLog);
timerAlarm.createHour(45, 0, doHourlyLog);
#else
timerAlarm.createHour(59, 0, doHourlyLog);
#endif
timerAlarm.createDay(23,58, 0, doDailyLog);
timerAlarm.createWeek(timerAlarm.dw_Sunday, 23, 59, 0, doWeeklyLog);
timerAlarm.createMonth(31, 0, 0, 0, doMonthlyLog);
}
void checkCounters() {
// see logging.ino
// time_t t = now();
if (timeIsSet && !TimeTriggersSet) {
initTimers();
TimeTriggersSet= true;
}
if (coldbootE && gotPowerReading) {
if (needToInitLogVars) {
doInitLogVars();
}
resetEnergyDaycount();
resetEnergyMonthcount();
}
if (coldbootG && gotGasReading) {
if (needToInitLogVarsGas) {
doInitLogVarsGas();
}
resetGasCount();
}
// if (!CHK_FLAG(logFlags, hourFlag) && ( minute() == 10 || minute() == 20 || minute() == 30 || minute() == 40 || minute() == 50) ) doHourlyLog();
// if (!minFlag && second() > 55) doMinutelyLog();
// if (!CHK_FLAG(logFlags, hourFlag) && minute(t) == 59) doHourlyLog();
// if (!CHK_FLAG(logFlags, dayFlag) && (hour(t) == 23) && (minute(t) == 59)) doDailyLog();
// if (!CHK_FLAG(logFlags, weekFlag) && weekday(t) == 1 && hour(t) == 23 && minute(t) == 59) doWeeklyLog(); // day of the week (1-7), Sunday is day 1
// if (!CHK_FLAG(logFlags, monthFlag) && day(t) == 28 && month(t) == 2 && hour(t) == 23 && minute(t) == 59 && year(t) != 2024 && year(t) != 2028 ) doMonthlyLog();
// if (!CHK_FLAG(logFlags, monthFlag) && day(t) == 29 && month(t) == 2 && hour(t) == 23 && minute(t) == 59 ) doMonthlyLog(); // schrikkeljaren
// if (!CHK_FLAG(logFlags, monthFlag) && day(t) == 30 && (month(t) == 4 || month(t) == 6 || month(t) == 9 || month(t) == 11) && hour(t) == 23 && minute(t) == 59 ) doMonthlyLog();
// if (!CHK_FLAG(logFlags, monthFlag) && day(t) == 31 && (month(t) == 1 || month(t) == 3 || month(t) == 5 || month(t) == 7 || month(t) == 8 || month(t) == 10 || month() == 12) && hour() == 23 && minute() == 59 ) doMonthlyLog();
// if (!CHK_FLAG(logFlags, monthFlag) && day(t) == 31 && month(t) == 12 && hour(t) == 23 && minute(t) == 59 ) doYearlyLog();
// doMonthlyLog();
if (!monthFlag && day() == 31 && (month() == 1 || month() == 3 || month() == 5 || month() == 7 || month() == 8 || month() == 10 || month() == 12) && hour() == 23 && minute() == 59 ) doMonthlyLog();
if (!monthFlag && day() == 31 && month() == 12 && hour() == 23 && minute() == 59 ) doYearlyLog();
}
void resetFlags() {
// if (CHK_FLAG(logFlags, hourFlag) && (minute() > 0) ) CLR_FLAG(logFlags, hourFlag);// if (hourFlag && (minute() > 24)) hourFlag = false; // CLR_FLAG(logFlags, hourFlag);
// if (CHK_FLAG(logFlags, dayFlag) && (day() > 0)) CLR_FLAG(logFlags, dayFlag);
// if (CHK_FLAG(logFlags, weekFlag) && (weekday() > 1)) CLR_FLAG(logFlags, weekFlag);
// if (CHK_FLAG(logFlags, monthFlag) && (day() > 0)) CLR_FLAG(logFlags, monthFlag);
// if (CHK_FLAG(logFlags, yearFlag) && (day() == 1) && month() == 1) CLR_FLAG(logFlags, yearFlag);
// debug("logFlags : ");
// Serial.println(logFlags);
if (minFlag && (second() > 5) && (second() < 10)) minFlag = false;
if (checkMinute >= 59) checkMinute = 0;
if (hourFlag && ((minute() > (checkMinute + 2)) && (minute() < (checkMinute + 4)))) hourFlag=false;// if (hourFlag && (minute() > 24)) hourFlag = false; // CLR_FLAG(logFlags, hourFlag);
if (dayFlag && (day() > 0)) dayFlag = false;
if (weekFlag && (weekday() > 1)) weekFlag = false;
if (monthFlag && (day() > 0)) monthFlag = false;
if (yearFlag && (day() == 1) && month() == 1) yearFlag = false;
}
void doWatchDogs(){
char payload[60];
if (ESP.getFreeHeap() < 2000) ESP.reset(); // watchdog, in case we still have a memery leak
if (millis() - LastSample > 300000) {
Serial.flush();
sprintf(payload, "No data in 300 sec, restarting monitoring: ", timestampkaal());
if (MQTT_debug) send_mqtt_message("p1wifi/logging", payload);
hourFlag = false;
nextUpdateTime = millis() + 10000;
OEstate = false;
state = WAITING;
monitoring=true;
}
// if (minute() == 23) hourFlag = false; // clear all flags at a safe timeslot.
// if (minute() == 43) hourFlag = false; // clear all flags at a safe timeslot.
// if (!monitoring && (minute() == 16 || minute() == 31 || minute() == 46) ) monitoring = true; // kludge to make sure we keep monitoring
}