From 15a61188fdc5b2f01275c311b4e7e836d28f864b Mon Sep 17 00:00:00 2001 From: fvanroie Date: Tue, 10 Mar 2020 15:12:05 +0100 Subject: [PATCH] Progress OTA messages --- src/hasp_ota.cpp | 44 +++++++++++++++++++------------------------- src/hasp_ota.h | 3 ++- src/main.cpp | 27 ++++++++++++++++++--------- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/hasp_ota.cpp b/src/hasp_ota.cpp index f0032ed6..81bca3f2 100644 --- a/src/hasp_ota.cpp +++ b/src/hasp_ota.cpp @@ -10,26 +10,13 @@ #define F_OTA_URL F("otaurl") -std::string otaUrl = "http://10.1.0.3"; -int8_t prevUpdateValue = -1; +std::string otaUrl = "http://10.1.0.3"; +int8_t otaPrecentageComplete = -1; -void otaProgress(uint8_t val) +void otaProgress() { - String type; - uint8_t deltaValue; - - if(ArduinoOTA.getCommand() == U_FLASH) { - type = F("Firmware"); - deltaValue = 4; - } else { // U_SPIFFS - deltaValue = 2; - type = F("Filesystem"); - } - - if(val - prevUpdateValue >= deltaValue) { - debugPrintln(String(F("OTA: ")) + type + F(" update in progress... ") + val + "%"); - prevUpdateValue = val; - } + debugPrintln(String(F("OTA: ")) + (ArduinoOTA.getCommand() == U_FLASH ? F("Firmware") : F("Filesystem")) + + F(" update in progress... ") + otaPrecentageComplete + "%"); } void otaSetup(JsonObject settings) @@ -53,23 +40,24 @@ void otaSetup(JsonObject settings) debugPrintln(F("OTA: Start update")); dispatchCommand("page 0"); + otaPrecentageComplete = 0; // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\""); }); ArduinoOTA.onEnd([]() { - prevUpdateValue = -1; - otaProgress(100); + otaPrecentageComplete = 100; + otaProgress(); + otaPrecentageComplete = -1; dispatchPage("0"); // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rComplete!\""); dispatchReboot(true); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - if(total != 0) { - int8_t val = progress * 100 / total; - otaProgress(val); - } + if(total != 0) otaPrecentageComplete = progress * 100 / total; + // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rProgress: " + String(progress / (total / 100)) + "%\""); }); ArduinoOTA.onError([](ota_error_t error) { + otaPrecentageComplete = -1; debugPrintln(String(F("OTA: ERROR code ")) + String(error)); if(error == OTA_AUTH_ERROR) debugPrintln(F("OTA: ERROR - Auth Failed")); @@ -87,9 +75,15 @@ void otaSetup(JsonObject settings) }); ArduinoOTA.begin(); debugPrintln(F("OTA: Over the Air firmware update ready")); + debugPrintln(F("OTA: Setup Complete")); } -void otaLoop(bool wifiIsConnected) +void otaLoop() { ArduinoOTA.handle(); } + +void otaEverySecond() +{ + if(otaPrecentageComplete >= 0) otaProgress(); +} \ No newline at end of file diff --git a/src/hasp_ota.h b/src/hasp_ota.h index 4eea4144..429d2808 100644 --- a/src/hasp_ota.h +++ b/src/hasp_ota.h @@ -4,6 +4,7 @@ #include "ArduinoJson.h" void otaSetup(JsonObject settings); -void otaLoop(bool wifiIsConnected); +void otaLoop(void); +void otaEverySecond(void); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f36e3356..b3add7cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,15 +88,15 @@ void setup() wifiSetup(settings[F("wifi")]); #if HASP_USE_MQTT - mqttSetup(settings[F("mqtt")]); + // mqttSetup(settings[F("mqtt")]); #endif #if HASP_USE_TELNET - telnetSetup(settings[F("telnet")]); + // telnetSetup(settings[F("telnet")]); #endif #if HASP_USE_MDNS - mdnsSetup(settings[F("mdns")]); + // mdnsSetup(settings[F("mdns")]); #endif #if HASP_USE_HTTP @@ -134,30 +134,39 @@ void loop() /* Network Services Loops */ #if HASP_USE_WIFI isConnected = wifiLoop(); - #if HASP_USE_MQTT mqttLoop(isConnected); #endif + if(isConnected) { #if HASP_USE_HTTP - httpLoop(isConnected); + httpLoop(); #endif #if HASP_USE_TELNET - telnetLoop(isConnected); + telnetLoop(); #endif #if HASP_USE_MDNS - mdnsLoop(isConnected); + mdnsLoop(); #endif #if HASP_USE_BUTTON - buttonLoop(); + buttonLoop(); #endif + } - otaLoop(isConnected); + otaLoop(); debugLoop(); #endif + static unsigned long mainLastLoopTime = 0; + + // Every Secons Loop + if(millis() - mainLastLoopTime >= 1000) { + mainLastLoopTime += 1000; + otaEverySecond(); + } + // delay(1); } \ No newline at end of file