@@ -26,19 +26,38 @@ MultiSerialInterface interface_manager;
2626 // include nrf52 bluetooth interface
2727 #include < helpers/nrf52/SerialBLEInterface.h>
2828 SerialBLEInterface bluetooth_interface;
29+ #elif defined(RP2040_PLATFORM)
30+ // include rp2040 (Pico W / CYW43) bluetooth interface
31+ #include < helpers/rp2040/SerialBLEInterface.h>
32+ SerialBLEInterface bluetooth_interface;
2933 #else
3034 #error "SerialBLEInterface is not defined for this platform"
3135 #endif
3236#endif
3337
3438// include wifi interface
35- #ifdef WIFI_SSID
39+ #ifdef ENABLE_WIFI_INTERFACE
40+ #ifndef WIFI_SSID
41+ #define WIFI_SSID " "
42+ #endif
43+ #ifndef WIFI_PWD
44+ #define WIFI_PWD " "
45+ #endif
3646 #ifndef TCP_PORT
3747 #define TCP_PORT 5000
3848 #endif
39- #ifdef ESP32
40- // include esp32 wifi interface
41- #include < helpers/esp32/SerialWifiInterface.h>
49+ #ifndef WIFI_RETRY_INTERVAL
50+ #if defined(RP2040_PLATFORM)
51+ #define WIFI_RETRY_INTERVAL 30000 // each attempt blocks loop(), so retry less often
52+ #else
53+ #define WIFI_RETRY_INTERVAL 10000 // millis between reconnect attempts
54+ #endif
55+ #endif
56+ #ifndef WIFI_RETRY_TIMEOUT
57+ #define WIFI_RETRY_TIMEOUT 5000 // RP2040: cap on how long one join may block loop()
58+ #endif
59+ #if defined(ESP32) || defined(RP2040_PLATFORM)
60+ #include < helpers/wifi/SerialWifiInterface.h>
4261 SerialWifiInterface wifi_interface;
4362 #else
4463 #error "SerialWifiInterface is not defined for this platform"
@@ -108,9 +127,13 @@ void halt() {
108127}
109128
110129/* WIFI RECONNECT TRACKERS */
111- #if defined(ESP32) && defined(WIFI_SSID)
130+ #ifdef ENABLE_WIFI_INTERFACE
112131 bool wifi_needs_reconnect = false ;
113132 unsigned long last_wifi_reconnect_attempt = 0 ;
133+ char wifi_ssid[33 ] = WIFI_SSID ; // replaced by stored prefs at boot, if set
134+ char wifi_pwd[64 ] = WIFI_PWD ;
135+ bool wifi_was_connected = false ;
136+ bool wifi_enabled = false ; // set at boot from prefs; false also when the effective SSID is blank
114137#endif
115138
116139void setup () {
@@ -191,23 +214,46 @@ void setup() {
191214#endif
192215
193216// add wifi interface
194- #ifdef WIFI_SSID
195- board.setInhibitSleep (true ); // prevent sleep when WiFi is active
196- WiFi.setAutoReconnect (true );
197-
198- WiFi.onEvent ([](WiFiEvent_t event, WiFiEventInfo_t info){
199- if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED ) {
200- WIFI_DEBUG_PRINTLN (" WiFi disconnected. Flagging for reconnect..." );
201- wifi_needs_reconnect = true ;
202- } else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP ) {
203- WIFI_DEBUG_PRINTLN (" WiFi connected successfully!" );
204- wifi_needs_reconnect = false ;
205- }
206- });
217+ #ifdef ENABLE_WIFI_INTERFACE
218+ // use wifi ssid and password from prefs if ssid is not empty, otherwise use the build flag defaults
219+ if (the_mesh.getNodePrefs ()->wifi_ssid [0 ]) {
220+ strcpy (wifi_ssid, the_mesh.getNodePrefs ()->wifi_ssid );
221+ strcpy (wifi_pwd, the_mesh.getNodePrefs ()->wifi_pwd );
222+ }
223+ // only start wifi if enabled and ssid is not empty
224+ wifi_enabled = the_mesh.getNodePrefs ()->wifi_enabled ;
225+ if (wifi_enabled && wifi_ssid[0 ]) {
226+ #if defined(ESP32)
227+ board.setInhibitSleep (true ); // prevent sleep when WiFi is active
228+ WiFi.setAutoReconnect (true );
229+
230+ WiFi.onEvent ([](WiFiEvent_t event, WiFiEventInfo_t info){
231+ if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED ) {
232+ WIFI_DEBUG_PRINTLN (" WiFi disconnected. Flagging for reconnect..." );
233+ wifi_needs_reconnect = true ;
234+ } else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP ) {
235+ WIFI_DEBUG_PRINTLN (" WiFi connected successfully!" );
236+ wifi_needs_reconnect = false ;
237+ }
238+ });
239+ #endif
207240
208- WiFi.begin (WIFI_SSID , WIFI_PWD );
209- wifi_interface.begin (TCP_PORT );
210- interface_manager.addInterface (InterfaceType::WiFi, &wifi_interface);
241+ WIFI_DEBUG_PRINTLN (" connecting to %s" , wifi_ssid);
242+
243+ #if defined(RP2040_PLATFORM)
244+ // the join itself blocks inside the core (CYW43::begin busy-waits for the
245+ // association), so every attempt stalls the mesh loop. beginNoBlock() only skips the
246+ // extra DHCP wait. Give the first connect a full window, then bound the retries below.
247+ WiFi.beginNoBlock (wifi_ssid, wifi_pwd);
248+ last_wifi_reconnect_attempt = millis (); // let DHCP finish before the poll can retry
249+ #else
250+ WiFi.begin (wifi_ssid, wifi_pwd);
251+ #endif
252+ wifi_interface.begin (TCP_PORT );
253+ interface_manager.addInterface (InterfaceType::WiFi, &wifi_interface);
254+ } else {
255+ WIFI_DEBUG_PRINTLN (" wifi disabled" );
256+ }
211257#endif
212258
213259// add usb interface
@@ -262,13 +308,34 @@ void loop() {
262308#endif
263309 }
264310
265- #if defined(ESP32) && defined(WIFI_SSID)
266- // Safely attempt to reconnect every 10 seconds if flagged
267- if (wifi_needs_reconnect && (millis () - last_wifi_reconnect_attempt > 10000 )) {
268- WIFI_DEBUG_PRINTLN (" Attempting manual WiFi reconnect..." );
269- WiFi.disconnect ();
270- WiFi.reconnect ();
271- last_wifi_reconnect_attempt = millis ();
311+ #ifdef ENABLE_WIFI_INTERFACE
312+ if (wifi_enabled) {
313+ // RP2040 has no WiFi event callbacks, so poll the link state instead
314+ #if defined(RP2040_PLATFORM)
315+ wifi_needs_reconnect = (WiFi.status () != WL_CONNECTED );
316+ if (wifi_was_connected == wifi_needs_reconnect) { // link state changed
317+ wifi_was_connected = !wifi_needs_reconnect;
318+ if (wifi_was_connected) {
319+ WIFI_DEBUG_PRINTLN (" connected, listening on %s:%d" , WiFi.localIP ().toString ().c_str (), TCP_PORT );
320+ } else {
321+ WIFI_DEBUG_PRINTLN (" link lost" );
322+ }
323+ }
324+ #endif
325+
326+ // Safely attempt to reconnect if flagged. On RP2040 each attempt blocks the mesh loop
327+ // for up to WIFI_RETRY_TIMEOUT, so retry less often and cap how long a join may stall.
328+ if (wifi_needs_reconnect && (millis () - last_wifi_reconnect_attempt > WIFI_RETRY_INTERVAL )) {
329+ WIFI_DEBUG_PRINTLN (" Attempting manual WiFi reconnect to %s (status %d)..." , wifi_ssid, WiFi.status ());
330+ #if defined(RP2040_PLATFORM)
331+ WiFi.setTimeout (WIFI_RETRY_TIMEOUT );
332+ WiFi.beginNoBlock (wifi_ssid, wifi_pwd); // no reconnect() on this platform
333+ #else
334+ WiFi.disconnect ();
335+ WiFi.reconnect ();
336+ #endif
337+ last_wifi_reconnect_attempt = millis ();
338+ }
272339 }
273340#endif
274341}
0 commit comments