From d80d1e24e9b1a278f7e94d894dfe6c0c4c80cd43 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 18 Sep 2018 22:58:02 +0200 Subject: [PATCH] ota.cpp: added a retry loop for write-to-flash --- platformio.ini | 2 +- src/ota.cpp | 36 +++++++++++++++++++++++++----------- src/paxcounter.conf | 1 + 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index 5ca50241..7cf58877 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,7 +26,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng [common] ; for release_version use max.10 chars total, use any decimal format like "a.b.c" -release_version = 1.4.33 +release_version = 1.4.34 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose debug_level = 0 diff --git a/src/ota.cpp b/src/ota.cpp index 5d94bbed..34136f85 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -112,7 +112,7 @@ void processOTAUpdate(const String &version) { } } - // ESP_LOGI(TAG, "Requesting: " + firmwarePath); + ESP_LOGI(TAG, "Requesting %s", firmwarePath); client.print(String("GET ") + firmwarePath + " HTTP/1.1\r\n"); client.print(String("Host: ") + currentHost + "\r\n"); @@ -185,20 +185,33 @@ void processOTAUpdate(const String &version) { // check whether we have everything for OTA update if (contentLength && isValidContentType) { - if (Update.begin(contentLength)) { - ESP_LOGI(TAG, "Starting OTA update. This will take some time to " - "complete..."); - size_t written = Update.writeStream(client); - if (written == contentLength) { - ESP_LOGI(TAG, "Written %d bytes successfully", written); - } else { - ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", - written, contentLength); - // Retry?? + size_t written; + + if (Update.begin(contentLength)) { + + int i = FLASH_MAX_TRY; + while ((i--) && (written != contentLength)) { + + ESP_LOGI(TAG, + "Starting OTA update, attempt %d of %d. This will take some " + "time to complete...", + i, FLASH_MAX_TRY); + + written = Update.writeStream(client); + + if (written == contentLength) { + ESP_LOGI(TAG, "Written %d bytes successfully", written); + break; + } else { + ESP_LOGI(TAG, + "Written only %d of %d bytes, OTA update attempt cancelled.", + written, contentLength); + } } if (Update.end()) { + if (Update.isFinished()) { ESP_LOGI(TAG, "OTA update completed. Rebooting to runmode."); ESP.restart(); @@ -209,6 +222,7 @@ void processOTAUpdate(const String &version) { } else { ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); } + } else { ESP_LOGI(TAG, "There isn't enough space to start OTA update"); client.flush(); diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 92380894..1c6c6948 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -67,6 +67,7 @@ // OTA settings #define WIFI_MAX_TRY 20 // maximum number of wifi connect attempts for OTA update [default = 20] +#define FLASH_MAX_TRY 3 // maximum number of attempts for writing update binary to flash [default = 3] // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored