From f7f1bec255ba9f3c58277ae22056b9bc79a2f702 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 30 Sep 2023 02:54:38 +0700 Subject: [PATCH] ota update feature --- platformio.ini | 4 +-- src/espmega_iot_core.cpp | 39 +++++++++++++++++++++++++++- src/espmega_iot_core.hpp | 8 ++++++ src/espmega_iot_ota.cpp | 56 ++++++++++++++++++++++++++++++++++++++++ src/espmega_iot_ota.hpp | 4 +++ 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/espmega_iot_ota.cpp create mode 100644 src/espmega_iot_ota.hpp diff --git a/platformio.ini b/platformio.ini index c2961a6..0074e59 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,5 +21,5 @@ lib_deps = siwats/ESPMegaPROR3@^1.3.0 robtillaart/DHTNEW@^0.4.18 seithan/Easy Nextion Library@^1.0.6 robtillaart/FRAM_I2C@^0.6.1 -monitor_speed = 115200 -board_build.partitions = no_ota.csv \ No newline at end of file + esphome/ESPAsyncWebServer-esphome@^3.1.0 +monitor_speed = 115200 \ No newline at end of file diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index f162c9b..08cde64 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -1,7 +1,7 @@ #include // OS Configuration -#define FASTBOOT +//#define FASTBOOT // Network Connectivity char HOSTNAME[15]; @@ -11,6 +11,7 @@ IPAddress GATEWAY(0, 0, 0, 0); IPAddress DNS(0, 0, 0, 0); IPAddress MQTT_SERVER(0, 0, 0, 0); uint16_t MQTT_PORT = 0; +WebServer otaserver(80); bool standalone = true; // #define MQTT_BASE_TOPIC "/espmega/ProR3" char MQTT_BASE_TOPIC[20]; @@ -141,6 +142,7 @@ void setup() mqtt_connect(); lcd_send_command("boot_state.txt=\"Threads Initializing . . .\""); thread_initialization(); + ota_begin(); Serial.println("Initialization Completed."); Serial.println("Jumping to User Code."); user_init(); @@ -151,6 +153,7 @@ void setup() Serial2.write(0xFF); Serial2.write(0xFF); #endif + } void loop() @@ -162,6 +165,9 @@ void loop() thread_controller.run(); lcd_loop(); user_loop(); + otaserver.handleClient(); + + } void eeprom_retrieve_init() @@ -242,6 +248,37 @@ void eeprom_retrieve_init() strcat(INPUTS_TOPIC, "/input/00"); } +void ota_begin() { + otaserver.on("/", HTTP_GET, []() { + otaserver.sendHeader("Connection", "close"); + otaserver.send(200, "text/html", ota_upload_page); + }); + otaserver.on("/update", HTTP_POST, []() { + otaserver.sendHeader("Connection", "close"); + otaserver.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, []() { + HTTPUpload& upload = otaserver.upload(); + if (upload.status == UPLOAD_FILE_START) { + Serial.printf("Update: %s\n", upload.filename.c_str()); + if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { + Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { + Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } else { + Update.printError(Serial); + } + } + }); + otaserver.begin(); +} + void io_begin() { Serial.println("Initializing I/O . . ."); diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index bcf4a4e..19d97a2 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -10,9 +10,15 @@ #include #include #include +#include +#include +#include + #include "lcd.hpp" #include "user_code.hpp" #include "ir_codes.hpp" +#include "espmega_iot_ota.hpp" +#include "espmega_iot_timer.hpp" void virtual_interrupt_loop(); void virtual_interrupt_callback(int pin, int state); @@ -26,6 +32,8 @@ void state_request_callback(String topic, String message); void io_begin(); void ir_loop(); +void ota_begin(); + void publish_pwm_states(); void publish_pwm_state(int id); void pwm_set_state(int id, int state); diff --git a/src/espmega_iot_ota.cpp b/src/espmega_iot_ota.cpp new file mode 100644 index 0000000..b1be6de --- /dev/null +++ b/src/espmega_iot_ota.cpp @@ -0,0 +1,56 @@ +#include "espmega_iot_ota.hpp" +/* Style */ +String ota_style = +""; + +/* Server Index Page */ +String ota_upload_page = +"" +"
" +"

ESPMega PRO

" +"

Programming Tool

" +"" +"" +"" +"

" +"
" +"

" +"" + ota_style; \ No newline at end of file diff --git a/src/espmega_iot_ota.hpp b/src/espmega_iot_ota.hpp new file mode 100644 index 0000000..9531b86 --- /dev/null +++ b/src/espmega_iot_ota.hpp @@ -0,0 +1,4 @@ +#pragma once +#include +extern String ota_upload_page; +extern String ota_style; \ No newline at end of file