From 3a519bdc542bfd2ccc3f96f996dbb7f42f77373b Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 9 Feb 2021 21:23:37 -0600 Subject: [PATCH] Added logic to check for OTA partitions Warning will be thrown If OTA is enabled but partition table is not configured for OTA. OTA will not be started. --- src/HomeSpan.cpp | 73 ++++++++++++++++++++++++++---------------------- src/src.ino | 4 +-- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 5426cbd..41851a3 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "HomeSpan.h" #include "HAP.h" @@ -445,41 +446,45 @@ void Span::checkConnect(){ hapServer->begin(); if(otaEnabled){ - ArduinoOTA.setHostname(hostName); - ArduinoOTA.setPasswordHash(otaPwd); + if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ + ArduinoOTA.setHostname(hostName); + ArduinoOTA.setPasswordHash(otaPwd); - ArduinoOTA - .onStart([]() { - String type; - if (ArduinoOTA.getCommand() == U_FLASH) - type = "sketch"; - else // U_SPIFFS - type = "filesystem"; - Serial.println("\n*** OTA Starting:" + type); - homeSpan.statusLED.start(LED_OTA_STARTED); - }) - .onEnd([]() { - Serial.println("\n*** OTA Completed. Rebooting..."); - homeSpan.statusLED.off(); - }) - .onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("*** Progress: %u%%\r", (progress / (total / 100))); - }) - .onError([](ota_error_t error) { - Serial.printf("*** OTA Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed\n"); - else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed\n"); - else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed\n"); - else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed\n"); - else if (error == OTA_END_ERROR) Serial.println("End Failed\n"); - }); - - ArduinoOTA.begin(); - Serial.print("Starting OTA: "); - Serial.print(displayName); - Serial.print(" at "); - Serial.print(WiFi.localIP()); - Serial.print("\n"); + ArduinoOTA + .onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; + Serial.println("\n*** OTA Starting:" + type); + homeSpan.statusLED.start(LED_OTA_STARTED); + }) + .onEnd([]() { + Serial.println("\n*** OTA Completed. Rebooting..."); + homeSpan.statusLED.off(); + }) + .onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("*** Progress: %u%%\r", (progress / (total / 100))); + }) + .onError([](ota_error_t error) { + Serial.printf("*** OTA Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed\n"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed\n"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed\n"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed\n"); + else if (error == OTA_END_ERROR) Serial.println("End Failed\n"); + }); + + ArduinoOTA.begin(); + Serial.print("Starting OTA: "); + Serial.print(displayName); + Serial.print(" at "); + Serial.print(WiFi.localIP()); + Serial.print("\n"); + } else { + Serial.print("\n*** Warning: Can't enable OTA - Partition table used to compile this sketch is not configured for OTA.\n\n"); + } } Serial.print("\n"); diff --git a/src/src.ino b/src/src.ino index 6df17bf..b12712f 100644 --- a/src/src.ino +++ b/src/src.ino @@ -8,13 +8,13 @@ void setup() { Serial.begin(115200); - homeSpan.setLogLevel(2); + homeSpan.setLogLevel(1); // homeSpan.setHostNameSuffix(""); homeSpan.setPortNum(1200); homeSpan.setMaxConnections(16); // homeSpan.setQRID("One1"); - homeSpan.enableOTA(); +// homeSpan.enableOTA(); homeSpan.setSketchVersion("Test 1.2.3"); homeSpan.begin(Category::Lighting,"HomeSpanTest");