mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-08-02 10:16:11 +00:00
Refactor WiFi auto-reconnect to use non-blocking polling
This commit is contained in:
@@ -105,6 +105,12 @@ void halt() {
|
|||||||
while (1) ;
|
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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
@@ -195,6 +201,18 @@ void setup() {
|
|||||||
|
|
||||||
#ifdef WIFI_SSID
|
#ifdef WIFI_SSID
|
||||||
board.setInhibitSleep(true); // prevent sleep when WiFi is active
|
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);
|
WiFi.begin(WIFI_SSID, WIFI_PWD);
|
||||||
serial_interface.begin(TCP_PORT);
|
serial_interface.begin(TCP_PORT);
|
||||||
#elif defined(BLE_PIN_CODE)
|
#elif defined(BLE_PIN_CODE)
|
||||||
@@ -229,4 +247,14 @@ void loop() {
|
|||||||
ui_task.loop();
|
ui_task.loop();
|
||||||
#endif
|
#endif
|
||||||
rtc_clock.tick();
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user