Skip to content

Commit 86db8c7

Browse files
committed
Simplify ESP-NOW vs WiFi config
1 parent 57ac777 commit 86db8c7

File tree

5 files changed

+47
-76
lines changed

5 files changed

+47
-76
lines changed

Firmware/RTK_Surveyor/ESPNOW.ino

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,21 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
8989
//If the radio is off entirely, start the radio, turn on only the LR protocol
9090
void espnowStart()
9191
{
92-
9392
#ifdef COMPILE_ESPNOW
9493
if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
9594
{
96-
WiFi.mode(WIFI_STA);
97-
9895
//Radio is off, turn it on
9996
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR); //Stops WiFi Station.
10097

98+
WiFi.mode(WIFI_STA);
99+
101100
log_d("WiFi off, ESP-Now added to protocols");
102101
}
103102
//If WiFi is on but ESP NOW is off, then enable LR protocol
104103
else if (wifiState > WIFI_OFF && espnowState == ESPNOW_OFF)
105104
{
106-
WiFi.mode(WIFI_STA);
107-
108105
//Enable WiFi + ESP-Now
109-
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
106+
//Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
110107
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); //Stops WiFi Station.
111108

112109
log_d("WiFi on, ESP-Now added to protocols");
@@ -181,43 +178,43 @@ void espnowStop()
181178
#ifdef COMPILE_ESPNOW
182179
if (espnowState == ESPNOW_OFF) return;
183180

184-
// Turn off promiscuous WiFi mode
181+
//Turn off promiscuous WiFi mode
185182
esp_wifi_set_promiscuous(false);
186183
esp_wifi_set_promiscuous_rx_cb(NULL);
187184

188-
// Deregister callbacks
185+
//Deregister callbacks
189186
//esp_now_unregister_send_cb();
190187
esp_now_unregister_recv_cb();
191188

192-
// Deinit ESP-NOW
189+
//Forget all ESP-Now Peers
190+
for (int x = 0 ; x < settings.espnowPeerCount ; x++)
191+
espnowRemovePeer(settings.espnowPeers[x]);
192+
193+
//Leave WiFi with default settings (no WIFI_PROTOCOL_LR for ESP NOW)
194+
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); //Stops WiFi Station.
195+
196+
//Deinit ESP-NOW
193197
if (esp_now_deinit() != ESP_OK) {
194198
systemPrintln("Error deinitializing ESP-NOW");
195199
return;
196200
}
197201

202+
espnowSetState(ESPNOW_OFF);
203+
198204
if (wifiState == WIFI_OFF)
199205
{
200206
//ESP Now was the only thing using the radio so turn WiFi radio off entirely
201207
WiFi.mode(WIFI_OFF);
202208

203209
log_d("WiFi Radio off entirely");
204210
}
205-
//If WiFi is on, then disable LR protocol
211+
//If WiFi is on, then restart WiFi
206212
else if (wifiState > WIFI_OFF)
207213
{
208-
wifiSetState(WIFI_START); //Force WiFi to restart
209-
210-
// Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
211-
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N); //Stops WiFi Station.
212-
213-
WiFi.mode(WIFI_STA);
214-
215-
log_d("WiFi protocols on, LR protocol off");
214+
wifiStart(); //Force WiFi to restart
216215
}
217216

218217
#endif
219-
220-
espnowSetState(ESPNOW_OFF);
221218
}
222219

223220
//Start ESP-Now if needed, put ESP-Now into broadcast state

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ void ntripClientUpdate()
465465
if (!ntripClient->connected())
466466
{
467467
//Broken connection, retry the NTRIP client connection
468-
systemPrintln("NTRIP Client connection dropped");
468+
systemPrintln("NTRIP Client connection to caster was broken");
469469
ntripClientStop(false); //Allocate new wifiClient
470470
}
471471
else

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ void loop()
669669
tcpUpdate(); //Turn on TCP Client or Server as needed
670670

671671
printPosition(); //Periodically print GNSS coordinates if enabled
672-
672+
673673
//A small delay prevents panic if no other I2C or functions are called
674674
delay(10);
675675
}

Firmware/RTK_Surveyor/WiFi.ino

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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()
268270
void 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;

Firmware/RTK_Surveyor/menuSystem.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ void menuWiFi()
494494
//Restart WiFi if we are not in AP config mode
495495
if (wifiIsConnected())
496496
{
497+
log_d("Menu caused restarting of WiFi");
497498
wifiStop();
498499
wifiStart();
499500
}

0 commit comments

Comments
 (0)