-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path8_SYSTEM.ino
523 lines (497 loc) · 13.5 KB
/
8_SYSTEM.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
void millis2wait(const int &value)
{
unsigned long pause = millis();
while (millis() < pause + value)
{
yield(); // wait approx. [period] milliseconds
}
}
void micros2wait(const int &value)
{
unsigned long pause = micros();
while (micros() < pause + value)
{
yield(); // wait approx. [period] microsseconds
}
}
void setTicker()
{
TickerSen.config(tickerSenCallback, (SENCYLCE * SEN_UPDATE), 0);
TickerAct.config(tickerActCallback, ACT_UPDATE, 0);
TickerInd.config(tickerIndCallback, IND_UPDATE, 0);
TickerPUBSUB.config(tickerPUBSUBCallback, tickerPUSUB, 0);
TickerMQTT.config(tickerMQTTCallback, tickerMQTT, 0);
TickerDisp.config(tickerDispCallback, DISP_UPDATE, 0);
TickerTime.config(tickerTimeCallback, TIME_UPDATE, 0);
TickerMQTT.stop();
}
void setupTime()
{
// #ifdef ESP32
// configTzTime(ntpZone, ntpServer);
// #elif ESP8266
// configTime(ntpZone, ntpServer);
// #endif
configTzTime(ntpZone, ntpServer);
getLocalTime(&timeinfo);
// 1 Minute vor Winterzeit 27.10.2024 2:59:00 1729994340
// 1 Minute vor Sommerzeit 31.03.2024 1:59:00 1711846740
// timeval epoch = {1711846740, 0};
// settimeofday((const timeval*)&epoch, 0);
strftime(zeit, sizeof(zeit), "%H:%M:%S", &timeinfo);
TickerTime.start();
// TickerTime.updatenow();
}
// Sensor Adresse String to Hex
uint8_t charToHex(char c)
{
if ((c >= '0') && (c <= '9'))
return c - '0';
if ((c >= 'A') && (c <= 'F'))
return 10 + c - 'A';
if ((c >= 'a') && (c <= 'f'))
return 10 + c - 'a';
return 255;
}
bool towCharToHex(char MSB, char LSB, uint8_t *ptrValue)
{
uint8_t uMSB = charToHex(MSB);
uint8_t uLSB = charToHex(LSB);
if (uMSB == 255 || uLSB == 255)
return false;
if (uMSB > 0)
*ptrValue = uMSB * 16 + uLSB;
else
*ptrValue = uLSB;
return true;
}
String IPtoString(IPAddress ip)
{
String res = "";
for (uint8_t i = 0; i < 3; i++)
{
res += String((ip >> (8 * i)) & 0xFF) + ".";
}
res += String(((ip >> 8 * 3)) & 0xFF);
return res;
}
unsigned char convertCharToHex(char ch)
{
unsigned char returnType;
switch (ch)
{
case '0':
returnType = 0;
break;
case '1':
returnType = 1;
break;
case '2':
returnType = 2;
break;
case '3':
returnType = 3;
break;
case '4':
returnType = 4;
break;
case '5':
returnType = 5;
break;
case '6':
returnType = 6;
break;
case '7':
returnType = 7;
break;
case '8':
returnType = 8;
break;
case '9':
returnType = 9;
break;
case 'A':
returnType = 10;
break;
case 'B':
returnType = 11;
break;
case 'C':
returnType = 12;
break;
case 'D':
returnType = 13;
break;
case 'E':
returnType = 14;
break;
case 'F':
returnType = 15;
break;
default:
returnType = 0;
break;
}
return returnType;
}
void sendAlarm(const uint8_t &setAlarm)
{
if (PIN_BUZZER == -100)
return;
switch (setAlarm)
{
case ALARM_ON:
tone(PIN_BUZZER, 440, 50);
millis2wait(150);
tone(PIN_BUZZER, 660, 50);
millis2wait(150);
tone(PIN_BUZZER, 880, 50);
break;
case ALARM_OFF:
tone(PIN_BUZZER, 880, 50);
millis2wait(150);
tone(PIN_BUZZER, 660, 50);
millis2wait(150);
tone(PIN_BUZZER, 440, 50);
break;
case ALARM_INFO:
tone(PIN_BUZZER, 880, 50);
break;
case ALARM_SUCCESS:
tone(PIN_BUZZER, 880, 50);
millis2wait(150);
tone(PIN_BUZZER, 880, 50);
millis2wait(150);
tone(PIN_BUZZER, 880, 50);
break;
case ALARM_WARNING:
tone(PIN_BUZZER, 660, 50);
millis2wait(150);
tone(PIN_BUZZER, 660, 50);
millis2wait(150);
tone(PIN_BUZZER, 660, 50);
millis2wait(150);
break;
case ALARM_ERROR:
tone(PIN_BUZZER, 440, 50);
millis2wait(150);
tone(PIN_BUZZER, 440, 50);
millis2wait(150);
tone(PIN_BUZZER, 440, 50);
millis2wait(150);
break;
default:
break;
}
}
void EM_LOG()
{
if (LittleFS.exists(LOGUPDATESYS)) // WebUpdate Firmware
{
fsUploadFile = LittleFS.open(LOGUPDATESYS, "r");
int anzahlSys = 0;
if (fsUploadFile)
{
anzahlSys = char(fsUploadFile.read()) - '0';
}
fsUploadFile.close();
bool check = LittleFS.remove(LOGUPDATESYS);
if (LittleFS.exists(DEVBRANCH)) // WebUpdate Firmware
{
DEBUG_INFO("SYS", "*** SYSINFO: Firmware Update development Anzahl Versuche: %d", anzahlSys);
check = LittleFS.remove(DEVBRANCH);
}
else
{
DEBUG_INFO("SYS", "*** SYSINFO: Firmware Update Anzahl Versuche: %d", anzahlSys);
}
alertState = true;
}
if (LittleFS.exists(LOGUPDATETOOLS)) // WebUpdate Firmware
{
fsUploadFile = LittleFS.open(LOGUPDATETOOLS, "r");
int anzahlTools = 0;
if (fsUploadFile)
{
anzahlTools = char(fsUploadFile.read()) - '0';
}
fsUploadFile.close();
bool check = LittleFS.remove(LOGUPDATETOOLS);
if (LittleFS.exists(DEVBRANCH)) // WebUpdate Firmware
{
DEBUG_INFO("SYS", "*** SYSINFO: Framework Update development Anzahl der Versuche: %d", anzahlTools);
check = LittleFS.remove(DEVBRANCH);
}
else
{
DEBUG_INFO("SYS", "*** SYSINFO: Framework Update Anzahl der Versuche: %d", anzahlTools);
}
alertState = true;
}
}
void EM_MDNSET() // MDNS setup
{
uint8_t tmpLevel = getTagLevel("SYS");
setTagLevel("SYS", INNU_INFO);
#ifdef ESP32
if (MDNS.begin(nameMDNS))
{
MDNS.addService("http", "tcp", PORT);
DEBUG_INFO("SYS", "*** SYSINFO: mDNS started as %s.local connected to %s Time: %s RSSI: %d", nameMDNS, WiFi.localIP().toString().c_str(), zeit, WiFi.RSSI());
}
#elif ESP8266
if (mdns.begin(nameMDNS))
{
DEBUG_INFO("SYS", "*** SYSINFO: mDNS started as %s.local connected to %s Time: %s RSSI: %d", nameMDNS, WiFi.localIP().toString().c_str(), zeit, WiFi.RSSI());
}
#endif
else
DEBUG_INFO("SYS", "*** SYSINFO: error start mDNS! IP Adresse: %s Time: %s RSSI: %d", WiFi.localIP().toString().c_str(), zeit, WiFi.RSSI());
setTagLevel("SYS", tmpLevel);
}
void EM_REBOOT() // Reboot ESP
{
// Stop actors
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].getIsOn())
{
actors[i].setIsOn(false);
actors[i].Update();
}
}
// Stop induction
if (inductionCooker.getisInduon())
{
inductionCooker.setNewPower(0);
inductionCooker.setisInduon(false);
inductionCooker.Update();
}
server.sendHeader("Location", "/", true);
replyReboot("reboot");
// server.send(205, FPSTR("text/plain"), "reboot");
LittleFS.end(); // unmount LittleFS
ESP.restart();
}
void EM_MQTTCONNECT() // MQTT connect
{
if (WiFi.status() == WL_CONNECTED) // kein wlan = kein mqtt
{
// if (useFerm)
// pubsubClient.setBufferSize(2048); // extra large Buffersize to receive fermenterupdate
// else
// pubsubClient.setBufferSize(512); // large buffersize to receive all CBPi4 MQTT payload
pubsubClient.setBufferSize(2048); // large buffersize to receive all CBPi4 MQTT payload
pubsubClient.setServer(mqtthost, mqttport);
pubsubClient.setCallback(mqttcallback);
pubsubClient.connect(mqtt_clientid, mqttuser, mqttpass);
DEBUG_INFO("SYS", "Connecting MQTT broker %s with client-id: %s user: %s pass: %s", mqtthost, mqtt_clientid, mqttuser, mqttpass);
}
}
void EM_MQTTSUBSCRIBE() // MQTT subscribe
{
if (pubsubClient.connected())
{
DEBUG_VERBOSE("SYS", "MQTT connected! Subscribing...");
mqtt_state = true; // MQTT state ok
for (int i = 0; i < numberOfActors; i++)
{
actors[i].mqtt_subscribe();
yield();
}
if (inductionCooker.getIsEnabled())
inductionCooker.mqtt_subscribe();
if (useDisplay)
{
dispPublishmqtt();
cbpi4kettle_subscribe();
cbpi4steps_subscribe();
cbpi4notification_subscribe();
if (useFerm)
{
cbpi4fermenter_subscribe();
cbpi4fermentersteps_subscribe();
}
}
else if (mqttBuzzer) // mqttBuzzer only
cbpi4notification_subscribe();
TickerMQTT.stop();
}
}
void EM_MQTTRESTORE() // restore saved values after reconnect MQTT
{
if (pubsubClient.connected())
{
mqtt_state = true;
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].getActorSwitch() && !actors[i].getActorState())
{
DEBUG_INFO("SYS", "EM MQTTRES: %s isOnBeforeError: %d Powerlevel: %d", actors[i].getActorName().c_str(), actors[i].getIsOnBeforeError(), actors[i].getActorPower());
actors[i].setIsOn(actors[i].getIsOnBeforeError());
actors[i].setActorState(true);
actors[i].Update();
}
yield();
}
if (!inductionCooker.getInductionState())
{
DEBUG_INFO("SYS", "EM MQTTRES: Induction power: %d powerLevelOnError: %d powerLevelBeforeError: %d", inductionCooker.getPower(), inductionCooker.getPowerLevelOnError(), inductionCooker.getPowerLevelBeforeError());
inductionCooker.setNewPower(inductionCooker.getPowerLevelBeforeError());
inductionCooker.setisInduon(true);
inductionCooker.setInductionState(true);
inductionCooker.Update();
DEBUG_INFO("SYS", "EM MQTTRES: Induction restore old value: %d", inductionCooker.getNewPower());
}
}
}
void EM_MQTTERROR() // MQTT Error -> handling
{
if (pubsubClient.connect(mqtt_clientid, mqttuser, mqttpass))
{
DEBUG_INFO("SYS", "MQTT auto reconnect successful. Subscribing..");
EM_MQTTSUBSCRIBE();
EM_MQTTRESTORE();
miscSSE();
return;
}
if (millis() - mqttconnectlasttry >= wait_on_error_mqtt)
{
if (StopOnMQTTError)
{
mqtt_state = false; // MQTT in error state
if (PIN_BUZZER != -100)
sendAlarm(ALARM_ERROR);
DEBUG_ERROR("SYS", "EM MQTTERROR: MQTT Broker %s not availible! StopOnMQTTError: %d mqtt_state: %d", mqtthost, StopOnMQTTError, mqtt_state);
actERR();
inductionCooker.indERR();
}
}
}
void debugLog(String valFile, String valText)
{
File debLog = LittleFS.open(valFile, "a");
if (debLog)
{
debLog.print(zeit);
debLog.print("\t");
debLog.print(valText);
debLog.print("\n");
debLog.close();
}
}
#ifdef ESP8266
void EM_WIFICONNECT(const WiFiEventStationModeGotIP &event)
{
if (wlanStatus > 0)
{
wifiManager.setConnectTimeout(30); // 30sek Timeout für WIFI_STA Modus. Anschließend AP Mode
wlanStatus = 0;
}
}
#endif
#ifdef ESP8266
void EM_WIFIDISCONNECT(const WiFiEventStationModeDisconnected &event)
{
if (wlanStatus == 0)
wifiManager.setConnectTimeout(300); // 30sek Timeout für WIFI_STA Modus. Anschließend AP Mode
wlanStatus++;
}
#endif
#ifdef ESP32
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
{
switch (event)
{
case ARDUINO_EVENT_WIFI_READY:
DEBUG_VERBOSE("SYS", "WiFi interface ready");
break;
case ARDUINO_EVENT_WIFI_SCAN_DONE:
DEBUG_VERBOSE("SYS", "Completed scan for access points");
break;
case ARDUINO_EVENT_WIFI_STA_START:
DEBUG_VERBOSE("SYS", "WiFi client started");
break;
case ARDUINO_EVENT_WIFI_STA_STOP:
DEBUG_VERBOSE("SYS", "WiFi clients stopped");
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
DEBUG_VERBOSE("SYS", "Connected to access point");
// wifiManager.setConnectTimeout(30);
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
WiFi.reconnect();
break;
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
DEBUG_VERBOSE("SYS", "Authentication mode of access point has changed");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
// wifiManager.setConnectTimeout(30);
DEBUG_VERBOSE("SYS", "WiFiStationGotIP %s", WiFi.localIP().toString().c_str());
break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
// wifiManager.setConnectTimeout(300);
DEBUG_VERBOSE("SYS", "Lost IP address and IP address is reset to 0. Reconnect Timeout: 300s");
WiFi.reconnect();
break;
case ARDUINO_EVENT_WPS_ER_SUCCESS:
DEBUG_VERBOSE("SYS", "WiFi Protected Setup (WPS): succeeded in enrollee mode");
break;
case ARDUINO_EVENT_WPS_ER_FAILED:
DEBUG_VERBOSE("SYS", "WiFi Protected Setup (WPS): failed in enrollee mode");
break;
case ARDUINO_EVENT_WPS_ER_TIMEOUT:
DEBUG_VERBOSE("SYS", "WiFi Protected Setup (WPS): timeout in enrollee mode");
break;
case ARDUINO_EVENT_WPS_ER_PIN:
DEBUG_VERBOSE("SYS", "WiFi Protected Setup (WPS): pin code in enrollee mode");
break;
case ARDUINO_EVENT_WIFI_AP_START:
// wifiManager.setConnectTimeout(300);
DEBUG_VERBOSE("SYS", "WiFi access point started");
break;
case ARDUINO_EVENT_WIFI_AP_STOP:
DEBUG_VERBOSE("SYS", "WiFi access point stopped");
// EM_REBOOT();
break;
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
DEBUG_VERBOSE("SYS", "Client connected");
break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
DEBUG_VERBOSE("SYS", "Client disconnected");
break;
case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
DEBUG_VERBOSE("SYS", "Assigned IP address to client");
break;
case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
DEBUG_VERBOSE("SYS", "Received probe request");
break;
case ARDUINO_EVENT_WIFI_AP_GOT_IP6:
DEBUG_VERBOSE("SYS", "AP IPv6 is preferred");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
DEBUG_VERBOSE("SYS", "STA IPv6 is preferred");
break;
case ARDUINO_EVENT_ETH_GOT_IP6:
DEBUG_VERBOSE("SYS", "Ethernet IPv6 is preferred");
break;
case ARDUINO_EVENT_ETH_START:
DEBUG_VERBOSE("SYS", "Ethernet started");
break;
case ARDUINO_EVENT_ETH_STOP:
DEBUG_VERBOSE("SYS", "Ethernet stopped");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
DEBUG_VERBOSE("SYS", "Ethernet connected");
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
DEBUG_VERBOSE("SYS", "Ethernet disconnected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
DEBUG_VERBOSE("SYS", "Obtained IP address");
break;
default:
break;
}
}
#endif