@@ -135,13 +135,17 @@ bool wifiStartAP()
135135 {
136136 // Stop any current WiFi activity
137137 wifiStop ();
138-
138+
139139 // Start in AP mode
140140 WiFi.mode (WIFI_AP);
141141
142- // Before attempting WiFiMulti connect, be sure we have default WiFi protocols enabled.
143- // This must come after WiFi.mode
144- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
142+ // Before starting AP mode, be sure we have default WiFi protocols enabled.
143+ // esp_wifi_set_protocol requires WiFi to be started
144+ esp_err_t response = esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
145+ if (response != ESP_OK)
146+ systemPrintf (" wifiStartAP: Error setting WiFi protocols: %s\r\n " , esp_err_to_name (response));
147+ else
148+ log_d (" WiFi protocols set" );
145149
146150 IPAddress local_IP (192 , 168 , 4 , 1 );
147151 IPAddress gateway (192 , 168 , 4 , 1 );
@@ -159,8 +163,6 @@ bool wifiStartAP()
159163 else
160164 {
161165 // Start webserver on local WiFi instead of AP
162- log_d (" WiFi Config starting WiFi" );
163- wifiStart (); // Makes sure any ESP-Now settings have been cleared
164166
165167 // Attempt to connect to local WiFi with increasing timeouts
166168 int timeout = 0 ;
@@ -179,11 +181,11 @@ bool wifiStartAP()
179181 {
180182 displayNoWiFi (2000 );
181183 requestChangeState (STATE_ROVER_NOT_STARTED);
182- return (false );
184+ return (false );
183185 }
184186 }
185187
186- return (true );
188+ return (true );
187189}
188190
189191#endif // COMPILE_WIFI
@@ -281,7 +283,7 @@ void wifiStart()
281283 if (wifiNetworkCount () == 0 )
282284 {
283285 systemPrintln (" Error: Please enter at least one SSID before using WiFi" );
284- // paintWiFiNoNetworksFail(4000, true ); //TODO
286+ // paintNoWiFi(2000 ); //TODO
285287 wifiStop ();
286288 return ;
287289 }
@@ -292,16 +294,6 @@ void wifiStart()
292294
293295 log_d (" Starting WiFi" );
294296
295- WiFi.mode (WIFI_STA);
296-
297- // Before attempting WiFiMulti connect, be sure we have default WiFi protocols enabled.
298- // This must come after WiFi.mode
299- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
300-
301- // If ESP-NOW is running, blend in ESP-NOW protocol.
302- if (espnowState > ESPNOW_OFF)
303- 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.
304-
305297 wifiSetState (WIFI_CONNECTING); // This starts the state machine running
306298
307299 // Display the heap state
@@ -341,19 +333,25 @@ void wifiStop()
341333
342334 wifiConnectionAttempts = 0 ; // Reset the timeout
343335
344- // No matter what, turn off WiFi to stop rolling STA_DISCONNECTED and AUTH_EXPIRE notifications
345- WiFi.mode (WIFI_OFF);
346- log_d (" WiFi Stopped" );
347336
348337 // If ESP-Now is active, change protocol to only Long Range and re-start WiFi
349338 if (espnowState > ESPNOW_OFF)
350339 {
351- // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
352- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR); // Stops WiFi Station.
353-
354- WiFi.mode (WIFI_STA);
355-
356- log_d (" WiFi disabled, ESP-Now left in place" );
340+ if (WiFi.getMode () == WIFI_OFF)
341+ WiFi.mode (WIFI_STA);
342+
343+ // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
344+ // esp_wifi_set_protocol requires WiFi to be started
345+ esp_err_t response = esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR);
346+ if (response != ESP_OK)
347+ systemPrintf (" wifiStop: Error setting ESP-Now lone protocol: %s\r\n " , esp_err_to_name (response));
348+ else
349+ log_d (" WiFi disabled, ESP-Now left in place" );
350+ }
351+ else
352+ {
353+ WiFi.mode (WIFI_OFF);
354+ log_d (" WiFi Stopped" );
357355 }
358356
359357 // Display the heap state
@@ -381,8 +379,38 @@ bool wifiConnect(unsigned long timeout)
381379{
382380#ifdef COMPILE_WIFI
383381
384- if (wifiIsConnected ())
385- return (true );
382+ if (wifiIsConnected ()) return (true ); // Nothing to do
383+
384+ // Before we can issue esp_wifi_() commands WiFi must be started
385+ if (WiFi.getMode () == WIFI_OFF)
386+ WiFi.mode (WIFI_STA);
387+
388+ // Verify that the necessary protocols are set
389+ uint8_t protocols = 0 ;
390+ esp_err_t response = esp_wifi_get_protocol (WIFI_IF_STA, &protocols);
391+ if (response != ESP_OK)
392+ systemPrintf (" wifiConnect: Failed to get protocols: %s\r\n " , esp_err_to_name (response));
393+
394+ // If ESP-NOW is running, blend in ESP-NOW protocol.
395+ if (espnowState > ESPNOW_OFF)
396+ {
397+ if (protocols != (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR))
398+ {
399+ esp_err_t response = esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); // Enable WiFi + ESP-Now.
400+ if (response != ESP_OK)
401+ systemPrintf (" wifiConnect: Error setting WiFi + ESP-NOW protocols: %s\r\n " , esp_err_to_name (response));
402+ }
403+ }
404+ else
405+ {
406+ // Make sure default WiFi protocols are in place
407+ if (protocols != (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N))
408+ {
409+ esp_err_t response = esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Enable WiFi.
410+ if (response != ESP_OK)
411+ systemPrintf (" wifiConnect: Error setting WiFi + ESP-NOW protocols: %s\r\n " , esp_err_to_name (response));
412+ }
413+ }
386414
387415 WiFiMulti wifiMulti;
388416
@@ -395,16 +423,16 @@ bool wifiConnect(unsigned long timeout)
395423
396424 systemPrint (" Connecting WiFi... " );
397425
398- int response = wifiMulti.run (timeout);
399- if (response == WL_CONNECTED)
426+ int wifiResponse = wifiMulti.run (timeout);
427+ if (wifiResponse == WL_CONNECTED)
400428 {
401429 systemPrintln ();
402430 return true ;
403431 }
404- else if (response == WL_DISCONNECTED)
432+ else if (wifiResponse == WL_DISCONNECTED)
405433 systemPrint (" No friendly WiFi networks detected. " );
406434 else
407- systemPrintf (" WiFi failed to connect: error #%d. " , response );
435+ systemPrintf (" WiFi failed to connect: error #%d. " , wifiResponse );
408436
409437#endif
410438
@@ -600,6 +628,8 @@ void tcpUpdate()
600628
601629 if (settings.enableTcpClient == false && settings.enableTcpServer == false ) return ; // Nothing to do
602630
631+ if (wifiInConfigMode ()) return ; // Do not service TCP during WiFi config
632+
603633 // If TCP is enabled, but WiFi is not connected, start WiFi
604634 if (wifiIsConnected () == false && (settings.enableTcpClient == true || settings.enableTcpServer == true ))
605635 {
0 commit comments