@@ -145,13 +145,14 @@ void wifiStartAP()
145145 // Connect to local router
146146#define WIFI_SSID " TRex"
147147#define WIFI_PASSWORD " parachutes"
148- WiFi.mode (WIFI_STA);
149148
150149#ifdef COMPILE_ESPNOW
151150 // Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
152- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
151+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Stops WiFi Station
153152#endif
154153
154+ WiFi.mode (WIFI_STA);
155+
155156 WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
156157 Serial.print (" WiFi connecting to" );
157158 while (wifiGetStatus () != WL_CONNECTED)
@@ -161,15 +162,16 @@ void wifiStartAP()
161162 }
162163 Serial.print (" WiFi connected with IP: " );
163164 Serial.println (WiFi.localIP ());
164- #else // LOCAL_WIFI_TESTING
165+ #else // End LOCAL_WIFI_TESTING
165166 // Start in AP mode
166- WiFi.mode (WIFI_AP);
167167
168168#ifdef COMPILE_ESPNOW
169169 // Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
170- esp_wifi_set_protocol (WIFI_IF_AP, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
170+ esp_wifi_set_protocol (WIFI_IF_AP, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Stops WiFi AP.
171171#endif
172172
173+ WiFi.mode (WIFI_AP);
174+
173175 IPAddress local_IP (192 , 168 , 4 , 1 );
174176 IPAddress gateway (192 , 168 , 1 , 1 );
175177 IPAddress subnet (255 , 255 , 255 , 0 );
@@ -182,7 +184,7 @@ void wifiStartAP()
182184 }
183185 Serial.print (" WiFi AP Started with IP: " );
184186 Serial.println (WiFi.softAPIP ());
185- #endif // LOCAL_WIFI_TESTING
187+ #endif // End AP Testing
186188}
187189
188190#endif // COMPILE_WIFI
@@ -209,27 +211,23 @@ void wifiStart(char* ssid, char* pw)
209211#ifdef COMPILE_WIFI
210212 if ((wifiState == WIFI_OFF) || (wifiState == WIFI_ON))
211213 {
212- WiFi. mode (WIFI_STA );
214+ wifiSetState (WIFI_NOTCONNECTED );
213215
214216#ifdef COMPILE_ESPNOW
215217 if (espnowState > ESPNOW_OFF)
216- {
217- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); // Enable WiFi + ESP-Now
218- }
218+ 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.
219219 else
220- {
221- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Set basic WiFi protocols
222- }
220+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Set basic WiFi protocols. Stops WiFi Station.
223221#else
224222 // Be sure the standard protocols are turned on. ESP Now have have previously turned them off.
225- WiFi.mode (WIFI_STA);
226- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
223+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); // Set basic WiFi protocols. Stops WiFi Station.
227224#endif
228225
226+ WiFi.mode (WIFI_STA);
227+
229228 Serial.printf (" WiFi connecting to %s\r\n " , ssid);
230229 WiFi.begin (ssid, pw);
231230 wifiTimer = millis ();
232- wifiSetState (WIFI_NOTCONNECTED);
233231
234232 // Display the heap state
235233 reportHeapNow ();
@@ -254,26 +252,26 @@ void wifiStop()
254252 // If WiFi is on but ESP NOW is off, then turn off radio entirely
255253 else if (espnowState == ESPNOW_OFF)
256254 {
257- WiFi.mode (WIFI_OFF);
258255 wifiSetState (WIFI_OFF);
256+ WiFi.mode (WIFI_OFF);
259257 Serial.println (" WiFi Stopped" );
260258 }
261259 // If ESP-Now is active, change protocol to only Long Range
262260 else if (espnowState > ESPNOW_OFF)
263261 {
264- WiFi. mode (WIFI_STA );
262+ wifiSetState (WIFI_OFF );
265263
266264 // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
267- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR);
265+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR); // Stops WiFi Station.
268266
269- wifiSetState (WIFI_OFF );
267+ WiFi. mode (WIFI_STA );
270268
271269 Serial.println (" WiFi disabled, ESP-Now left in place" );
272270 }
273271#else
274272 // Turn off radio
275- WiFi.mode (WIFI_OFF);
276273 wifiSetState (WIFI_OFF);
274+ WiFi.mode (WIFI_OFF);
277275 Serial.println (" WiFi Stopped" );
278276#endif
279277
0 commit comments