-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathHelper.ino
488 lines (414 loc) · 15.3 KB
/
Helper.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
/*
***************************************************************************
** Program : helperStuff, part of DSMRloggerAPI
**
** Copyright (c) 2021 Willem Aandewiel / Martijn Hendriks
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
#ifndef _HELPER
#define _HELPER
#include "rom/rtc.h"
#define _getChipId() (uint64_t)ESP.getEfuseMac()
const PROGMEM char *resetReasons[] { "Unknown", "Vbat power on reset", "2-unknown","Software reset digital core", "Legacy watch dog reset digital core",
"Deep Sleep reset digital core", "Reset by SLC module, reset digital core","Timer Group0 Watch dog reset digital core","Timer Group1 Watch dog reset digital core",
"RTC Watch dog Reset digital core","Instrusion tested to reset CPU","Time Group reset CPU","Software reset CPU","RTC Watch dog Reset CPU","for APP CPU, reseted by PRO CPU",
"Reset when the vdd voltage is not stable","RTC Watch dog reset digital core and rtc module"};
void SetConfig(){
#ifdef NRG_DONGLE
/* check if slot is in use
S1 S2
0 0 -> NONE
0 1 -> n/a
1 0 -> H20
1 1 -> RS485
*/
pinMode(SENSE1, INPUT); //4
pinMode(SENSE2, INPUT); //21
byte board = digitalRead(SENSE1) << 1 + digitalRead(SENSE2);
Debugf("\n--P1NRGD: check ext board [%d]\n",board);
switch ( board ) {
case 0: Debugln("--P1NRGD: No board");break;
case 1: Debugln("--P1NRGD: N/A");break;
case 2: Debugln("--P1NRGD: H20");WtrMtr = true;break;
case 3: Debugln("--P1NRGD: RS485");break;
}
Debugln();
#endif
#ifdef ULTRA
UseRGB = true;
IOWater = IO_WATER;
WtrMtr = false;
P1Out = true;
#else
switch ( P1Status.dev_type ) {
case PRO : UseRGB = false;
IOWater = 5;
break;
case PRO_BRIDGE: UseRGB = true;
IOWater = -1;
pinMode(PRT_LED, OUTPUT);
digitalWrite(PRT_LED, true); //default on
break;
case PRO_ETH: UseRGB = true;
IOWater = -1;
break;
case PRO_H20_B: UseRGB = true;
IOWater = 0;
WtrMtr = true;
break;
case PRO_H20_2: UseRGB = false;
IOWater = 3;
RxP1 = 4;
P1Out = true;
WtrMtr = true;
break;
case P1EP: UseRGB = false;
IOWater = -1;
P1Out = true;
WtrMtr = false;
break;
case P1NRG: UseRGB = false;
IOWater = IO_WATER_SENSOR;
P1Out = true;
// WtrMtr = true;
break;
}
#endif
//pin modes
pinMode(DTR_IO, OUTPUT);
pinMode(LED, OUTPUT);
if ( IOWater != -1 ) pinMode(IOWater, INPUT_PULLUP);
Debugf("Config set UseRGB [%s] IOWater [%d]\n", UseRGB ? "true" : "false", IOWater);
// if ( UseRGB ) rgb.begin();
// sign of life = ON during setup or change config
SwitchLED( LED_ON, LED_BLUE );
delay(2000);
SwitchLED( LED_OFF, LED_BLUE );
}
void FacReset() {
DebugTln(F("/!\\ Factory reset"));
// bFacReset = false;
P1StatusClear();
LittleFS.remove("/DSMRsettings.json");
LittleFS.remove("/fixedip.json");
resetWifi();
}
void ToggleLED(byte mode) {
if ( UseRGB ) {
if ( LEDenabled ) SwitchLED( mode, LED_GREEN );
else { neopixelWrite(RGBLED_PIN,0,0,0); /*rgb.setPixel(LED_BLACK);*/ };
} // PRO_BRIDGE
else if ( LEDenabled ) digitalWrite(LED, !digitalRead(LED)); else digitalWrite(LED, LED_OFF);
}
void ClearRGB(){
R_value = 0;
G_value = 0;
B_value = 0;
}
void SwitchLED( byte mode, uint32_t color) {
if ( UseRGB ) {
if ( LEDenabled ) {
uint32_t value = 0; //off mode
if ( mode == LED_ON ) value = BRIGHTNESS;
if ( color != LED_GREEN ) ClearRGB();
switch ( color ) {
case LED_RED: R_value = value; break;
case LED_GREEN: G_value = value; break;
case LED_BLUE: B_value = value; break;
}
} else ClearRGB();
// rgb.setPixel(R_value+G_value+B_value);
neopixelWrite(RGBLED_PIN,R_value,G_value,B_value);
// rgbLedWrite(RGBLED_PIN,R_value,G_value,B_value); //sdk 3.0
} else {
digitalWrite(LED, color==LED_RED?LED_OFF:mode);
}
}
//===========================================================================================
const char* getResetReason(){
return resetReasons[rtc_get_reset_reason(0)];
}
//===========================================================================================
void ShutDownHandler(){
MQTTDisconnect();
P1StatusWrite();
P1StatusEnd();
DebugTln(F("/!\\ SHUTDOWN /!\\"));
DebugFlush();
}
//===========================================================================================
void P1Reboot(){
delay(500);
ESP.restart();
delay(2000);
}
//===========================================================================================
bool bailout () // to prevent firmware from crashing!
{
if (ESP.getFreeHeap() > 5500) return false; //do nothing
DebugT(F("Bailout due to low heap --> reboot in 3 seconds")); Debugln(ESP.getFreeHeap());
P1Reboot();
return true; // komt hier als het goed is niet
}
//===========================================================================================
bool compare(String x, String y)
{
for (int i = 0; i < min(x.length(), y.length()); i++) {
if (x[i] != y[i])
{
return (bool)(x[i] < y[i]);
}
}
return x.length() < y.length();
} // compare()
//===========================================================================================
boolean isValidIP(IPAddress ip)
{
/* Works as follows:
* example:
* 127.0.0.1
* 1 => 127||0||0||1 = 128>0 = true
* 2 => !(false || false) = true
* 3 => !(false || false || false || false ) = true
* 4 => !(true && true && true && true) = false
* 5 => !(false) = true
* true && true & true && false && true = false ==> correct, this is an invalid addres
*
* 0.0.0.0
* 1 => 0||0||0||0 = 0>0 = false
* 2 => !(true || true) = false
* 3 => !(false || false || false || false) = true
* 4 => !(true && true && true && tfalse) = true
* 5 => !(false) = true
* false && false && true && true && true = false ==> correct, this is an invalid addres
*
* 192.168.0.1
* 1 => 192||168||0||1 =233>0 = true
* 2 => !(false || false) = true
* 3 +> !(false || false || false || false) = true
* 4 => !(false && false && true && true) = true
* 5 => !(false) = true
* true & true & true && true && true = true ==> correct, this is a valid address
*
* 255.255.255.255
* 1 => 255||255||255||255 =255>0 = true
* 2 => !(false || false ) = true
* 3 +> !(true || true || true || true) = false
* 4 => !(false && false && false && false) = true
* 5 => !(true) = false
* true && true && false && true && false = false ==> correct, this is an invalid address
*
* 0.123.12.1 => true && false && true && true && true = false ==> correct, this is an invalid address
* 10.0.0.0 => true && false && true && true && true = false ==> correct, this is an invalid address
* 10.255.0.1 => true && true && false && true && true = false ==> correct, this is an invalid address
* 150.150.255.150 => true && true && false && true && true = false ==> correct, this is an invalid address
*
* 123.21.1.99 => true && true && true && true && true = true ==> correct, this is annvalid address
* 1.1.1.1 => true && true && true && true && true = true ==> correct, this is annvalid address
*
* Some references on valid ip addresses:
* - https://www.quora.com/How-do-you-identify-an-invalid-IP-address
*
*/
boolean _isValidIP = false;
_isValidIP = ((ip[0] || ip[1] || ip[2] || ip[3])>0); // if any bits are set, then it is not 0.0.0.0
_isValidIP &= !((ip[0]==0) || (ip[3]==0)); // if either the first or last is a 0, then it is invalid
_isValidIP &= !((ip[0]==255) || (ip[1]==255) || (ip[2]==255) || (ip[3]==255)) ; // if any of the octets is 255, then it is invalid
_isValidIP &= !(ip[0]==127 && ip[1]==0 && ip[2]==0 && ip[3]==1); // if not 127.0.0.0 then it might be valid
_isValidIP &= !(ip[0]>=224); // if ip[0] >=224 then reserved space
// DebugTf( "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
// if (_isValidIP)
// Debugln(F(" = Valid IP"));
// else
// Debugln(F(" = Invalid IP!"));
return _isValidIP;
} // isValidIP()
//===========================================================================================
bool isNumericp(const char *timeStamp, int8_t len)
{
for (int i=0; (i<len && i<12);i++)
{
if (timeStamp[i] < '0' || timeStamp[i] > '9')
{
return false;
}
}
return true;
} // isNumericp()
//===========================================================================================
int8_t splitString(String inStrng, char delimiter, String wOut[], uint8_t maxWords)
{
int16_t inxS = 0, inxE = 0, wordCount = 0;
inStrng.trim();
while(inxE < inStrng.length() && wordCount < maxWords)
{
inxE = inStrng.indexOf(delimiter, inxS); //finds location of first ,
wOut[wordCount] = inStrng.substring(inxS, inxE); //captures first data String
wOut[wordCount].trim();
//DebugTf("[%d] => [%c] @[%d] found[%s]\r\n", wordCount, delimiter, inxE, wOut[wordCount].c_str());
inxS = inxE;
inxS++;
wordCount++;
}
// zero rest of the words
for(int i=wordCount; i< maxWords; i++)
{
wOut[wordCount][0] = 0;
}
// if not whole string processed place rest in last word
if (inxS < inStrng.length())
{
wOut[maxWords-1] = inStrng.substring(inxS, inStrng.length()); // store rest of String
}
return wordCount;
} // splitString()
uint64_t uptime() {
return esp_log_timestamp()/1000;;
}
String upTime() {
char calcUptime[20];
uint64_t upTimeSeconds = esp_log_timestamp()/1000;
snprintf(calcUptime, sizeof(calcUptime), "%d(d)-%02d:%02d(H:m)"
, int((upTimeSeconds / (60 * 60 * 24)) % 365)
, int((upTimeSeconds / (60 * 60)) % 24)
, int((upTimeSeconds / (60)) % 60));
return calcUptime;
} // upTime()
//===========================================================================================
void strConcat(char *dest, int maxLen, const char *src)
{
if (strlen(dest) + strlen(src) < maxLen)
{
strcat(dest, src);
}
else
{
DebugTf("Combined string > %d chars\r\n", maxLen);
}
} // strConcat()
//===========================================================================================
void strConcat(char *dest, int maxLen, float v, int dec)
{
static char buff[25];
if (dec == 0) sprintf(buff,"%.0f", v);
else if (dec == 1) sprintf(buff,"%.1f", v);
else if (dec == 2) sprintf(buff,"%.2f", v);
else if (dec == 3) sprintf(buff,"%.3f", v);
else if (dec == 4) sprintf(buff,"%.4f", v);
else if (dec == 5) sprintf(buff,"%.5f", v);
else sprintf(buff,"%f", v);
if (strlen(dest) + strlen(buff) < maxLen)
{
strcat(dest, buff);
}
else
{
DebugTf("Combined string > %d chars\r\n", maxLen);
}
} // strConcat()
//===========================================================================================
void strConcat(char *dest, int maxLen, int32_t v)
{
static char buff[25];
sprintf(buff,"%d", v);
if (strlen(dest) + strlen(buff) < maxLen)
{
strcat(dest, buff);
}
else
{
DebugTf("Combined string > %d chars\r\n", maxLen);
}
} // strConcat()
//===========================================================================================
// a 'save' string copy
void strCopy(char *dest, int maxLen, const char *src, uint8_t frm, uint8_t to)
{
int d=0;
//DebugTf("dest[%s], src[%s] max[%d], frm[%d], to[%d] =>\r\n", dest, src, maxLen, frm, to);
dest[0] = '\0';
for (int i=0; i<=frm; i++)
{
if (src[i] == 0) return;
}
for (int i=frm; (src[i] != 0 && i<=to && d<maxLen); i++)
{
dest[d++] = src[i];
}
dest[d] = '\0';
} // strCopy()
//===========================================================================================
// a 'save' version of strncpy() that does not put a '\0' at
// the end of dest if src >= maxLen!
void strCopy(char *dest, int maxLen, const char *src)
{
dest[0] = '\0';
strcat(dest, src);
} // strCopy()
//===========================================================================================
int stricmp(const char *a, const char *b)
{
for (;; a++, b++) {
int d = tolower((unsigned char)*a) - tolower((unsigned char)*b);
if (d != 0 || !*a)
return d;
}
} // stricmp()
//===========================================================================================
float formatFloat(float v, int dec)
{
return (String(v, dec).toFloat());
} // formatFloat()
//===========================================================================================
float strToFloat(const char *s, int dec)
{
float r = 0.0;
int p = 0;
int d = -1;
r = strtof(s, NULL);
p = (int)(r*pow(10, dec));
r = p / pow(10, dec);
//DebugTf("[%s][%d] => p[%d] -> r[%f]\r\n", s, dec, p, r);
return r;
} // strToFloat()
//=======================================================================
template<typename Item>
Item& typecastValue(Item& i)
{
return i;
}
//=======================================================================
float typecastValue(TimestampedFixedValue i)
{
return strToFloat(String(i).c_str(), 3);
}
//=======================================================================
float typecastValue(FixedValue i)
{
return i;
}
#endif
/***************************************************************************
*
* 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.
*
***************************************************************************/