-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup_OTA_WBServer.ino
514 lines (477 loc) · 19.9 KB
/
setup_OTA_WBServer.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
#include "constants.h"
#include "preferences.h"
#include "ntp.h"
void setup_OTA_WBServer(){
// Init the OTA + WebServer
// Set Hostname.
String hostNAME(HOSTNAME);
hostNAME += String(ESP.getChipId(), HEX);
SERIAL_OUT.print("set OTA+WiFi hostname: "); SERIAL_OUT.println(hostNAME);
WiFi.hostname(hostNAME);
ArduinoOTA.onStart([]() { events.send("Update Start", "ota"); });
ArduinoOTA.onEnd([]() { events.send("Update End", "ota"); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
char p[32];
sprintf(p, "Progress: %u%%\n", (progress/(total/100)));
events.send(p, "ota");
});
ArduinoOTA.onError([](ota_error_t error) {
if(error == OTA_AUTH_ERROR) events.send("Auth Failed", "ota");
else if(error == OTA_BEGIN_ERROR) events.send("Begin Failed", "ota");
else if(error == OTA_CONNECT_ERROR) events.send("Connect Failed", "ota");
else if(error == OTA_RECEIVE_ERROR) events.send("Recieve Failed", "ota");
else if(error == OTA_END_ERROR) events.send("End Failed", "ota");
});
ArduinoOTA.setHostname((const char *)hostNAME.c_str());
ArduinoOTA.begin();
MDNS.addService("http","tcp",80);
ws.onEvent(onWsEvent);
server.addHandler(&ws);
events.onConnect([](AsyncEventSourceClient *clientSSI){
clientSSI->send("hello!",NULL,millis(),1000);
});
server.addHandler(&events);
server.addHandler(new SPIFFSEditor(http_username,http_password));
server.on("/", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", "text/html");
request->send(response);
}
});
server.on("/index.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", "text/html");
request->send(response);
}
});
server.on("/programlist.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/programlist.htm", "text/html");
request->send(response);
}
});
server.on("/programinput.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/programinput.htm", "text/html");
request->send(response);
}
});
server.on("/programmod.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/programmod.htm", "text/html");
request->send(response);
}
});
server.on("/setting.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/setting.htm", "text/html");
request->send(response);
}
});
server.on("/graph.htm", [](AsyncWebServerRequest *request)
{
if (!request->authenticate(http_username,http_password))
{
Serial.printf("Autentication required \n");
return request->requestAuthentication();//request->redirect("/login");
}
else{
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/graph.htm", "text/html");
request->send(response);
}
});
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.printf("GET /heap");
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server.on("/valvola1ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele1status_WBS=1;
mInput(SLOT_VALVOLA_1) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola1ON");
request->redirect("/index.htm");
});
server.on("/valvola1OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele1status_WBS=0;
mInput(SLOT_VALVOLA_1) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola1OFF");
request->redirect("/index.htm");
});
server.on("/valvola2ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele2status_WBS=1;
mInput(SLOT_VALVOLA_2) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola2ON");
request->redirect("/index.htm");
});
server.on("/valvola2OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele2status_WBS=0;
mInput(SLOT_VALVOLA_2) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola2OFF");
request->redirect("/index.htm");
});
server.on("/valvola3ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele3status_WBS=1;
mInput(SLOT_VALVOLA_3) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola3ON");
request->redirect("/index.htm");
});
server.on("/valvola3OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele3status_WBS=0;
mInput(SLOT_VALVOLA_3) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola3OFF");
request->redirect("/index.htm");
});
server.on("/valvola4ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele4status_WBS=1;
mInput(SLOT_VALVOLA_4) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola4ON");
request->redirect("/index.htm");
});
server.on("/valvola4OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele4status_WBS=0;
mInput(SLOT_VALVOLA_4) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola4OFF");
request->redirect("/index.htm");
});
server.on("/valvola5ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele5status_WBS=1;
mInput(SLOT_VALVOLA_5) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola5ON");
request->redirect("/index.htm");
});
server.on("/valvola5OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele5status_WBS=0;
mInput(SLOT_VALVOLA_5) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola5OFF");
request->redirect("/index.htm");
});
server.on("/valvola6ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele6status_WBS=1;
mInput(SLOT_VALVOLA_6) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola6ON");
request->redirect("/index.htm");
});
server.on("/valvola6OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele6status_WBS=0;
mInput(SLOT_VALVOLA_6) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola6OFF");
request->redirect("/index.htm");
});
server.on("/valvola7ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele7status_WBS=1;
mInput(SLOT_VALVOLA_7) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola7ON");
request->redirect("/index.htm");
});
server.on("/valvola7OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele7status_WBS=0;
mInput(SLOT_VALVOLA_7) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola7OFF");
request->redirect("/index.htm");
});
server.on("/valvola8ON", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele8status_WBS=1;
mInput(SLOT_VALVOLA_8) = Souliss_T1n_OnCmd; //accende il relè
Serial.printf("\nGET /valvola8ON");
request->redirect("/index.htm");
});
server.on("/valvola8OFF", HTTP_GET, [](AsyncWebServerRequest *request){
S_rele8status_WBS=0;
mInput(SLOT_VALVOLA_8) = Souliss_T1n_OffCmd; //accende il relè
Serial.printf("\nGET /valvola8OFF");
request->redirect("/index.htm");
});
server.on("/all", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.printf("\nGET /all");
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += ", \"S_setpoint_WBS\":"+S_setpoint_WBS;
json += ", \"S_humidity_WBS\":"+S_humidity_WBS;
json += ", \"S_rele1status_WBS\":" +String(S_rele1status_WBS);
json += ", \"S_rele2status_WBS\":" +String(S_rele2status_WBS);
json += ", \"S_rele3status_WBS\":" +String(S_rele3status_WBS);
json += ", \"S_rele4status_WBS\":" +String(S_rele4status_WBS);
json += ", \"S_rele5status_WBS\":" +String(S_rele5status_WBS);
json += ", \"S_rele6status_WBS\":" +String(S_rele6status_WBS);
json += ", \"S_rele7status_WBS\":" +String(S_rele7status_WBS);
json += ", \"S_rele8status_WBS\":" +String(S_rele8status_WBS);
json += ", \"S_sensoreattivo_WBS\":" +String(S_sensoreattivo_WBS);
json += ", \"S_numerovalvole_WBS\":" +String(S_numerovalvole_WBS);
json += ", \"S_nomeprogramma_WBS\":" +String(S_nomeprogramma_WBS);
json += ", \"S_version_WBS\":" +String(S_version_WBS);
json += "}";
Serial.printf("Json: \n");
request->send(200, "application/json", json);
});
server.on("/getFileList", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.printf("GET /getFileList");
Serial.println("Ecco la lista dei programmi:");
int i=0;
String ListaProgrammi = "";
Dir dir = SPIFFS.openDir("/programs/");
ListaProgrammi += "[";
int primo = 0;
while (dir.next()) {
if (primo==0) {
ListaProgrammi += "\"";
}
else
{
ListaProgrammi += ",\"";
}
ListaProgrammi += dir.fileName();
ListaProgrammi += "\"";
primo = 1;
}
ListaProgrammi += "]";
Serial.println(ListaProgrammi);
// open file for writing
File ssi_spiffs = SPIFFS.open("/listaprogrammi.json", "w");
if (!ssi_spiffs) {
Serial.println("listaprogrammi.json open failed");
}
//qui salvo il buffer su file
ssi_spiffs.println(ListaProgrammi);
Serial.print("Salvo in SPIFFS il buffer con i programmi :"); Serial.println(ListaProgrammi);
delay(1);
//chiudo il file
ssi_spiffs.close();
request->send(200, "text/html",ListaProgrammi);
});
server.on("/DeleteProgram", HTTP_GET, [](
AsyncWebServerRequest *request){
Serial.println("GET /DeleteProgram");
Serial.println("Cancello il programma");
//cancello il file
//Check if GET parameter exists
int params = request->params();
if(request->hasParam("file")) {
AsyncWebParameter* p = request->getParam("file");
//const char * nomefile = (char*)p->value().c_str(); //QUESTO E' IL PUNTO DOVE HAI LASCIATO AGOSTINO
String nomefile = p->value();
Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
if (SPIFFS.exists(nomefile) == true) {
bool ssi_spiffs = SPIFFS.remove(nomefile);
if (!ssi_spiffs) {
Serial.print("file '");
Serial.print(nomefile);
Serial.println("' non trovato!");
request->send(404, "text/html","Non trovo il programma da cancellare");
}
else
{
Serial.print("file '");
Serial.print(nomefile);
Serial.println("' cancellato!");
request->send(200, "text/html","Ok programma cancellato!");
}
delay(1);
} else {
Serial.print("file '");
Serial.println("' non trovato!");
request->send(404, "text/html","Non trovo il programma da cancellare");
//!?request->redirect("/programlist.htm");
}
} else {
Serial.print("file '");
//Serial.print(nomefile);
Serial.println("' errore!");
request->send(500, "text/html","Errore");
}
});
server.serveStatic("/", SPIFFS, "/"); //.setDefaultFile("index.htm");
server.onNotFound([](AsyncWebServerRequest *request){
Serial.printf("NOT_FOUND: ");
if(request->method() == HTTP_GET)
Serial.printf("GET");
else if(request->method() == HTTP_POST)
Serial.printf(" POST http://%s%s\n", "Host",request->host().c_str());
//Serial.printf("POST");
else if(request->method() == HTTP_DELETE)
Serial.printf("DELETE");
else if(request->method() == HTTP_PUT)
Serial.printf("PUT");
else if(request->method() == HTTP_PATCH)
Serial.printf("PATCH");
else if(request->method() == HTTP_HEAD)
Serial.printf("HEAD");
else if(request->method() == HTTP_OPTIONS)
Serial.printf("OPTIONS");
else
Serial.printf("UNKNOWN");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
if(request->contentLength()){
Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
}
int headers = request->headers();
int i;
for(i=0;i<headers;i++){
AsyncWebHeader* h = request->getHeader(i);
Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
}
int params = request->params();
for(i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isFile()){
Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
} else if(p->isPost()){
Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else {
Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(404);
});
server.onFileUpload([](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
if(!index)
Serial.printf("UploadStart: %s\n", filename.c_str());
Serial.printf("%s", (const char*)data);
if(final)
Serial.printf("UploadEnd: %s (%u)\n", filename.c_str(), index+len);
});
server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
AsyncWebHeader* h = request->getHeader("Referer");
Serial.printf("MyHeader: %s\n", h->value().c_str());
String cerca=h->value().c_str();
if (cerca.indexOf("setting")==-1)
{
Serial.printf("Setting non trovato: %s\n", h->value().c_str());
}
else{
Serial.printf("Setting trovato: %s\n", h->value().c_str());
if(!index)
Serial.printf("Request : ",request);
Serial.printf("BodyStart: %u\n", index);
Serial.printf("scrivo file: ssi_setting.json");
Serial.printf("%s", (const char*)data);
String S_filena_WBS = "/ssi_settings.json";
fsUploadFile = SPIFFS.open(S_filena_WBS, "w");
if (!fsUploadFile)
Serial.println("file open failed");
fsUploadFile.printf("%s",(const char*)data);
if(index + len == total)
//Serial.printf("BodyEnd: %u\n", total);
fsUploadFile.close();
delay (1000);
request->redirect("/salvatosetting.htm");
delay (1000);
ReadAllSettingsFromSPIFFS();
}
if (cerca.indexOf("programmod")==-1)
{
Serial.printf("Modifica Programma non trovata: %s\n", h->value().c_str());
}
else{
Serial.printf("Modifica trovata: %s\n", h->value().c_str());
if(!index)
Serial.printf("Request : ",request);
Serial.printf("BodyStart: %u\n", index);
Serial.printf("scrivo file: /programs/ssi_program1.json");
Serial.printf("%s", (const char*)data);
String S_filena_WBS = "/programs/ssi_program1.json";
fsUploadFile = SPIFFS.open(S_filena_WBS, "w");
if (!fsUploadFile)
Serial.println("file open failed");
fsUploadFile.printf("%s",(const char*)data);
if(index + len == total)
//Serial.printf("BodyEnd: %u\n", total);
fsUploadFile.close();
delay (1000);
request->redirect("/salvatoprogramma.htm");
delay (1000);
// qui sicuramente andrà la funzione per ricaricare i programmi dopo averli modificati ed attivarli....
//ReadAllSettingsFromSPIFFS();
}
if (cerca.indexOf("programinput")==-1)
{
Serial.printf("Inserimento Programma non trovato: %s\n", h->value().c_str());
}
else{
Serial.printf("Inserimento Programma trovato: %s\n", h->value().c_str());
#define FILEPREFIX_LEN strlen("/programs/ssi_program")
//Serial.println("Ecco i file:");
String str = "";
Dir dir = SPIFFS.openDir("/programs/");
//funzione per trovare prossimo numero per filename del programma
int max_num = 0;
while (dir.next()) {
//Serial.println(dir.fileName());
int extpos = dir.fileName().indexOf(".json"); // indice della sottostring ".json" nel nome del file
if(extpos > -1 ){ // solo se l'hai trovata
int num = dir.fileName().substring(FILEPREFIX_LEN, extpos).toInt(); // estrai la stringa che rappresenta il numero e convertila a intero
if(num > max_num){ max_num = num; }
}
}
//Serial.print("Max is: ");Serial.println(max_num);
if(!index)
Serial.printf("Request : ",request);
Serial.printf("BodyStart: %u\n", index);
Serial.printf("%s", (const char*)data);
String S_filena_WBS = "/programs/ssi_program" + String(++max_num) + ".json";
//Serial.print("Prossimo file da salvare: ");Serial.println(S_filena_WBS);
fsUploadFile = SPIFFS.open(S_filena_WBS, "w");
if (!fsUploadFile)
Serial.println("file open failed");
fsUploadFile.printf("%s",(const char*)data);
if(index + len == total)
//Serial.printf("BodyEnd: %u\n", total);
fsUploadFile.close();
delay (1000);
request->redirect("/salvatoprogramma.htm");
delay (1000);
}
});
//Client
///////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef TTD
Serial.println("SEND BOOTUP");
HTTPClient clienthttp_SSI;
const char* host="http://www.google-analytics.com/collect";
String eventData = "v=1&t=event&tid=UA-89261240-1&cid=555&ec=SSI"+String(VERSION)+"&ea=BOOTUP&el="+String(ESP.getChipId(),HEX);
clienthttp_SSI.begin(host);
clienthttp_SSI.addHeader("User-agent", "Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0");
clienthttp_SSI.POST(eventData);
clienthttp_SSI.writeToStream(&Serial);
clienthttp_SSI.end();
Serial.println("BOOTUP CLOSED");
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////
server.begin();
///////////////////////////////////////////////////////////////////////////////////////////////////////
}