From 064da6b4f1d2d8549e39cce52861bba33aec0b87 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sun, 22 Dec 2024 03:16:03 +0100 Subject: [PATCH] feat: add ota upgrade --- esp32/platformio.ini | 3 +++ esp32/src/main.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/esp32/platformio.ini b/esp32/platformio.ini index 46c6895..9b4fd6d 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -59,3 +59,6 @@ build_flags = -D FEATURE_CONFIGAP=D0 -D FEATURE_CONFIGAP_GND=D1 -D FEATURE_LDR=A2 + -D FEATURE_OTA=1 +upload_protocol = espota +upload_port = spaceapimap.local \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 28db6cf..a90eb07 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -9,6 +9,9 @@ #include #include #include +#ifdef FEATURE_OTA +#include +#endif #include "spacemap.h" @@ -204,6 +207,43 @@ void setup() { filter[0]["data"]["state"]["open"] = true; filter[0]["data"]["state"]["lastchange"] = true; +#ifdef FEATURE_OTA + ArduinoOTA + .onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) { + type = "sketch"; + } else { // U_SPIFFS + type = "filesystem"; + } + + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() + Serial.println("Start updating " + type); + }) + .onEnd([]() { + Serial.println("\nEnd"); + }) + .onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }) + .onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) { + Serial.println("Auth Failed"); + } else if (error == OTA_BEGIN_ERROR) { + Serial.println("Begin Failed"); + } else if (error == OTA_CONNECT_ERROR) { + Serial.println("Connect Failed"); + } else if (error == OTA_RECEIVE_ERROR) { + Serial.println("Receive Failed"); + } else if (error == OTA_END_ERROR) { + Serial.println("End Failed"); + } + }) + .setHostname("spaceapimap"); + ArduinoOTA.begin(); +#endif // FEATURE_OTA + #ifdef FEATURE_LDR analogSetPinAttenuation(FEATURE_LDR, ADC_11db); #endif // FEATURE_LDR @@ -375,5 +415,10 @@ void loop() { } // Finally, update the LED chain pixels.show(); + +#ifdef FEATURE_OTA + ArduinoOTA.handle(); +#endif + delay(50); }