-
Notifications
You must be signed in to change notification settings - Fork 6
/
7_WEB.ino
396 lines (364 loc) · 8.91 KB
/
7_WEB.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
void handleRoot()
{
server.sendHeader(PSTR("Content-Encoding"), "gzip");
server.send_P(200, "text/html", index_htm_gz, index_htm_gz_len);
}
bool loadFromLittlefs(String path)
{
if (path.endsWith("/undefined"))
{
DEBUG_ERROR("SYS", "Web loadFromLittlefs path error: %s", path.c_str());
return false;
}
else if (path.endsWith("/"))
path += "index.htm";
String contentType;
if (server.hasArg("download"))
contentType = F("application/octet-stream");
else
contentType = getContentType(path); // FSBrowser
if (LittleFS.exists(path.c_str()))
{
File dataFile = LittleFS.open(path.c_str(), "r");
if (dataFile)
{
int32_t fsize = dataFile.size();
server.sendHeader("Content-Length", (String)fsize);
size_t sent = server.streamFile(dataFile, contentType);
dataFile.close();
return true;
}
else
return false;
}
return false;
}
void mqttcallback(char *topic, unsigned char *payload, unsigned int length)
{
// Uncomment for debug output received MQTT payloads
// log_e("Web: Received MQTT Topic with char payload: %s\n", topic);
// Serial.print("Web: Payload: ");
// for (unsigned int i = 0; i < length; i++)
// {
// Serial.print((char)payload[i]);
// }
// Serial.println(" ");
// char payload_msg[length];
if (inductionCooker.getTopic() == topic)
{
inductionCooker.handlemqtt(payload, length);
return;
}
if (numberOfActors > 0)
{
for (uint8_t i = 0; i < numberOfActors; i++)
{
if (actors[i].getActorTopic() == topic)
{
actors[i].handlemqtt(payload);
return;
}
}
}
if (useDisplay)
{
char *p;
const char *kettleupdate = "cbpi/kettleupdate/";
const char *stepupdate = "cbpi/stepupdate/";
const char *sensorupdate = "cbpi/sensordata/";
const char *notificationupdate = "cbpi/notification";
const char *fermenterupdate = "cbpi/fermenterupdate/";
const char *fermenterstepupdate = "cbpi/fermenterstepupdate/";
p = strstr(topic, kettleupdate);
if (p)
{
cbpi4kettle_handlemqtt(payload);
return;
}
p = strstr(topic, stepupdate);
if (p)
{
cbpi4steps_handlemqtt(payload);
return;
}
p = strstr(topic, notificationupdate);
if (p)
{
cbpi4notification_handlemqtt(payload);
return;
}
p = strstr(topic, sensorupdate);
if (p)
{
cbpi4sensor_handlemqtt(payload);
return;
}
p = strstr(topic, fermenterupdate);
if (p)
{
cbpi4fermenter_handlemqtt(payload);
return;
}
p = strstr(topic, fermenterstepupdate);
if (p)
{
cbpi4fermentersteps_handlemqtt(payload);
return;
}
}
else if (mqttBuzzer)
{
char *p;
const char *notificationupdate = "cbpi/notification";
p = strstr(topic, notificationupdate);
if (p)
{
cbpi4notification_handlemqtt(payload);
return;
}
}
}
void handleRequestMisc2()
{
JsonDocument doc;
doc["host"] = mqtthost;
doc["port"] = mqttport;
doc["s_mqtt"] = mqtt_state;
doc["display"] = useDisplay;
if (startMDNS)
doc["mdns"] = nameMDNS;
else
doc["mdns"] = 0;
char response[measureJson(doc) + 2];
serializeJson(doc, response, sizeof(response));
replyResponse(response);
}
void handleRequestMiscAlert()
{
if (alertState)
{
replyResponse("1");
alertState = false;
}
else
replyResponse("0");
}
void handleRequestMisc()
{
JsonDocument doc;
doc["host"] = mqtthost;
doc["port"] = mqttport;
doc["user"] = mqttuser;
doc["pass"] = mqttpass;
doc["mdns_name"] = nameMDNS;
doc["mdns"] = startMDNS;
doc["mqbuz"] = mqttBuzzer;
doc["res"] = senRes;
doc["display"] = useDisplay;
doc["ferm"] = useFerm;
doc["page"] = startPage;
doc["dev"] = devBranch;
doc["e_mqtt"] = StopOnMQTTError;
doc["d_mqtt"] = wait_on_error_mqtt / 1000;
doc["dsa"] = wait_on_Sensor_error_actor / 1000;
doc["dsi"] = wait_on_Sensor_error_induction / 1000;
doc["s_mqtt"] = mqtt_state; // Anzeige MQTT Status -> mqtt_state verzögerter Status!
doc["spi"] = startSPI;
doc["ntp"] = ntpServer;
doc["zone"] = ntpZone;
doc["duty"] = DUTYCYLCE;
doc["sen"] = SENCYLCE;
doc["logCFG"] = getTagLevel("CFG");
doc["logSen"] = getTagLevel("SEN");
doc["logAct"] = getTagLevel("ACT");
doc["logInd"] = getTagLevel("IND");
doc["logSys"] = getTagLevel("SYS");
if (getTagLevel("DIS") > 0)
doc["logDis"] = 1;
else
doc["logDis"] = 0;
String message = "";
if (isPin(PIN_BUZZER))
{
message += OPTIONSTART;
message += PinToString(PIN_BUZZER);
message += OPTIONDISABLED;
}
else
{
message += OPTIONSTART;
message += "-";
message += OPTIONDISABLED;
}
for (uint8_t i = 0; i < NUMBEROFPINS; i++)
{
if (pins_used[pins[i]] == false)
{
message += OPTIONSTART;
message += pin_names[i];
message += OPTIONEND;
}
}
doc["buz"] = message;
char response[measureJson(doc) + 2];
serializeJson(doc, response, sizeof(response));
replyResponse(response);
}
void handleSetMiscLang()
{
JsonDocument doc;
DeserializationError error = deserializeJson(doc, server.arg(0));
if (error)
{
DEBUG_ERROR("SYS", "error deserializeJson %s", error.c_str());
replyServerError("Server error set language");
return;
}
selLang = doc["lang"];
saveConfig();
replyOK();
}
void handleRequestFirm()
{
String request = server.arg(0);
String message;
#ifdef ESP32
message = F("MQTTDevice32 V ");
#elif ESP8266
message = F("MQTTDevice V ");
#endif
message += Version;
if (devBranch == 1)
message += F(" dev");
replyResponse(message.c_str());
}
void handleGetTitle()
{
#ifdef ESP32
replyResponse("MQTTDevice32 ");
#elif ESP8266
replyResponse("MQTTDevice4 ");
#endif
}
void handleReqSys()
{
JsonDocument doc;
String message;
#ifdef ESP32
message = F("MQTTDevice32 V");
doc["title"] = "MQTTDevice32";
#elif ESP8266
message = F("MQTTDevice4 V");
doc["title"] = "MQTTDevice4";
#endif
message += Version;
if (devBranch == 1)
message += F(" dev");
doc["firm"] = message;
char response[measureJson(doc) + 2];
serializeJson(doc, response, sizeof(response));
replyResponse(response);
}
void handleSetMisc()
{
JsonDocument doc;
DeserializationError error = deserializeJson(doc, server.arg(0));
if (error)
{
DEBUG_ERROR("SYS", "error deserializeJson %s", error.c_str());
replyServerError("Server error deserialize misc settings");
return;
}
if (doc["reset"] && doc["clear"])
{
LittleFS.remove(CONFIG);
WiFi.disconnect();
wifiManager.resetSettings();
delay(PAUSE1SEC);
EM_REBOOT();
}
if (doc["reset"])
{
WiFi.disconnect();
wifiManager.resetSettings();
delay(PAUSE1SEC);
ESP.restart();
}
if (doc["clear"])
{
LittleFS.remove(CONFIG);
EM_REBOOT();
}
PIN_BUZZER = StringToPin(doc["buz"]);
pins_used[PIN_BUZZER] = true;
mqttport = doc["mqttport"];
strlcpy(mqtthost, doc["mqtthost"] | "", maxHostSign);
strlcpy(mqttuser, doc["mqttuser"] | "", maxUserSign);
strlcpy(mqttpass, doc["mqttpass"] | "", maxPassSign);
mqttBuzzer = doc["mqbuz"];
senRes = doc["res"];
useDisplay = doc["display"];
startPage = doc["page"];
useFerm = doc["ferm"];
devBranch = doc["dev"];
strlcpy(nameMDNS, doc["mdns_name"] | "", maxHostSign);
startMDNS = doc["mdns"];
StopOnMQTTError = doc["e_mqtt"];
wait_on_error_mqtt = doc["d_mqtt"].as<int>() * 1000;
wait_on_Sensor_error_actor = doc["dsa"].as<int>() * 1000;
wait_on_Sensor_error_induction = doc["dsi"].as<int>() * 1000;
strlcpy(ntpServer, doc["ntp"] | NTP_ADDRESS, maxNTPSign);
strlcpy(ntpZone, doc["zone"] | NTP_ZONE, maxNTPSign);
startSPI = doc["spi"];
DUTYCYLCE = doc["duty"];
SENCYLCE = doc["sen"];
setTagLevel("CFG", doc["logCFG"]);
setTagLevel("SEN", doc["logSen"]);
setTagLevel("ACT", doc["logAct"]);
setTagLevel("IND", doc["logInd"]);
setTagLevel("SYS", doc["logSys"]);
if (doc["logDis"] > 0)
{
setTagLevel("DIS", 3);
nextion.setDebug(true);
}
else
{
setTagLevel("DIS", 0);
nextion.setDebug(false);
}
saveConfig();
replyOK();
miscSSE();
}
void handleRestore()
{
HTTPUpload &upload = server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = "config.txt"; // upload.filename;
if (!filename.startsWith("/"))
filename = "/" + filename;
fsUploadFile = LittleFS.open(filename, "w"); // Open the file for writing in LittleFS (create if it doesn't exist)
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
}
else if (upload.status == UPLOAD_FILE_END)
{
if (fsUploadFile)
{ // If the file was successfully created
fsUploadFile.close(); // Close the file again
EM_REBOOT();
}
}
}
void handleGetLanguage()
{
char response[3];
sprintf_P(response, PSTR("%d"), selLang);
replyResponse(response);
}