ota.cpp: added a retry loop for write-to-flash

This commit is contained in:
Klaus K Wilting 2018-09-18 22:58:02 +02:00
parent ed50a79297
commit d80d1e24e9
3 changed files with 27 additions and 12 deletions

View File

@ -26,7 +26,7 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng
[common] [common]
; for release_version use max.10 chars total, use any decimal format like "a.b.c" ; 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! ; 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 ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose
debug_level = 0 debug_level = 0

View File

@ -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("GET ") + firmwarePath + " HTTP/1.1\r\n");
client.print(String("Host: ") + currentHost + "\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 // check whether we have everything for OTA update
if (contentLength && isValidContentType) { if (contentLength && isValidContentType) {
size_t written;
if (Update.begin(contentLength)) { if (Update.begin(contentLength)) {
ESP_LOGI(TAG, "Starting OTA update. This will take some time to "
"complete..."); int i = FLASH_MAX_TRY;
size_t written = Update.writeStream(client); 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) { if (written == contentLength) {
ESP_LOGI(TAG, "Written %d bytes successfully", written); ESP_LOGI(TAG, "Written %d bytes successfully", written);
break;
} else { } else {
ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", ESP_LOGI(TAG,
"Written only %d of %d bytes, OTA update attempt cancelled.",
written, contentLength); written, contentLength);
// Retry?? }
} }
if (Update.end()) { if (Update.end()) {
if (Update.isFinished()) { if (Update.isFinished()) {
ESP_LOGI(TAG, "OTA update completed. Rebooting to runmode."); ESP_LOGI(TAG, "OTA update completed. Rebooting to runmode.");
ESP.restart(); ESP.restart();
@ -209,6 +222,7 @@ void processOTAUpdate(const String &version) {
} else { } else {
ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError());
} }
} else { } else {
ESP_LOGI(TAG, "There isn't enough space to start OTA update"); ESP_LOGI(TAG, "There isn't enough space to start OTA update");
client.flush(); client.flush();

View File

@ -67,6 +67,7 @@
// OTA settings // OTA settings
#define WIFI_MAX_TRY 20 // maximum number of wifi connect attempts for OTA update [default = 20] #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 // LMIC settings
// define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored