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;
bool ret = false;
while (i--) {
ESP_LOGI(TAG, "Trying to connect to %s", WIFI_SSID);
while (i--) {
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);

View File

@ -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