@@ -102,6 +102,7 @@ void wifiSetState(byte newState)
102102 if (wifiState == newState)
103103 systemPrint (" *" );
104104 wifiState = newState;
105+
105106 switch (newState)
106107 {
107108 default :
@@ -156,12 +157,7 @@ void wifiStartAP()
156157 else
157158 {
158159 // Start webserver on local WiFi instead of AP
159- WiFi.mode (WIFI_STA);
160-
161- #ifdef COMPILE_ESPNOW
162- // Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
163- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Stops WiFi Station
164- #endif
160+ wifiStart (); // Makes sure any ESP-Now settings have been cleared
165161
166162 // Attempt to connect to local WiFi 3 times with increasing timeouts
167163 int timeout = 0 ;
@@ -215,6 +211,12 @@ void wifiUpdate()
215211 if (wifiConnect (10000 ) == true ) // Attempt to connect to any SSID on settings list
216212 {
217213
214+ if (espnowState > ESPNOW_OFF)
215+ {
216+ // esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); //Enable WiFi + ESP-Now. Stops WiFi Station.
217+ espnowStart ();
218+ }
219+
218220 wifiSetState (WIFI_CONNECTED);
219221 }
220222 else
@@ -243,7 +245,7 @@ void wifiUpdate()
243245 // Verify link is still up
244246 if (wifiIsConnected () == false )
245247 {
246- log_d (" WiFi link lost" );
248+ systemPrintln (" WiFi link lost" );
247249 wifiConnectionAttempts = 0 ; // Reset the timeout
248250 wifiSetState (WIFI_CONNECTING);
249251 }
@@ -268,48 +270,31 @@ void wifiUpdate()
268270void wifiStart ()
269271{
270272#ifdef COMPILE_WIFI
271- #ifdef COMPILE_ESPNOW
272- bool restartESPNow = false ;
273- #endif
274-
275- if (wifiIsConnected () == true ) return ; // We don't need to do anything
276-
277- if (wifiState > WIFI_OFF) return ; // We're in the midst of connecting
278-
279- log_d (" Starting WiFi" );
280-
281273 if (wifiNetworkCount () == 0 )
282274 {
283275 systemPrintln (" Error: Please enter at least one SSID before using WiFi" );
284276 // paintWiFiNoNetworksFail(4000, true); //TODO
285- wifiSetState (WIFI_OFF );
277+ wifiStop ( );
286278 return ;
287279 }
288280
289- wifiSetState (WIFI_CONNECTING); // This starts the state machine running
281+ if (wifiIsConnected () == true ) return ; // We don't need to do anything
282+
283+ if (wifiState > WIFI_OFF) return ; // We're in the midst of connecting
284+
285+ log_d (" Starting WiFi" );
290286
291287 WiFi.mode (WIFI_STA);
292288
293- #ifdef COMPILE_ESPNOW
289+ // Before attempting WiFiMulti connect, be sure we have default WiFi protocols enabled.
290+ // This must come after WiFi.mode
291+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
292+
293+ // If ESP-NOW is running, blend in ESP-NOW protocol.
294294 if (espnowState > ESPNOW_OFF)
295- {
296- restartESPNow = true ;
297- espnowStop ();
298295 esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); // Enable WiFi + ESP-Now. Stops WiFi Station.
299- }
300- else
301- {
302- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Set basic WiFi protocols. Stops WiFi Station.
303- }
304- #else
305- // Be sure the standard protocols are turned on. ESP Now may have previously turned them off.
306- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Set basic WiFi protocols. Stops WiFi Station.
307- #endif
308296
309- #ifdef COMPILE_ESPNOW
310- if (restartESPNow == true )
311- espnowStart ();
312- #endif
297+ wifiSetState (WIFI_CONNECTING); // This starts the state machine running
313298
314299 // Display the heap state
315300 reportHeapNow ();
@@ -348,28 +333,21 @@ void wifiStop()
348333
349334 wifiConnectionAttempts = 0 ; // Reset the timeout
350335
336+ // No matter what, turn off WiFi to stop rolling STA_DISCONNECTED and AUTH_EXPIRE notifications
337+ WiFi.mode (WIFI_OFF);
338+ log_d (" WiFi Stopped" );
339+
351340#ifdef COMPILE_ESPNOW
352- // If WiFi is on but ESP NOW is off, then turn off radio entirely
353- if (espnowState == ESPNOW_OFF)
354- {
355- WiFi.mode (WIFI_OFF);
356- log_d (" WiFi Stopped" );
357- }
358- // If ESP-Now is active, change protocol to only Long Range
359- else if (espnowState > ESPNOW_OFF)
341+ // If ESP-Now is active, change protocol to only Long Range and re-start WiFi
342+ if (espnowState > ESPNOW_OFF)
360343 {
361-
362344 // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
363345 esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR); // Stops WiFi Station.
364346
365347 WiFi.mode (WIFI_STA);
366348
367349 log_d (" WiFi disabled, ESP-Now left in place" );
368350 }
369- #else
370- // Turn off radio
371- WiFi.mode (WIFI_OFF);
372- log_d (" WiFi Stopped" );
373351#endif
374352
375353 // Display the heap state
@@ -422,11 +400,6 @@ bool wifiConnect(unsigned long timeout)
422400 else
423401 systemPrintf (" WiFi failed to connect: error #%d. " , response);
424402
425- if (wifiConnectionAttemptTimeout / 1000 < 120 )
426- systemPrintf (" Next WiFi attempt in %d seconds.\r\n " , wifiConnectionAttemptTimeout / 1000 );
427- else
428- systemPrintf (" Next WiFi attempt in %d minutes.\r\n " , wifiConnectionAttemptTimeout / 1000 / 60 );
429-
430403#endif
431404
432405 return false ;
0 commit comments