|
38 | 38 | #include <ETH.h> |
39 | 39 | #endif |
40 | 40 | #include <Update.h> |
| 41 | + #include <atomic> |
41 | 42 | #elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_RP2350) |
42 | 43 | #include <WiFi.h> |
43 | 44 | #include <LEAmDNS.h> |
@@ -90,6 +91,46 @@ bool isAPMode() { |
90 | 91 | return inAPMode; |
91 | 92 | } |
92 | 93 |
|
| 94 | +#if defined(ARDUINO_ARCH_ESP32) |
| 95 | + namespace WifiRecovery{ |
| 96 | + constexpr unsigned long WIFI_RECOVERY_DELAY = 10000UL; |
| 97 | + |
| 98 | + enum class WifiEvent : uint8_t { NONE = 0, ASSOC_LEAVE, STA_CONNECTED}; |
| 99 | + std::atomic<WifiEvent> wifiEvent{WifiEvent::NONE}; |
| 100 | + |
| 101 | + bool wifiRecoveryPending = false; |
| 102 | + unsigned long wifiRecoveryStarted = 0UL; |
| 103 | + |
| 104 | + void processWiFiRecovery() { |
| 105 | + switch (wifiEvent.exchange(WifiEvent::NONE, std::memory_order_relaxed)) { |
| 106 | + case WifiEvent::ASSOC_LEAVE: |
| 107 | + if (!wifiRecoveryPending) { |
| 108 | + wifiRecoveryPending = true; |
| 109 | + wifiRecoveryStarted = millis(); |
| 110 | + Log::SERIAL_LOG("[WiFi] ASSOC_LEAVE detected, recovery watchdog armed"); |
| 111 | + } |
| 112 | + break; |
| 113 | + |
| 114 | + case WifiEvent::STA_CONNECTED: |
| 115 | + wifiRecoveryPending = false; |
| 116 | + Log::SERIAL_LOG("[WiFi] STA_CONNECTED detected, disabling recovery watchdog"); |
| 117 | + break; |
| 118 | + |
| 119 | + default: |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + if (wifiRecoveryPending && millis() - wifiRecoveryStarted >= WIFI_RECOVERY_DELAY) { |
| 124 | + wifiRecoveryPending = false; |
| 125 | + |
| 126 | + if (WiFi.status() != WL_CONNECTED) { |
| 127 | + Log::SERIAL_LOG("[WiFi] Recovery timeout, reconnect request: ", WiFi.reconnect() ? "accepted" : "failed"); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +#endif |
| 133 | + |
93 | 134 | void setup() { |
94 | 135 | #if defined(ARDUINO_ARCH_ESP32) |
95 | 136 | LittleFS.begin(true); |
@@ -156,6 +197,21 @@ void setup() { |
156 | 197 | { |
157 | 198 | Log::SERIAL_LOG("Connected → ", WiFi.localIP()); |
158 | 199 | } |
| 200 | + |
| 201 | + #if defined(ARDUINO_ARCH_ESP32) |
| 202 | + // register recovery procedure if not in AP/ethernet mode |
| 203 | + if (!inAPMode) { |
| 204 | + WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) { |
| 205 | + if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED && info.wifi_sta_disconnected.reason == WIFI_REASON_ASSOC_LEAVE) { |
| 206 | + WifiRecovery::wifiEvent.store(WifiRecovery::WifiEvent::ASSOC_LEAVE, std::memory_order_relaxed); |
| 207 | + } |
| 208 | + else if (event == ARDUINO_EVENT_WIFI_STA_CONNECTED) { |
| 209 | + WifiRecovery::wifiEvent.store(WifiRecovery::WifiEvent::STA_CONNECTED, std::memory_order_relaxed); |
| 210 | + } |
| 211 | + }); |
| 212 | + Log::SERIAL_LOG("Registered recovery procedure"); |
| 213 | + } |
| 214 | + #endif |
159 | 215 | } |
160 | 216 |
|
161 | 217 | if (!inAPMode) { |
@@ -206,6 +262,11 @@ void loop() |
206 | 262 | if (!inAPMode) { |
207 | 263 | MDNS.update(); |
208 | 264 | } |
| 265 | + #elif defined(ARDUINO_ARCH_ESP32) |
| 266 | + if (!inAPMode && !hasEthernet) { |
| 267 | + // wifi recovery routine |
| 268 | + WifiRecovery::processWiFiRecovery(); |
| 269 | + } |
209 | 270 | #endif |
210 | 271 |
|
211 | 272 |
|
|
0 commit comments