Browse Source

Merge pull request #2493 from Rococo88/complex-wifi-reconnect

Refactor WiFi auto-reconnect to use non-blocking polling
pull/2565/head
Liam Cottle 3 weeks ago
committed by GitHub
parent
commit
6325a85336
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      examples/companion_radio/main.cpp

28
examples/companion_radio/main.cpp

@ -105,6 +105,12 @@ void halt() {
while (1) ;
}
/* WIFI RECONNECT TRACKERS */
#if defined(ESP32) && defined(WIFI_SSID)
bool wifi_needs_reconnect = false;
unsigned long last_wifi_reconnect_attempt = 0;
#endif
void setup() {
Serial.begin(115200);
@ -195,6 +201,18 @@ void setup() {
#ifdef WIFI_SSID
board.setInhibitSleep(true); // prevent sleep when WiFi is active
WiFi.setAutoReconnect(true);
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
WIFI_DEBUG_PRINTLN("WiFi disconnected. Flagging for reconnect...");
wifi_needs_reconnect = true;
} else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
WIFI_DEBUG_PRINTLN("WiFi connected successfully!");
wifi_needs_reconnect = false;
}
});
WiFi.begin(WIFI_SSID, WIFI_PWD);
serial_interface.begin(TCP_PORT);
#elif defined(BLE_PIN_CODE)
@ -229,4 +247,14 @@ void loop() {
ui_task.loop();
#endif
rtc_clock.tick();
#if defined(ESP32) && defined(WIFI_SSID)
// Safely attempt to reconnect every 10 seconds if flagged
if (wifi_needs_reconnect && (millis() - last_wifi_reconnect_attempt > 10000)) {
WIFI_DEBUG_PRINTLN("Attempting manual WiFi reconnect...");
WiFi.disconnect();
WiFi.reconnect();
last_wifi_reconnect_attempt = millis();
}
#endif
}

Loading…
Cancel
Save