Skip to content

Commit c697ef7

Browse files
committed
show success page after firmware update
1 parent 37f523f commit c697ef7

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

include/pages.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#include "debug.h"
1111

1212
void setupPages(AsyncWebServer* server, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm);
13-
void sendResponseHeader(AsyncResponseStream *response, const char *title);
13+
void sendResponseHeader(AsyncResponseStream *response, const char *title, bool inlineStyle = false);
1414
void sendResponseTrailer(AsyncResponseStream *response);
1515
void sendButton(AsyncResponseStream *response, const char *title, const char *action, const char *css = "");
1616
void sendTableRow(AsyncResponseStream *response, const char *name, uint32_t value);
1717
void sendTableRow(AsyncResponseStream *response, const char *name, String value);
1818
void sendDebugForm(AsyncResponseStream *response, String slaveId, String reg, String function, String count);
19+
void sendMinCss(AsyncResponseStream *response);
1920
const String ErrorName(Modbus::Error code);
2021
const String WiFiQuality(int rssiValue);
2122
#endif /* PAGES_H */

src/pages.cpp

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,24 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
337337
request->send(response);
338338
});
339339
server->on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
340+
request->onDisconnect([](){
341+
ESP.restart();
342+
});
340343
dbgln("[webserver] OTA finished");
341344
if (Update.hasError()){
342345
auto *response = request->beginResponse(500, "text/plain", "Ota failed");
343346
response->addHeader("Connection", "close");
344347
request->send(response);
345348
}
346349
else{
347-
request->redirect("/");
350+
auto *response = request->beginResponseStream("text/html");
351+
response->addHeader("Connection", "close");
352+
sendResponseHeader(response, "Firmware Update", true);
353+
response->print("<p>Update successful.</p>");
354+
sendButton(response, "Back", "/");
355+
sendResponseTrailer(response);
356+
request->send(response);
348357
}
349-
ESP.restart();
350358
}, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
351359
dbg("[webserver] OTA progress ");dbgln(index);
352360
if (!index) {
@@ -410,31 +418,9 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
410418
}
411419
}
412420
dbgln("[webserver] GET /style.css");
413-
auto *response = request->beginResponse(200, "text/css",
414-
"body{"
415-
"font-family:sans-serif;"
416-
"text-align: center;"
417-
"background: #252525;"
418-
"color: #faffff;"
419-
"}"
420-
"#content{"
421-
"display: inline-block;"
422-
"min-width: 340px;"
423-
"}"
424-
"button{"
425-
"width: 100%;"
426-
"line-height: 2.4rem;"
427-
"background: #1fa3ec;"
428-
"border: 0;"
429-
"border-radius: 0.3rem;"
430-
"font-size: 1.2rem;"
431-
"-webkit-transition-duration: 0.4s;"
432-
"transition-duration: 0.4s;"
433-
"color: #faffff;"
434-
"}"
435-
"button:hover{"
436-
"background: #0e70a4;"
437-
"}"
421+
auto *response = request->beginResponseStream("text/css");
422+
sendMinCss(response);
423+
response->print(
438424
"button.r{"
439425
"background: #d43535;"
440426
"}"
@@ -464,14 +450,49 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
464450
});
465451
}
466452

467-
void sendResponseHeader(AsyncResponseStream *response, const char *title){
453+
void sendMinCss(AsyncResponseStream *response){
454+
response->print("body{"
455+
"font-family:sans-serif;"
456+
"text-align: center;"
457+
"background: #252525;"
458+
"color: #faffff;"
459+
"}"
460+
"#content{"
461+
"display: inline-block;"
462+
"min-width: 340px;"
463+
"}"
464+
"button{"
465+
"width: 100%;"
466+
"line-height: 2.4rem;"
467+
"background: #1fa3ec;"
468+
"border: 0;"
469+
"border-radius: 0.3rem;"
470+
"font-size: 1.2rem;"
471+
"-webkit-transition-duration: 0.4s;"
472+
"transition-duration: 0.4s;"
473+
"color: #faffff;"
474+
"}"
475+
"button:hover{"
476+
"background: #0e70a4;"
477+
"}");
478+
}
479+
480+
void sendResponseHeader(AsyncResponseStream *response, const char *title, bool inlineStyle){
468481
response->print("<!DOCTYPE html>"
469482
"<html lang=\"en\" class=\"\">"
470483
"<head>"
471484
"<meta charset='utf-8'>"
472485
"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>");
473486
response->printf("<title>ESP32 Modbus Gateway - %s</title>", title);
474-
response->print("<link rel=\"stylesheet\" href=\"style.css\">"
487+
if (inlineStyle){
488+
response->print("<style>");
489+
sendMinCss(response);
490+
response->print("</style>");
491+
}
492+
else{
493+
response->print("<link rel=\"stylesheet\" href=\"style.css\">");
494+
}
495+
response->print(
475496
"</head>"
476497
"<body>"
477498
"<h2>ESP32 Modbus Gateway</h2>");

0 commit comments

Comments
 (0)