forked from patience4711/ESP32-read-APS-inverters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASYSERVER.ino
451 lines (403 loc) · 17.6 KB
/
ASYSERVER.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
void start_server() {
if( diagNose != 0 ) consoleOut("starting server");
//server.addHandler(&ws);
server.on("/CONSOLE", HTTP_GET, [](AsyncWebServerRequest *request){
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
request->send_P(200, "text/html", CONSOLE_HTML);
});
server.on("/details", HTTP_GET, [](AsyncWebServerRequest *request) {
iKeuze = atoi(request->arg("inv").c_str()) ;
requestUrl = request->url();
request->send_P(200, "text/html", DETAILSPAGE);
});
// Simple Firmware Update
server.on("/FWUPDATE", HTTP_GET, [](AsyncWebServerRequest *request){
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
requestUrl = "/";
if (!request->authenticate("admin", pswd) ) return request->requestAuthentication();
request->send_P(200, "text/html", otaIndex);
});
server.on("/handleFwupdate", HTTP_POST, [](AsyncWebServerRequest *request){
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
Serial.println("FWUPDATE requested");
if( !Update.hasError() ) {
toSend="<br><br><center><h2>UPDATE SUCCESS !!</h2><br><br>";
toSend +="click here to reboot<br><br><a href='/REBOOT'><input style='font-size:3vw;' type='submit' value='REBOOT'></a>";
} else {
toSend="<br><br><center><kop>update failed<br><br>";
toSend +="click here to go back <a href='/FWUPDATE'>BACK</a></center>";
}
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", toSend);
response->addHeader("Connection", "close");
request->send(response);
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
//Serial.println("filename = " + filename);
if(filename != "") {
if(!index){
//#ifdef DEBUG
Serial.printf("start firmware update: %s\n", filename.c_str());
//#endif
//Update.runAsync(true);
if(!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)){
//#ifdef DEBUG
Update.printError(Serial);
//#endif
}
}
} else {
if( diagNose != 0 ) consoleOut("filename empty, aborting");
// Update.hasError()=true;
}
if(!Update.hasError()){
if(Update.write(data, len) != len){
Serial.println("update failed with error: " );
Update.printError(Serial);
}
}
if(final){
if(Update.end(true)){
Serial.printf("firmware Update Success: %uB\n", index+len);
} else {
Update.printError(Serial);
}
}
});
// ***********************************************************************************
// homepage
// ***********************************************************************************
server.on("/SW=BACK", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "both");
request->redirect( requestUrl );
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "both");
//sendHomepage();
//if( diagNose != 0 ) consoleOut(F("send Homepage"));
request->send_P(200, "text/html", ECU_HOMEPAGE);
});
server.on("/STYLESHEET", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/css", STYLESHEET);
});
server.on("/JAVASCRIPT", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/css", JAVA_SCRIPT);
});
server.on("/INVSCRIPT", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/css", INV_SCRIPT);
});
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request) {
//Serial.println("favicon requested");
AsyncWebServerResponse *response = request->beginResponse_P(200, "image/x-icon", FAVICON, FAVICON_len);
request->send(response);
});
server.on("/MENU", HTTP_GET, [](AsyncWebServerRequest *request) {
//Serial.println("requestUrl = " + request->url() ); // can we use this
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
toSend = FPSTR(HTML_HEAD);
toSend += FPSTR(MENUPAGE);
request->send(200, "text/html", toSend);
});
server.on("/SECURITY", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/css", SECURITY);
});
server.on("/DENIED", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", REQUEST_DENIED);
});
// ***********************************************************************************
// basisconfig
// ***********************************************************************************
server.on("/BASISCONFIG", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
requestUrl = request->url();// remember this to come back after reboot
zendPageBasis();
request->send(200, "text/html", toSend);
});
server.on("/basisconfig", HTTP_GET, [](AsyncWebServerRequest *request) {
handleBasisconfig(request);
//request->send(200, "text/html", toSend);
request->redirect( requestUrl );
});
server.on("/IPCONFIG", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
zendPageIPconfig();
request->send(200, "text/html", toSend);
});
server.on("/IPconfig", HTTP_GET, [](AsyncWebServerRequest *request) {
handleIPconfig(request);
});
server.on("/MQTT", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
requestUrl = request->url();
zendPageMQTTconfig();
request->send(200, "text/html", toSend);
});
server.on("/MQTTconfig", HTTP_GET, [](AsyncWebServerRequest *request) {
handleMQTTconfig(request);
request->redirect( requestUrl );
});
server.on("/GEOCONFIG", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
requestUrl = request->url();
zendPageGEOconfig();
request->send(200, "text/html", toSend);
});
server.on("/geoconfig", HTTP_GET, [](AsyncWebServerRequest *request) {
//DebugPrintln(F("geoconfig requested"));
handleGEOconfig(request);
request->redirect( requestUrl );
});
server.on("/REBOOT", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "admin");
actionFlag = 10;
confirm();
request->send(200, "text/html", toSend);
});
server.on("/STARTAP", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
String toSend = F("<!DOCTYPE html><html><head><script type='text/javascript'>setTimeout(function(){ window.location.href='/SW=BACK'; }, 5000 ); </script>");
toSend += F("</head><body><center><h2>OK the accesspoint is started.</h2>Wait unil the led goes on.<br><br>Then go to the wifi-settings on your pc/phone/tablet and connect to ESP32-ECU");
request->send ( 200, "text/html", toSend ); //zend bevestiging
actionFlag = 11;
});
//server.on("/INFOPAGE", HTTP_GET, [](AsyncWebServerRequest *request) {
////Serial.println(F("/INFOPAGE requested"));
//loginBoth(request, "both");
//handleInfo(request);
//});
server.on("/ABOUT", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println(F("/INFOPAGE requested"));
loginBoth(request, "both");
handleAbout(request);
});
server.on("/TEST", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "admin");
actionFlag = 44;
request->send( 200, "text/html", "<center><br><br><h3>checking zigbee.. please wait a minute.<br>Then you can find the result in the log.<br><br><a href=\'/LOGPAGE\'>click here</a></h3>" );
});
server.on("/LOGPAGE", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "both");
requestUrl = request->url();
handleLogPage(request);
//request->send_P(200, "text/html", LOGPAGE, putList);
});
server.on("/CLEAR_LOG", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
Clear_Log(request);
request->redirect( "/LOGPAGE" ); // refreshes the page
});
server.on("/MQTT_TEST", HTTP_GET, [](AsyncWebServerRequest *request) {
loginBoth(request, "admin");
char Mqtt_send[26] = {0};
strcpy( Mqtt_send , Mqtt_outTopic);
if(Mqtt_send[strlen(Mqtt_send -1)] == '/') {
strcat(Mqtt_send, String(Inv_Prop[0].invIdx).c_str());
}
String toMQTT = "{\"test\":\"" + String(Mqtt_send) + "\"}";
//DebugPrintln("MQTT_Client.publish the message : " + toMQTT);
MQTT_Client.publish ( Mqtt_send, toMQTT.c_str(), true );
toSend = "sent mqtt message : " + toMQTT;
request->send( 200, "text/plain", toSend );
});
// ********************************************************************
// inverters
// ******************************************************************
server.on("/PAIR", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
loginBoth(request, "admin");
requestUrl = request->url();
//DebugPrintln(F("pairing requested"));
handlePair(request);
});
server.on("/INV_DEL", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
handleInverterdel(request);
});
server.on("/inverterconfig", HTTP_GET, [](AsyncWebServerRequest *request) {
//requestUrl = "/";
handleInverterconfig(request);
});
server.on("/INV_CONFIG", HTTP_GET, [](AsyncWebServerRequest *request) {
if(checkRemote( request->client()->remoteIP().toString()) ) request->redirect( "/DENIED" );
iKeuze=0;
inverterForm(); // prepare the page part with the form
request->send_P(200, "text/html", INVCONFIG_START, processor);
});
server.on("/INV", HTTP_GET, [](AsyncWebServerRequest *request) {
requestUrl = "/";
//bool nothing = false;
int i = atoi(request->arg("welke").c_str()) ;
iKeuze = i;
//DebugPrintln("?INV iKeuze at enter = " + String(iKeuze));
if( iKeuze == 99 ) {
iKeuze = inverterCount; //indicate this is an adition
inverterCount += 88;
}
String bestand = "/Inv_Prop" + String(iKeuze) + ".str";
if (!SPIFFS.exists(bestand)) Inv_Prop[iKeuze].invType = 2;
//nu roepen we zendpageRelevant aan
inverterForm(); // prepare the form page
request->send_P(200, "text/html", INVCONFIG_START, processor); //send the html code to the client
});
server.on("/CONFIRM_INV", HTTP_GET, [](AsyncWebServerRequest *request) {
toSend = FPSTR(CONFIRM_INV); // prepare the page
request->send(200, "text/html", toSend); //send the html code to the client
});
// ********************************************************************
// X H T R E Q U E S T S
//***********************************************************************
//server.on("/get.currentTime", HTTP_GET, [](AsyncWebServerRequest *request) {
// //ledblink(1, 100);
// String uur = String(hour());
// if(hour() < 10) { uur = "0" + String(hour()); }
// String minuten = String(minute());
// if(minute() < 10) { minuten = "0" + String(minute()); }
// String json = "{";
// json += "\"uur\":\"" + uur + "\",\"min\":\"" + minuten + "\"}";
// request->send(200, "text/json", json);
// json = String();
//});
//server.on("/get.Times", HTTP_GET, [](AsyncWebServerRequest *request) {
//
// String json = "{";
// // start polling
// String ssuur = "0" + String(hour(switchonTime));
// String ssmin = String(minute(switchonTime));
// if( minute(switchonTime) < 10 ) ssmin = "0" + ssmin;
// ssuur += ":" + ssmin;
// json += "\"srt\":\"" + ssuur + " hr\"";
// // end polling
// ssuur = String(hour(switchoffTime));
// ssmin = String(minute(switchoffTime));
// if( minute(switchoffTime) < 10 ) ssmin = "0" + ssmin;
// ssuur += ":" + ssmin;
// json += ",\"sst\":\"" + ssuur + " hr\"}";
//
// request->send(200, "text/json", json);
// json = String();
//});
server.on("/get.Times", HTTP_GET, [](AsyncWebServerRequest *request) {
//{"srt":"05:37 hr","sst":"21:32 hr"}
char json[40] = {0};
char temp[20]={0};
// start polling
if(minute(switchonTime) < 10 ) {
sprintf(temp,"{\"srt\":\"0%d:0%d hr\"", hour(switchonTime), minute(switchonTime) );
} else {
sprintf(temp,"{\"srt\":\"0%d:%d hr\"", hour(switchonTime), minute(switchonTime) );
}
strcat(json, temp);
if( minute(switchoffTime) < 10 ) {
sprintf(temp,",\"sst\":\"%d:0%d hr\"}", hour(switchoffTime), minute(switchoffTime) );
} else {
sprintf(temp,",\"sst\":\"%d:%d hr\"}", hour(switchoffTime), minute(switchoffTime) );
}
strcat(json, temp);
Serial.println("json length = " + String(strlen(json)));
request->send(200, "text/json", json);
//json = String();
});
// this link is called by the detailspage
server.on("/get.Inverter", HTTP_GET, [](AsyncWebServerRequest *request) {
// this is used by the detailspage and for http requests
// set the array into a json object
// int panelCount=4;
int i;
if (request->hasArg("inv")) {
i = (request->arg("inv").toInt()) ;
} else {
i = iKeuze;
//request->send(200, "text/plain", "no argument provided");
}
//Serial.println("i = " + String(i));
if( i < inverterCount) {
char json[350]={0} ;
// we need name polled type id serial
snprintf(json, sizeof(json), "{\"inv\":\"%d\",\"name\":\"%s\",\"polled\":\"%d\",\"type\":\"%d\",\"serial\":\"%s\",\"sid\":\"%s\",\"freq\":%s,\"temp\":%s,\"acv\":%s,\"sq\":%s" , i, Inv_Prop[i].invLocation, polled[i], Inv_Prop[i].invType, Inv_Prop[i].invSerial, Inv_Prop[i].invID, Inv_Data[i].freq, Inv_Data[i].heath, Inv_Data[i].acv, Inv_Data[i].sigQ );
//// we need to provide values for all panel so when connected this is n/e
char pan[4][50]={0};
for(int z = 0; z < 4; z++ )
{
if(Inv_Prop[i].conPanels[z]) // is this panel connected?
{
sprintf(pan[z], ",\"dcv%d\":%s,\"dcc%d\":%s,\"pow%d\":%s,\"en%d\":%s" , z , Inv_Data[i].dcv[z], z ,Inv_Data[i].dcc[z],z ,Inv_Data[i].power[z], z, en_saved[i][z]);
} else {
sprintf(pan[z], ",\"dcv%d\":\"n/e\",\"dcc%d\":\"n/e\",\"pow%d\":\"n/e\",\"en%d\":\"n/e\"", z ,z ,z ,z);
}
strcat(json, pan[z]);
}
//
char tail[40]={0};
sprintf(tail, ",\"power\":%s,\"eN\":%.2f}", Inv_Data[i].power[4], Inv_Data[i].en_total/(float)1000);
strcat(json, tail);
request->send(200, "text/json", json);
// json = String();
} else {
String term = "unknown inverter " + String(i);
request->send(200, "text/plain", term);
}
});
// this link provides the inverterdata on the frontpage
server.on("/get.Power", HTTP_GET, [](AsyncWebServerRequest *request) {
// set the array into a json object
uint8_t remote = 0;
if(checkRemote( request->client()->remoteIP().toString()) ) remote = 1; // for the menu link
char json[200]={0};
int i = atoi(request->arg("inv").c_str()) ;
uint8_t night = 0;
if(!timeRetrieved ) { night=0; } else {
//if(now() > switchonTime && now() < switchoffTime )
if(dayTime) { night = 0; } else { night = 1; }
}
snprintf(json, sizeof(json), "{\"nm\":\"%s\",\"polled\":\"%d\",\"type\":\"%d\",\"count\":\"%d\",\"remote\":\"%d\",\"state\":\"%d\",\"sleep\":\"%d\"" ,Inv_Prop[i].invLocation, polled[i], Inv_Prop[i].invType, inverterCount, remote, zigbeeUp, night );
// now populate the values depending on if the panel exists and if polled
char pan[4][50]={0};
for(int z = 0; z < 4; z++ )
{
//is the panel connected? if not put n/e
if( ! Inv_Prop[i].conPanels[z] ) { sprintf(pan[z], ",\"p%d\":\"n/e\"", z); }
// so the panel is connected, is the inverter polled?
else if (polled[i])
{
//polled, we put a value
sprintf( pan[z], ",\"p%d\":\"%s\"", z, Inv_Data[i].power[z] );
} else {
// not polled, we put n/a
sprintf(pan[z], ",\"p%d\":\"n/a\"", z);
}
// add this to json
strcat(json, pan[z]);
}
char tail[20]={0};
if (polled[i]) {
sprintf(tail, ",\"eN\":\"%.2f\"}", Inv_Data[i].en_total/(float)1000);
strcat(json, tail);
} else {
strcat(json, ",\"eN\":\"n/a\"}");
}
request->send(200, "text/json", json);
});
server.on("/get.Paired", HTTP_GET, [](AsyncWebServerRequest *request) {
// set the array into a json object
String json="{";
json += "\"invID\":\"" + String(Inv_Prop[iKeuze].invID) + "\"";
json += "}";
request->send(200, "text/json", json);
json = String();
});
// if everything failed we come here
server.onNotFound([](AsyncWebServerRequest *request){
//Serial.println("unknown request");
handleNotFound(request);
});
//AsyncElegantOTA.begin(&server);
server.begin();
}
void confirm() {
toSend="<html><body onload=\"setTimeout(function(){window.location.href='/';}, 3000 );\"><br><br><center><h3>processing<br>your request,<br>please wait</html>";
}