From e3514cef3c42f181f79081a5d9203598901b8175 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sun, 22 Oct 2023 00:36:22 +0700 Subject: [PATCH] initial lcd ota code --- src/espmega_iot_ledota.cpp | 52 ++++++++++++++++++++++++++++++++++++++ src/espmega_iot_ledota.hpp | 17 +++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/espmega_iot_ledota.cpp create mode 100644 src/espmega_iot_ledota.hpp diff --git a/src/espmega_iot_ledota.cpp b/src/espmega_iot_ledota.cpp new file mode 100644 index 0000000..14313cb --- /dev/null +++ b/src/espmega_iot_ledota.cpp @@ -0,0 +1,52 @@ +#include + +ESPMega_LCDOTA::register_ota() +{ + otaserver.on( + update_endpoint, HTTP_POST, []() + { + otaserver.sendHeader("Connection", "close"); + otaserver.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); }, + this->update_action); +} + +ESPMega_LCDOTA::update_action(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) +{ + HTTPUpload &upload = otaserver.upload(); + if (upload.status == UPLOAD_FILE_START) + { + send_stop_bit(); + update_init(); + } + else if (upload.status == UPLOAD_FILE_WRITE) + { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) + { + panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . ."); + Update.printError(Serial); + } + if (upload.currentSize != 0 && upload.totalSize != 0) + { + lcd_send_stop_bit(); + uint32_t totalsize_kb = upload.totalSize / 1000; + uint32_t upload_pct = 100 * upload.totalSize / 1000000; + String otafiletxt = "Downloading File : " + upload.filename + " (" + String(totalsize_kb) + "KB)"; + panel.writeNum("prog.val", upload_pct); + panel.writeStr("otatxt.txt", otafiletxt); + } + } + else if (upload.status == UPLOAD_FILE_END) + { + if (Update.end(true)) + { + panel.writeStr("otatxt.txt", "Update Completed, Rebooting . . ."); + Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } + else + { + panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . ."); + Update.printError(Serial); + } + } +} \ No newline at end of file diff --git a/src/espmega_iot_ledota.hpp b/src/espmega_iot_ledota.hpp new file mode 100644 index 0000000..3bcd073 --- /dev/null +++ b/src/espmega_iot_ledota.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include +class ESPMega_LCDOTA { + public: + ESPMega_LCDOTA(); + void update_init(); + void write_data(); + void update_end(); + private: + char update_endpoint[20]; + void register_ota(); + void send_stop_bit(); + void update_action(); + HardwareSerial lcdserial; + WebServer otaserver; +} \ No newline at end of file