From cbc846d30b40a856d5b9956d83bb479538869df2 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Mon, 5 Nov 2018 00:03:47 +0100 Subject: [PATCH] ota.cpp: fixed timeout --- src/ota.cpp | 16 +++++++++------- src/paxcounter.conf | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ota.cpp b/src/ota.cpp index 60c07dca..2e406a42 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -77,8 +77,8 @@ void start_ota_update() { int i = WIFI_MAX_TRY, j = OTA_MAX_TRY; bool ret = false; + ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID); while (i--) { - ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID); if (WiFi.status() == WL_CONNECTED) { // we now have wifi connection and try to do an OTA over wifi update ESP_LOGI(TAG, "Connected to %s", WIFI_SSID); @@ -91,6 +91,7 @@ void start_ota_update() { } goto end; } + vTaskDelay(5000 / portTICK_PERIOD_MS); } // wifi did not connect @@ -142,8 +143,7 @@ bool do_ota_update() { String prevHost = currentHost; WiFiClientSecure client; - // set server connection timeout and open server connection - client.setTimeout(RESPONSE_TIMEOUT_MS); + client.setCACert(bintray.getCertificate(currentHost)); if (!client.connect(currentHost.c_str(), port)) { @@ -151,11 +151,11 @@ bool do_ota_update() { display(3, " E", "connection lost"); goto failure; } + // client.setTimeout(RESPONSE_TIMEOUT); while (redirect) { if (currentHost != prevHost) { client.stop(); - client.setTimeout(RESPONSE_TIMEOUT_MS); client.setCACert(bintray.getCertificate(currentHost)); if (!client.connect(currentHost.c_str(), port)) { ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s", @@ -163,6 +163,7 @@ bool do_ota_update() { display(3, " E", "server error"); goto failure; } + // client.setTimeout(RESPONSE_TIMEOUT); } ESP_LOGI(TAG, "Requesting %s", firmwarePath.c_str()); @@ -174,7 +175,7 @@ bool do_ota_update() { unsigned long timeout = millis(); while (client.available() == 0) { - if (millis() - timeout > RESPONSE_TIMEOUT_MS) { + if ((millis() - timeout) > (RESPONSE_TIMEOUT * 1000)) { ESP_LOGI(TAG, "Client timeout"); display(3, " E", "client timeout"); goto failure; @@ -257,6 +258,7 @@ bool do_ota_update() { display(4, "**", "writing..."); written = Update.writeStream(client); + client.setTimeout(RESPONSE_TIMEOUT); if (written == contentLength) { ESP_LOGI(TAG, "Written %u bytes successfully", written); @@ -270,8 +272,8 @@ bool do_ota_update() { if (Update.end()) { goto finished; } else { - ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); - snprintf(buf, 17, "Error #: %d", Update.getError()); + ESP_LOGI(TAG, "An error occurred. Error#: %d", Update.getError()); + snprintf(buf, 17, "Error#: %d", Update.getError()); display(4, " E", buf); goto failure; } diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 1069b8be..5c47a955 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -68,9 +68,9 @@ // OTA settings #define USE_OTA 1 // Comment out to disable OTA update #define WIFI_MAX_TRY 5 // maximum number of wifi connect attempts for OTA update [default = 20] -#define OTA_MAX_TRY 3 // maximum number of attempts for OTA download and write to flash [default = 3] +#define OTA_MAX_TRY 5 // maximum number of attempts for OTA download and write to flash [default = 3] #define OTA_MIN_BATT 3700 // minimum battery level for OTA [millivolt] -#define RESPONSE_TIMEOUT_MS 30000 // firmware binary server connection timeout [milliseconds] +#define RESPONSE_TIMEOUT 60 // firmware binary server connection timeout [seconds] // LMIC settings // moved to src/lmic_config.h \ No newline at end of file