From 2d67a44ad032629fa40593395a8f6500208b00bd Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Mon, 4 Apr 2022 23:03:36 +0200 Subject: [PATCH] Added firmware upload page --- html/firmware.htm | 174 ++++++++++++++++++++++++++++++++++++++++++ html/firmware.min.htm | 9 +++ html/upload.htm | 2 +- html/upload.min.htm | 2 +- script/copy_html.py | 3 + src/resources.cpp | 2 + src/webserver.cpp | 100 ++++++++++++++++++------ src/webserver.hpp | 8 +- 8 files changed, 275 insertions(+), 25 deletions(-) create mode 100644 html/firmware.htm create mode 100644 html/firmware.min.htm diff --git a/html/firmware.htm b/html/firmware.htm new file mode 100644 index 0000000..d8cff7a --- /dev/null +++ b/html/firmware.htm @@ -0,0 +1,174 @@ + + + + + + + Beer Gravity Monitor + + + + + + + + + + + + + +
+ +
+ + + + + +
+
Here you can upload a new firmware version, it will not check the version number so you can also downgrade the firmware here. +
+
+ +
+
Current version:
+
Loading...
+
+
+
Platform:
+
Loading...
+
+ +
+ +
+
+ + +
+ +
+
+ +
+
+
+ +
+
+ + + + + +
(C) Copyright 2021-22 Magnus Persson
+ + \ No newline at end of file diff --git a/html/firmware.min.htm b/html/firmware.min.htm new file mode 100644 index 0000000..8887857 --- /dev/null +++ b/html/firmware.min.htm @@ -0,0 +1,9 @@ +Beer Gravity Monitor

Here you can upload a new firmware version, it will not check the version number so you can also downgrade the firmware here.
Current version:
Loading...
Platform:
Loading...

(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file diff --git a/html/upload.htm b/html/upload.htm index 515cbbf..d06eb25 100644 --- a/html/upload.htm +++ b/html/upload.htm @@ -90,7 +90,7 @@
- +
diff --git a/html/upload.min.htm b/html/upload.min.htm index affe5f1..08f7198 100644 --- a/html/upload.min.htm +++ b/html/upload.min.htm @@ -1 +1 @@ -Beer Gravity Monitor

The listed files below needs to be uploaded to the FileSystem in order for the GUI to work. You can also flash the LittleFS filesystem but in that case you will loose your device settings. An OTA upgrade will automatically download the files if they are found in the same location as the firmware.bin. This page is a fallback option.

Once all the files are confirmed, please reboot the device for normal operation.
index.min.htm
Checking...
config.min.htm
Checking...
calibration.min.htm
Checking...
format.min.htm
Checking...
test.min.htm
Checking...
about.min.htm
Checking...

(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file +Beer Gravity Monitor

The listed files below needs to be uploaded to the FileSystem in order for the GUI to work. You can also flash the LittleFS filesystem but in that case you will loose your device settings. An OTA upgrade will automatically download the files if they are found in the same location as the firmware.bin. This page is a fallback option.

Once all the files are confirmed, please reboot the device for normal operation.
index.min.htm
Checking...
config.min.htm
Checking...
calibration.min.htm
Checking...
format.min.htm
Checking...
test.min.htm
Checking...
about.min.htm
Checking...

(C) Copyright 2021-22 Magnus Persson
\ No newline at end of file diff --git a/script/copy_html.py b/script/copy_html.py index 340b000..b925881 100644 --- a/script/copy_html.py +++ b/script/copy_html.py @@ -29,3 +29,6 @@ shutil.copyfile( source + file, target + file ) file = "test.min.htm" #print( "Copy file: " + source + file + "->" + target + file) shutil.copyfile( source + file, target + file ) +file = "firmware.min.htm" +#print( "Copy file: " + source + file + "->" + target + file) +shutil.copyfile( source + file, target + file ) diff --git a/src/resources.cpp b/src/resources.cpp index 1eaf3bf..3520037 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -40,5 +40,7 @@ INCBIN(AboutHtm, "data/about.min.htm"); // Minium web interface for uploading htm files INCBIN(UploadHtm, "data/upload.min.htm"); #endif +INCBIN(FirmwareHtm, "data/firmware.min.htm"); + // EOF diff --git a/src/webserver.cpp b/src/webserver.cpp index d2298b6..d333827 100644 --- a/src/webserver.cpp +++ b/src/webserver.cpp @@ -106,7 +106,7 @@ void WebServerHandler::webHandleConfig() { // void WebServerHandler::webHandleUpload() { LOG_PERF_START("webserver-api-upload"); - Log.notice(F("WEB : webServer callback for /api/upload." CR)); + Log.notice(F("WEB : webServer callback for /api/upload(get)." CR)); DynamicJsonDocument doc(300); doc["index"] = checkHtmlFile(WebServerHandler::HTML_INDEX); @@ -167,10 +167,11 @@ void WebServerHandler::webHandleUpload() { // void WebServerHandler::webHandleUploadFile() { LOG_PERF_START("webserver-api-upload-file"); - Log.notice(F("WEB : webServer callback for /api/upload/file." CR)); + Log.verbose(F("WEB : webServer callback for /api/upload(post)." CR)); HTTPUpload& upload = _server->upload(); String f = upload.filename; bool validFilename = false; + bool firmware = false; if (f.equalsIgnoreCase("index.min.htm") || f.equalsIgnoreCase("calibration.min.htm") || @@ -181,32 +182,85 @@ void WebServerHandler::webHandleUploadFile() { validFilename = true; } + if (f.endsWith(".bin")) { + validFilename = true; + firmware = true; + } + #if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING) - Log.verbose(F("WEB : webServer callback for /api/upload, receiving file %s, " - "valid=%s." CR), - f.c_str(), validFilename ? "yes" : "no"); + Log.verbose(F("WEB : webServer callback for /api/upload, receiving file %s, %d(%d) " + "valid=%s, firmware=%s." CR), + f.c_str(), upload.currentSize, upload.totalSize, validFilename ? "yes" : "no", firmware ? "yes" : "no"); #endif - if (upload.status == UPLOAD_FILE_START) { - Log.notice(F("WEB : Start upload." CR)); - if (validFilename) _uploadFile = LittleFS.open(f, "w"); - } else if (upload.status == UPLOAD_FILE_WRITE) { - Log.notice(F("WEB : Writing upload." CR)); - if (_uploadFile) - _uploadFile.write( - upload.buf, - upload.currentSize); // Write the received bytes to the file - } else if (upload.status == UPLOAD_FILE_END) { - Log.notice(F("WEB : Finish upload." CR)); - if (_uploadFile) { - _uploadFile.close(); - Log.notice(F("WEB : File uploaded %d bytes." CR), upload.totalSize); + if (firmware) { + // Handle firmware update + uint32_t maxSketchSpace = 1044464; //(ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; + + if (upload.status == UPLOAD_FILE_START) { + _uploadReturn = 200; + Log.notice(F("WEB : Start firmware upload, max sketch size %d kb." CR), maxSketchSpace/1024); + + if (!Update.begin(maxSketchSpace, U_FLASH, PIN_LED)){ + ErrorFileLog errLog; + errLog.addEntry(F("WEB : Not enough space to store for this firmware.")); + _uploadReturn = 500; + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + Log.notice(F("WEB : Writing firmware upload %d (%d)." CR), upload.totalSize, maxSketchSpace); + + if (upload.totalSize > maxSketchSpace) { + Log.error(F("WEB : Firmware file is to large." CR)); + _uploadReturn = 500; + } else if (Update.write(upload.buf, upload.currentSize) != upload.currentSize){ + Log.warning(F("WEB : Firmware write was unsuccessful." CR)); + _uploadReturn = 500; + } + } else if (upload.status == UPLOAD_FILE_END) { + Log.notice(F("WEB : Finish firmware upload." CR)); + if(Update.end(true)) { + _server->send(200); + delay(500); + ESP_RESET(); + } else { + ErrorFileLog errLog; + errLog.addEntry(F("WEB : Failed to finish firmware flashing error=") + String(Update.getError())); + _uploadReturn = 500; + } + } else { + Update.end(); + Log.notice(F("WEB : Firmware flashing aborted." CR)); + _uploadReturn = 500; } - _server->sendHeader("Location", "/"); - _server->send(303); + + delay(0); + } else { - _server->send(500, "text/plain", "Couldn't create file."); + // Handle HTML file upload + if (upload.status == UPLOAD_FILE_START) { + _uploadReturn = 200; + Log.notice(F("WEB : Start html upload." CR)); + + if (validFilename) _uploadFile = LittleFS.open(f, "w"); + } else if (upload.status == UPLOAD_FILE_WRITE) { + Log.notice(F("WEB : Writing html upload." CR)); + if (_uploadFile) + _uploadFile.write( + upload.buf, + upload.currentSize); + } else if (upload.status == UPLOAD_FILE_END) { + Log.notice(F("WEB : Finish html upload." CR)); + if (_uploadFile) { + _uploadFile.close(); + Log.notice(F("WEB : Html file uploaded %d bytes." CR), upload.totalSize); + } + _server->sendHeader("Location", "/"); + _server->send(303); + } else { + _server->send(500, "text/plain", "Couldn't upload html file."); + } } + LOG_PERF_STOP("webserver-api-upload-file"); } @@ -1111,6 +1165,8 @@ bool WebServerHandler::setupWebServer() { _server->on("/", std::bind(&WebServerHandler::webReturnUploadHtm, this)); } #endif + _server->on("/firmware.htm", + std::bind(&WebServerHandler::webReturnFirmwareHtm, this)); _server->serveStatic("/log", LittleFS, ERR_FILENAME); _server->serveStatic("/runtime", LittleFS, RUNTIME_FILENAME); diff --git a/src/webserver.hpp b/src/webserver.hpp index 65a23c6..3969a92 100644 --- a/src/webserver.hpp +++ b/src/webserver.hpp @@ -45,12 +45,14 @@ INCBIN_EXTERN(AboutHtm); #else INCBIN_EXTERN(UploadHtm); #endif +INCBIN_EXTERN(FirmwareHtm); class WebServerHandler { private: ESP8266WebServer* _server = 0; File _uploadFile; int _lastFormulaCreateError = 0; + int _uploadReturn = 200; void webHandleConfig(); void webHandleFormulaWrite(); @@ -78,7 +80,7 @@ class WebServerHandler { String getRequestArguments(); // Inline functions. - void webReturnOK() { _server->send(200); } + void webReturnOK() { _server->send(_uploadReturn); } #if defined(EMBED_HTML) void webReturnIndexHtm() { _server->send_P(200, "text/html", (const char*)gIndexHtmData, @@ -110,6 +112,10 @@ class WebServerHandler { gUploadHtmSize); } #endif + void webReturnFirmwareHtm() { + _server->send_P(200, "text/html", (const char*)gFirmwareHtmData, + gFirmwareHtmSize); + } public: enum HtmlFile {