diff --git a/Software/Software.ino b/Software/Software.ino index 13182646..510a5aad 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -127,6 +127,9 @@ void setup() { // Perform main program functions void loop() { + // Over-the-air updates by ElegantOTA + ElegantOTA.loop(); + // Input receive_can(); // Receive CAN messages. Runs as fast as possible #ifdef DUAL_CAN diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index 207f29aa..3573aee2 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -3,6 +3,9 @@ // Create AsyncWebServer object on port 80 AsyncWebServer server(80); +// Measure OTA progress +unsigned long ota_progress_millis = 0; + const char index_html[] PROGMEM = R"rawliteral( @@ -49,7 +52,11 @@ void init_webserver() { [](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, processor); }); // Send a GET request to /update - server.on("/update", HTTP_GET, [](AsyncWebServerRequest* request) { request->send(200, "text/plain", "OK"); }); + server.on("/debug", HTTP_GET, + [](AsyncWebServerRequest* request) { request->send(200, "text/plain", "Debug: all OK."); }); + + // Initialize ElegantOTA + init_ElegantOTA(); // Start server server.begin(); @@ -102,6 +109,14 @@ void init_WiFi_STA(const char* ssid, const char* password) { } } +void init_ElegantOTA() { + ElegantOTA.begin(&server); // Start ElegantOTA + // ElegantOTA callbacks + ElegantOTA.onStart(onOTAStart); + ElegantOTA.onProgress(onOTAProgress); + ElegantOTA.onEnd(onOTAEnd); +} + String processor(const String& var) { if (var == "PLACEHOLDER") { String content = ""; @@ -136,3 +151,27 @@ String processor(const String& var) { } return String(); } + +void onOTAStart() { + // Log when OTA has started + Serial.println("OTA update started!"); + // +} + +void onOTAProgress(size_t current, size_t final) { + // Log every 1 second + if (millis() - ota_progress_millis > 1000) { + ota_progress_millis = millis(); + Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final); + } +} + +void onOTAEnd(bool success) { + // Log when OTA has finished + if (success) { + Serial.println("OTA update finished successfully!"); + } else { + Serial.println("There was an error during OTA update!"); + } + // +} diff --git a/Software/src/devboard/webserver/webserver.h b/Software/src/devboard/webserver/webserver.h index 36ccb023..f599cea9 100644 --- a/Software/src/devboard/webserver/webserver.h +++ b/Software/src/devboard/webserver/webserver.h @@ -4,6 +4,7 @@ // Load Wi-Fi library #include #include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password +#include "../../lib/ayushsharma82-ElegantOTA/src/ElegantOTA.h" #include "../../lib/me-no-dev-AsyncTCP/src/AsyncTCP.h" #include "../../lib/me-no-dev-ESPAsyncWebServer/src/ESPAsyncWebServer.h" #include "../config.h" // Needed for LED defines @@ -43,12 +44,49 @@ void init_WiFi_AP(); void init_WiFi_STA(const char* ssid, const char* password); /** - * @brief Replaces placeholder with content section in web page + * @brief Initialization function for ElegantOTA. + * + * @param[in] void * + * @return void + */ +void init_ElegantOTA(); + +/** + * @brief Replaces placeholder with content section in web page + * * @param[in] var * * @return String */ String processor(const String& var); +/** + * @brief Executes on OTA start + * + * @param[in] void + * + * @return void + */ +void onOTAStart(); + +/** + * @brief Executes on OTA progress + * + * @param[in] current Current bytes + * @param[in] final Final bytes + * + * @return void + */ +void onOTAProgress(size_t current, size_t final); + +/** + * @brief Executes on OTA end + * + * @param[in] void + * + * @return bool success: success = true, failed = false + */ +void onOTAEnd(bool success); + #endif