From efed8bee5c268b7df4ab79a52f987897f270b1b1 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Mon, 20 Nov 2023 18:59:33 +0100 Subject: [PATCH] WiFi reconnect improvement --- data | 2 +- src/lib/config.cpp | 28 ++++++++++++++++++++++------ src/lib/config.hpp | 8 +------- src/lib/led_handler.cpp | 2 +- src/main.cpp | 4 ++++ 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/data b/data index 3eaf897..d25284e 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 3eaf897dbb6d1597ec71b4323ae0d68a02dee9c9 +Subproject commit d25284e3a47a9efe6c0a8877e8abeac098ced8af diff --git a/src/lib/config.cpp b/src/lib/config.cpp index 82b8b50..7d427b3 100644 --- a/src/lib/config.cpp +++ b/src/lib/config.cpp @@ -49,7 +49,7 @@ void setup() void tryImprovSetup() { - //WiFi.onEvent(WiFiEvent); + WiFi.onEvent(WiFiEvent); if (!preferences.getBool("wifiConfigured", false)) { @@ -228,7 +228,8 @@ void setupHardware() { Serial.println(F("An Error has occurred while mounting LittleFS")); } - if(!LittleFS.open("/index.html", "r")) { + + if(!LittleFS.open("/index.html.gz", "r")) { Serial.println("Error loading WebUI"); } @@ -474,8 +475,10 @@ void improv_set_error(improv::Error error) Serial.write(data.data(), data.size()); } -void WiFiEvent(WiFiEvent_t event) +void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { + static bool first_connect = true; + Serial.printf("[WiFi-event] event: %d\n", event); switch (event) @@ -496,19 +499,32 @@ void WiFiEvent(WiFiEvent_t event) Serial.println("Connected to access point"); break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - Serial.println("Disconnected from WiFi access point"); - queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); - break; + { + if (!first_connect) { + Serial.println("Disconnected from WiFi access point"); + queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + uint8_t reason = info.wifi_sta_disconnected.reason; + if(reason) + Serial.printf("Disconnect reason: %s, ", WiFi.disconnectReasonName((wifi_err_reason_t)reason)); + } + break; + } case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: Serial.println("Authentication mode of access point has changed"); break; case ARDUINO_EVENT_WIFI_STA_GOT_IP: + { Serial.print("Obtained IP address: "); Serial.println(WiFi.localIP()); + if (!first_connect) + queueLedEffect(LED_EFFECT_WIFI_CONNECT_SUCCESS); + first_connect = false; break; + } case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println("Lost IP address and IP address is reset to 0"); queueLedEffect(LED_EFFECT_WIFI_CONNECT_ERROR); + WiFi.reconnect(); break; case ARDUINO_EVENT_WIFI_AP_START: Serial.println("WiFi access point started"); diff --git a/src/lib/config.hpp b/src/lib/config.hpp index 4b10147..12c9809 100644 --- a/src/lib/config.hpp +++ b/src/lib/config.hpp @@ -22,7 +22,6 @@ #include "lib/button_handler.hpp" #include "lib/led_handler.hpp" - #define NTP_SERVER "pool.ntp.org" #define DEFAULT_MEMPOOL_INSTANCE "mempool.space" #define TIME_OFFSET_SECONDS 3600 @@ -34,11 +33,6 @@ #define DEFAULT_FG_COLOR GxEPD_WHITE #define DEFAULT_BG_COLOR GxEPD_BLACK -#define BITCOIND_HOST "" -#define BITCOIND_PORT 8332 -#define BITCOIND_RPC_USER "" -#define BITCOIND_RPC_PASS "" - void setup(); void setupTime(); void setupPreferences(); @@ -58,4 +52,4 @@ void improv_set_state(improv::State state); void improv_send_response(std::vector &response); void improv_set_error(improv::Error error); -void WiFiEvent(WiFiEvent_t event); \ No newline at end of file +void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); \ No newline at end of file diff --git a/src/lib/led_handler.cpp b/src/lib/led_handler.cpp index af6c4e0..429a1d6 100644 --- a/src/lib/led_handler.cpp +++ b/src/lib/led_handler.cpp @@ -28,7 +28,7 @@ void ledTask(void *parameter) pixels.clear(); break; case LED_EFFECT_WIFI_CONNECT_ERROR: - blinkDelayTwoColor(100, 1, pixels.Color(8, 161, 236), pixels.Color(255, 0, 0)); + blinkDelayTwoColor(100, 3, pixels.Color(8, 161, 236), pixels.Color(255, 0, 0)); break; case LED_FLASH_ERROR: blinkDelayColor(250, 3, 255, 0, 0); diff --git a/src/main.cpp b/src/main.cpp index 9d4b5e2..fc65c36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,10 @@ extern "C" void app_main() if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle); + if (!WiFi.isConnected()) { + WiFi.begin(); + } + vTaskDelay(pdMS_TO_TICKS(5000)); } } \ No newline at end of file