From 2085b6bf9f47ddb12cb7b347ed86c8b2e32ddb8a Mon Sep 17 00:00:00 2001 From: zivillian Date: Fri, 17 Jun 2022 20:27:16 +0200 Subject: [PATCH] use own OTA update --- include/pages.h | 1 + platformio.ini | 1 - src/main.cpp | 2 -- src/pages.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/include/pages.h b/include/pages.h index 40b2b62..1ac7d9d 100644 --- a/include/pages.h +++ b/include/pages.h @@ -4,6 +4,7 @@ #include #include #include + #include #include "config.h" void setupPages(AsyncWebServer* server, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config); diff --git a/platformio.ini b/platformio.ini index 9614930..40f429c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,5 @@ framework = arduino lib_deps = https://github.com/tzapu/WiFiManager.git https://github.com/me-no-dev/ESPAsyncWebServer.git - ayushsharma82/AsyncElegantOTA @ 2.2.7 eModbus ;build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 06b907d..dfeb04d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -38,7 +37,6 @@ void setup() { MBbridge.start(config.getTcpPort(), 10, 10000); dbgln("[modbus] finished"); setupPages(&webServer, &MBclient, &MBbridge, &config); - AsyncElegantOTA.begin(&webServer); webServer.begin(); dbgln("[setup] finished"); } diff --git a/src/pages.cpp b/src/pages.cpp index f8b8653..cd2d546 100644 --- a/src/pages.cpp +++ b/src/pages.cpp @@ -8,6 +8,7 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * sendButton(response, "Status", "status"); sendButton(response, "Config", "config"); sendButton(response, "Debug", "debug"); + sendButton(response, "Firmware update", "update"); sendButton(response, "Reboot", "reboot", "r"); sendResponseTrailer(response); request->send(response); @@ -185,6 +186,56 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi * sendResponseTrailer(response); request->send(response); }); + server->on("/update", HTTP_GET, [](AsyncWebServerRequest *request){ + dbgln("[webserver] GET /update"); + auto *response = request->beginResponseStream("text/html"); + sendResponseHeader(response, "Firmware Update"); + response->print("
" + "" + "

" + "" + "
" + "

"); + sendButton(response, "Back", "/"); + sendResponseTrailer(response); + request->send(response); + }); + server->on("/update", HTTP_POST, [](AsyncWebServerRequest *request){ + dbgln("[webserver] OTA finished"); + if (Update.hasError()){ + auto *response = request->beginResponse(500, "text/plain", "Ota failed"); + response->addHeader("Connection", "close"); + request->send(response); + } + else{ + request->redirect("/"); + } + ESP.restart(); + }, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + dbg("[webserver] OTA progress ");dbgln(index); + if (!index) { + //TODO add MD5 Checksum and Update.setMD5 + int cmd = (filename == "filesystem") ? U_SPIFFS : U_FLASH; + if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) { // Start with max available size + Update.printError(Serial); + return request->send(400, "text/plain", "OTA could not begin"); + } + } + // Write chunked data to the free sketch space + if(len){ + if (Update.write(data, len) != len) { + return request->send(400, "text/plain", "OTA could not write data"); + } + } + if (final) { // if the final flag is set then this is the last frame of data + if (!Update.end(true)) { //true to set the size to the current progress + Update.printError(Serial); + return request->send(400, "text/plain", "Could not end OTA"); + } + }else{ + return; + } + }); server->on("/favicon.ico", [](AsyncWebServerRequest *request){ dbgln("[webserver] GET /favicon.ico"); request->send(204);//TODO add favicon