forked from InnuendoPi/MQTTDevice4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path990_tickerCallback.ino
298 lines (278 loc) · 8.53 KB
/
990_tickerCallback.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
void pageCallback0()
{
if (startBuzzer)
sendAlarm(ALARM_INFO);
tempPage = 0;
TickerDisp.updatenow();
}
void pageCallback1()
{
if (startBuzzer)
sendAlarm(ALARM_INFO);
tempPage = 1;
TickerDisp.updatenow();
}
void pageCallback2()
{
if (startBuzzer)
sendAlarm(ALARM_INFO);
tempPage = 2;
TickerDisp.updatenow();
}
void powerButtonCallback()
{
inductionCooker.setInductionState(!inductionCooker.getInductionState());
}
void tickerDispCallback()
{
if (tempPage < 0)
activePage = nextion.getCurrentPageID();
else
{
activePage = tempPage;
tempPage = -1;
}
char ipMQTT[50];
sprintf_P(uhrzeit, (PGM_P)F("%02d:%02d"), timeClient.getHours(), timeClient.getMinutes());
if (startMDNS)
sprintf_P(ipMQTT, (PGM_P)F("http://%s.local"), nameMDNS);
else
sprintf_P(ipMQTT, (PGM_P)F("http://%s"), WiFi.localIP().toString().c_str());
activePage = nextion.getCurrentPageID();
switch (activePage)
{
case 0: // BrewPage
if (!activeBrew) // aktiver Step vorhanden?
strlcpy(currentStepName, "BrewPage", maxStepSign);
uhrzeit_text.attribute("txt", uhrzeit);
mqttDevice.attribute("txt", ipMQTT);
BrewPage();
break;
case 1: // KettlePage
if (!activeBrew) // aktiver Step vorhanden?
strlcpy(currentStepName, sensors[0].getSensorName().c_str(), maxStepSign);
strlcpy(structKettles[0].current_temp, sensors[0].getTotalValueString(), maxTempSign);
p1mqttDevice.attribute("txt", ipMQTT);
p1uhrzeit_text.attribute("txt", uhrzeit);
KettlePage();
break;
case 2: // Induction mode
// log_e("Ticker: dispCallback InductionPage activePage: %d", activePage);
strlcpy(structKettles[0].current_temp, sensors[0].getTotalValueString(), maxTempSign);
p2uhrzeit_text.attribute("txt", uhrzeit);
InductionPage();
break;
}
}
void tickerPUBSUBCallback() // Timer Objekt Sensoren
{
if (pubsubClient.connected())
{
mqtt_state = true;
pubsubClient.loop();
if (TickerMQTT.state() == RUNNING)
TickerMQTT.stop();
return;
}
else
{
if (TickerMQTT.state() != RUNNING)
{
#ifdef ESP32
log_e("%s", "Ticker PubSub Error: TickerMQTT started");
#endif
TickerMQTT.start();
mqttconnectlasttry = millis();
mqtt_state = false; // MQTT in error state
miscSSE();
}
TickerMQTT.update();
}
}
void tickerSenCallback() // Timer Objekt Sensoren
{
DS18B20.requestTemperatures();
switch (sensorsStatus)
{
case EM_OK:
// all sensors ok
lastSenInd = 0; // Delete induction timestamp after event
lastSenAct = 0; // Delete actor timestamp after event
// if (WiFi.status() == WL_CONNECTED && mqtt_state)
if (WiFi.status() == WL_CONNECTED && TickerPUBSUB.state() == RUNNING && mqtt_state)
{
for (int i = 0; i < numberOfActors; i++)
{
if (actors[i].getActorSwitch() && !actors[i].getActorState()) // Sensor in normal mode: check actor in error state
{
#ifdef ESP32
log_e("EM SenOK: %s isOnBeforeError: %d power level: %d", actors[i].getActorName().c_str(), actors[i].getIsOnBeforeError(), actors[i].getActorPower());
#endif
actors[i].setIsOn(actors[i].getIsOnBeforeError());
actors[i].setActorState(true);
actors[i].Update();
lastSenAct = 0; // Delete actor timestamp after event
}
yield();
}
if (!inductionCooker.getInductionState())
{
#ifdef ESP32
log_e("EM SenOK: Induction power: %d powerLevelOnError: %d powerLevelBeforeError: %d", inductionCooker.getPower(), inductionCooker.getPowerLevelOnError(), inductionCooker.getPowerLevelBeforeError());
#endif
if (!inductionCooker.getInductionState())
{
inductionCooker.setNewPower(inductionCooker.getPowerLevelBeforeError());
inductionCooker.setisInduon(true);
inductionCooker.setInductionState(true);
inductionCooker.Update();
lastSenInd = 0; // Delete induction timestamp after event
}
}
}
break;
case EM_CRCER:
// Sensor CRC ceck failed
case EM_DEVER:
// -127°C device error
case EM_UNPL:
// sensor unpluged
case EM_SENER:
// all other errors
// if (WiFi.status() == WL_CONNECTED && mqtt_state)
if (WiFi.status() == WL_CONNECTED && TickerPUBSUB.state() == RUNNING && mqtt_state)
{
for (int i = 0; i < numberOfSensors; i++)
{
if (!sensors[i].getSensorState())
{
switch (sensorsStatus)
{
case EM_CRCER:
// Sensor CRC ceck failed
#ifdef ESP32
log_e("EM CRCER: Sensor %s crc check failed", sensors[i].getSensorName().c_str());
#endif
break;
case EM_DEVER:
// -127°C device error
#ifdef ESP32
log_e("EM DEVER: Sensor %s device error", sensors[i].getSensorName().c_str());
#endif
break;
case EM_UNPL:
// sensor unpluged
#ifdef ESP32
log_e("EM UNPL: Sensor %s unplugged", sensors[i].getSensorName().c_str());
#endif
break;
default:
break;
}
}
if (sensors[i].getSensorSwitch() && !sensors[i].getSensorState())
{
if (lastSenAct == 0)
{
lastSenAct = millis(); // Timestamp on error
#ifdef ESP32
log_e("EM SENER: timestamp actors due to sensor error: %l Wait on error actors: %d", lastSenAct, wait_on_Sensor_error_actor / 1000);
#endif
}
if (lastSenInd == 0)
{
lastSenInd = millis(); // Timestamp on error
#ifdef ESP32
log_e("EM SENER: timestamp induction due to sensor error: %l Wait on error induction: %d", lastSenInd, wait_on_Sensor_error_induction / 1000);
#endif
}
if (millis() - lastSenAct >= wait_on_Sensor_error_actor) // Wait bevor Event handling
{
actERR();
}
if (millis() - lastSenInd >= wait_on_Sensor_error_induction) // Wait bevor Event handling
{
if (inductionCooker.getisInduon() && inductionCooker.getPowerLevelOnError() < 100 && inductionCooker.getInductionState())
{
inductionCooker.indERR();
}
}
} // Switchable
yield();
} // Iterate sensors
} // wlan und mqtt state
break;
default:
break;
}
handleSensors(false);
}
void tickerActCallback() // Timer Objekt Sensoren
{
handleActors(false);
}
void tickerIndCallback() // Timer Objekt Sensoren
{
handleInduction();
inductionSSE(false);
}
void tickerMQTTCallback() // Ticker helper function calling Event MQTT Error
{
if (TickerMQTT.counter() == 1)
{
switch (pubsubClient.state())
{
case -4: // MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECTION_TIMEOUT", pubsubClient.state());
#endif
break;
case -3: // MQTT_CONNECTION_LOST - the network connection was broken
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECTION_LOST", pubsubClient.state());
#endif
break;
case -2: // MQTT_CONNECT_FAILED - the network connection failed
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_FAILED", pubsubClient.state());
#endif
break;
case -1: // MQTT_DISCONNECTED - the client is disconnected cleanly
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_DISCONNECTED", pubsubClient.state());
#endif
break;
case 0: // MQTT_CONNECTED - the client is connected
pubsubClient.loop();
break;
case 1: // MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_BAD_PROTOCOL", pubsubClient.state());
#endif
break;
case 2: // MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_BAD_CLIENT_ID", pubsubClient.state());
#endif
break;
case 3: // MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_UNAVAILABLE", pubsubClient.state());
#endif
break;
case 4: // MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_BAD_CREDENTIALS", pubsubClient.state());
#endif
break;
case 5: // MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
#ifdef ESP32
log_e("MQTT status: error rc=%d MQTT_CONNECT_UNAUTHORIZED", pubsubClient.state());
#endif
break;
default:
break;
}
}
EM_MQTTERROR();
}