ota.cpp: fixed timeout

This commit is contained in:
Klaus K Wilting 2018-11-05 00:03:47 +01:00
parent 26f4bd8f09
commit cbc846d30b
2 changed files with 11 additions and 9 deletions

View File

@ -77,8 +77,8 @@ void start_ota_update() {
int i = WIFI_MAX_TRY, j = OTA_MAX_TRY; int i = WIFI_MAX_TRY, j = OTA_MAX_TRY;
bool ret = false; bool ret = false;
while (i--) {
ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID); ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID);
while (i--) {
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
// we now have wifi connection and try to do an OTA over wifi update // we now have wifi connection and try to do an OTA over wifi update
ESP_LOGI(TAG, "Connected to %s", WIFI_SSID); ESP_LOGI(TAG, "Connected to %s", WIFI_SSID);
@ -91,6 +91,7 @@ void start_ota_update() {
} }
goto end; goto end;
} }
vTaskDelay(5000 / portTICK_PERIOD_MS);
} }
// wifi did not connect // wifi did not connect
@ -142,8 +143,7 @@ bool do_ota_update() {
String prevHost = currentHost; String prevHost = currentHost;
WiFiClientSecure client; WiFiClientSecure client;
// set server connection timeout and open server connection
client.setTimeout(RESPONSE_TIMEOUT_MS);
client.setCACert(bintray.getCertificate(currentHost)); client.setCACert(bintray.getCertificate(currentHost));
if (!client.connect(currentHost.c_str(), port)) { if (!client.connect(currentHost.c_str(), port)) {
@ -151,11 +151,11 @@ bool do_ota_update() {
display(3, " E", "connection lost"); display(3, " E", "connection lost");
goto failure; goto failure;
} }
// client.setTimeout(RESPONSE_TIMEOUT);
while (redirect) { while (redirect) {
if (currentHost != prevHost) { if (currentHost != prevHost) {
client.stop(); client.stop();
client.setTimeout(RESPONSE_TIMEOUT_MS);
client.setCACert(bintray.getCertificate(currentHost)); client.setCACert(bintray.getCertificate(currentHost));
if (!client.connect(currentHost.c_str(), port)) { if (!client.connect(currentHost.c_str(), port)) {
ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s", ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s",
@ -163,6 +163,7 @@ bool do_ota_update() {
display(3, " E", "server error"); display(3, " E", "server error");
goto failure; goto failure;
} }
// client.setTimeout(RESPONSE_TIMEOUT);
} }
ESP_LOGI(TAG, "Requesting %s", firmwarePath.c_str()); ESP_LOGI(TAG, "Requesting %s", firmwarePath.c_str());
@ -174,7 +175,7 @@ bool do_ota_update() {
unsigned long timeout = millis(); unsigned long timeout = millis();
while (client.available() == 0) { while (client.available() == 0) {
if (millis() - timeout > RESPONSE_TIMEOUT_MS) { if ((millis() - timeout) > (RESPONSE_TIMEOUT * 1000)) {
ESP_LOGI(TAG, "Client timeout"); ESP_LOGI(TAG, "Client timeout");
display(3, " E", "client timeout"); display(3, " E", "client timeout");
goto failure; goto failure;
@ -257,6 +258,7 @@ bool do_ota_update() {
display(4, "**", "writing..."); display(4, "**", "writing...");
written = Update.writeStream(client); written = Update.writeStream(client);
client.setTimeout(RESPONSE_TIMEOUT);
if (written == contentLength) { if (written == contentLength) {
ESP_LOGI(TAG, "Written %u bytes successfully", written); ESP_LOGI(TAG, "Written %u bytes successfully", written);
@ -270,8 +272,8 @@ bool do_ota_update() {
if (Update.end()) { if (Update.end()) {
goto finished; goto finished;
} else { } else {
ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); ESP_LOGI(TAG, "An error occurred. Error#: %d", Update.getError());
snprintf(buf, 17, "Error #: %d", Update.getError()); snprintf(buf, 17, "Error#: %d", Update.getError());
display(4, " E", buf); display(4, " E", buf);
goto failure; goto failure;
} }

View File

@ -68,9 +68,9 @@
// OTA settings // OTA settings
#define USE_OTA 1 // Comment out to disable OTA update #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 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 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 // LMIC settings
// moved to src/lmic_config.h // moved to src/lmic_config.h