From 2854f2dd13a2b4c53c92548ebaf24aa07f91c8be Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 23 Mar 2021 23:09:52 +0100 Subject: [PATCH] ota.cpp: stabilize and speedup wifi login --- src/ota.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ota.cpp b/src/ota.cpp index db02d04f..4c8b7140 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -21,7 +21,8 @@ using namespace std; -const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, PAXEXPRESS_PACKAGE); +const BintrayClient paxexpress(PAXEXPRESS_USER, PAXEXPRESS_REPO, + PAXEXPRESS_PACKAGE); // usage of paxexpress: see https://github.com/paxexpress/docs // Connection port (HTTPS) @@ -41,6 +42,15 @@ inline String getHeaderValue(String header, String headerName) { void start_ota_update() { + uint8_t mac[6]; + char clientId[20]; + + // hash 6 byte MAC to 4 byte hash + esp_eth_get_mac(mac); + const uint32_t hashedmac = myhash((const char *)mac, 6); + snprintf(clientId, 20, "paxcounter_%08x", hashedmac); + const char *host = clientId; + switch_LED(LED_ON); // init display @@ -65,8 +75,28 @@ void start_ota_update() { ESP_LOGI(TAG, "Starting Wifi OTA update"); ota_display(1, "**", WIFI_SSID); + WiFi.disconnect(true); + WiFi.config(INADDR_NONE, INADDR_NONE, + INADDR_NONE); // call is only a workaround for bug in WiFi class + // see https://github.com/espressif/arduino-esp32/issues/806 + WiFi.setHostname(host); WiFi.mode(WIFI_STA); + WiFi.begin(); + + // Connect to WiFi network + // workaround applied here to bypass WIFI_AUTH failure + // see https://github.com/espressif/arduino-esp32/issues/2501 + + // 1st try WiFi.begin(WIFI_SSID, WIFI_PASS); + while (WiFi.status() == WL_DISCONNECTED) { + delay(500); + } + // 2nd try + if (WiFi.status() != WL_CONNECTED) { + WiFi.begin(WIFI_SSID, WIFI_PASS); + delay(500); + } uint8_t i = WIFI_MAX_TRY; int ret = 1; // 0 = finished, 1 = retry, -1 = abort @@ -74,7 +104,6 @@ void start_ota_update() { while (i--) { ESP_LOGI(TAG, "Trying to connect to %s, attempt %u of %u", WIFI_SSID, WIFI_MAX_TRY - i, WIFI_MAX_TRY); - delay(10000); // wait for stable connect 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); @@ -89,6 +118,7 @@ void start_ota_update() { if (WiFi.status() == WL_CONNECTED) goto end; // OTA update finished or OTA max attemps reached } + delay(10000); // wait for stable connect WiFi.reconnect(); }