Skip to content

Commit 43930d5

Browse files
committed
Add error handling to ESP-NOW
1 parent 321a369 commit 43930d5

File tree

8 files changed

+118
-73
lines changed

8 files changed

+118
-73
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ void beginGNSS()
432432
return;
433433
}
434434
}
435-
435+
436436
//Increase transactions to reduce transfer time
437437
i2cGNSS.i2cTransactionSize = 128;
438438

@@ -769,9 +769,7 @@ void radioStart()
769769
//Nothing to start. UART2 of ZED is connected to external Radio port and is configured at configureUbloxModule()
770770
}
771771
else if (settings.radioType == RADIO_ESPNOW)
772-
{
773772
espnowStart();
774-
}
775773
}
776774

777775
//Start task to determine SD card size

Firmware/RTK_Surveyor/ESPNOW.ino

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,33 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
9090
void espnowStart()
9191
{
9292
#ifdef COMPILE_ESPNOW
93+
94+
esp_err_t response;
95+
9396
if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
9497
{
95-
//Radio is off, turn it on
96-
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR); //Stops WiFi Station.
98+
if (WiFi.getMode() == WIFI_OFF)
99+
WiFi.mode(WIFI_STA);
97100

98-
WiFi.mode(WIFI_STA);
99-
100-
log_d("WiFi off, ESP-Now added to protocols");
101+
//Radio is off, turn it on
102+
//esp_wifi_set_protocol requires WiFi to be started
103+
response = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR); //Stops WiFi Station.
104+
if (response != ESP_OK)
105+
systemPrintf("espnowStart: Error setting ESP-Now lone protocol: %s\r\n", esp_err_to_name(response));
106+
else
107+
log_d("WiFi off, ESP-Now added to protocols");
101108
}
102109
//If WiFi is on but ESP NOW is off, then enable LR protocol
103110
else if (wifiState > WIFI_OFF && espnowState == ESPNOW_OFF)
104111
{
105112
//Enable WiFi + ESP-Now
106113
//Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
107-
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR); //Stops WiFi Station.
108-
109-
log_d("WiFi on, ESP-Now added to protocols");
114+
//esp_wifi_set_protocol requires WiFi to be started
115+
response = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
116+
if (response != ESP_OK)
117+
systemPrintf("espnowStart: Error setting ESP-Now + WiFi protocols: %s\r\n", esp_err_to_name(response));
118+
else
119+
log_d("WiFi on, ESP-Now added to protocols");
110120
}
111121

112122
//If ESP-Now is already active, do nothing
@@ -115,14 +125,18 @@ void espnowStart()
115125
log_d("ESP-Now already on");
116126
}
117127

128+
118129
// Init ESP-NOW
119130
if (esp_now_init() != ESP_OK) {
120-
systemPrintln("Error starting ESP-Now");
131+
systemPrintln("espnowStart: Error starting ESP-Now");
121132
return;
122133
}
123134

124135
// Use promiscuous callback to capture RSSI of packet
125-
esp_wifi_set_promiscuous(true);
136+
response = esp_wifi_set_promiscuous(true);
137+
if (response != ESP_OK)
138+
systemPrintf("espnowStart: Error setting promiscuous mode: %s\r\n", esp_err_to_name(response));
139+
126140
esp_wifi_set_promiscuous_rx_cb(&promiscuous_rx_cb);
127141

128142
// Register callbacks
@@ -179,19 +193,29 @@ void espnowStop()
179193
if (espnowState == ESPNOW_OFF) return;
180194

181195
//Turn off promiscuous WiFi mode
182-
esp_wifi_set_promiscuous(false);
196+
esp_err_t response = esp_wifi_set_promiscuous(false);
197+
if (response != ESP_OK)
198+
systemPrintf("espnowStop: Failed to set promiscuous mode: %s\r\n", esp_err_to_name(response));
199+
183200
esp_wifi_set_promiscuous_rx_cb(NULL);
184201

185202
//Deregister callbacks
186203
//esp_now_unregister_send_cb();
187-
esp_now_unregister_recv_cb();
204+
response = esp_now_unregister_recv_cb();
205+
if (response != ESP_OK)
206+
systemPrintf("espnowStop: Failed to unregister receive callback: %s\r\n", esp_err_to_name(response));
188207

189208
//Forget all ESP-Now Peers
190209
for (int x = 0 ; x < settings.espnowPeerCount ; x++)
191210
espnowRemovePeer(settings.espnowPeers[x]);
192211

193212
//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.
213+
//esp_wifi_set_protocol requires WiFi to be started
214+
response = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
215+
if (response != ESP_OK)
216+
systemPrintf("espnowStop: Error setting WiFi protocols: %s\r\n", esp_err_to_name(response));
217+
else
218+
log_d("WiFi on, ESP-Now added to protocols");
195219

196220
//Deinit ESP-NOW
197221
if (esp_now_deinit() != ESP_OK) {
@@ -325,10 +349,11 @@ esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
325349
esp_err_t espnowRemovePeer(uint8_t *peerMac)
326350
{
327351
#ifdef COMPILE_ESPNOW
328-
esp_err_t result = esp_now_del_peer(peerMac);
329-
if (result != ESP_OK)
330-
log_d("Failed to remove peer");
331-
return (result);
352+
esp_err_t response = esp_now_del_peer(peerMac);
353+
if (response != ESP_OK)
354+
log_d("Failed to remove peer: %s", esp_err_to_name(response));
355+
356+
return (response);
332357
#else
333358
return (ESP_OK);
334359
#endif
@@ -417,7 +442,7 @@ void espnowStaticPairing()
417442
bool exitPair = false;
418443
while (exitPair == false)
419444
{
420-
if (Serial.available())
445+
if (systemAvailable())
421446
{
422447
systemPrintln("User pressed button. Pairing canceled.");
423448
break;

Firmware/RTK_Surveyor/States.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ void updateSystemState()
613613
{
614614
systemPrint("Parsing: ");
615615
for (int x = 0 ; x < incomingSettingsSpot ; x++)
616-
Serial.write(incomingSettings[x]);
616+
systemWrite(incomingSettings[x]);
617617
systemPrintln();
618618

619619
parseIncomingSettings();

Firmware/RTK_Surveyor/System.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ bool setConstellations(bool sendCompleteBatch)
585585
//Periodically print position if enabled
586586
void printPosition()
587587
{
588-
//Periodically print the position
588+
//Periodically print the position
589589
if (settings.enablePrintPosition && ((millis() - lastPrintPosition) > 15000))
590590
{
591591
printCurrentConditions();

Firmware/RTK_Surveyor/WiFi.ino

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

Firmware/RTK_Surveyor/menuBase.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ void menuBase()
3131
{
3232
systemPrint("3) Set Lat/Long/Altitude coordinates: ");
3333
systemPrint(settings.fixedLat, haeNumberOfDecimals);
34-
Serial.write(167); //°
34+
systemWrite(167); //°
3535
systemPrint(", ");
3636
systemPrint(settings.fixedLong, haeNumberOfDecimals);
37-
Serial.write(167); //°
37+
systemWrite(167); //°
3838
systemPrint(", ");
3939
systemPrint(settings.fixedAltitude, 4);
4040
systemPrintln("m");

Firmware/RTK_Surveyor/menuFirmware.ino

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ void menuFirmware()
4747
{
4848
bool previouslyConnected = wifiIsConnected();
4949

50-
log_d("Firmware version Check starting WiFi");
51-
wifiStart(); //Makes sure any ESP-Now settings have been cleared
52-
5350
//Attempt to connect to local WiFi
5451
if (wifiConnect(10000) == true)
5552
{
@@ -349,9 +346,6 @@ bool otaCheckVersion(char *versionAvailable, uint8_t versionAvailableLength)
349346
#ifdef COMPILE_WIFI
350347
bool previouslyConnected = wifiIsConnected();
351348

352-
log_d("OTA Check Version starting WiFi");
353-
wifiStart(); //Makes sure any ESP-Now settings have been cleared
354-
355349
if (wifiConnect(10000) == true)
356350
{
357351
char versionString[20];
@@ -395,7 +389,9 @@ bool otaCheckVersion(char *versionAvailable, uint8_t versionAvailableLength)
395389
}
396390
}
397391
else
398-
systemPrintln("WiFi not available");
392+
{
393+
systemPrintln("WiFi not available.");
394+
}
399395

400396
if (systemState != STATE_WIFI_CONFIG)
401397
{
@@ -422,9 +418,6 @@ void otaUpdate()
422418
#ifdef COMPILE_WIFI
423419
bool previouslyConnected = wifiIsConnected();
424420

425-
log_d("OTA Update starting WiFi");
426-
wifiStart(); //Makes sure any ESP-Now settings have been cleared
427-
428421
if (wifiConnect(10000) == true)
429422
{
430423
char versionString[20];
@@ -470,7 +463,9 @@ void otaUpdate()
470463
}
471464
}
472465
else
473-
systemPrintln("WiFi not available");
466+
{
467+
systemPrintln("WiFi not available.");
468+
}
474469

475470
//Update failed. If WiFi was originally off, turn it off again
476471
if (previouslyConnected == false)

0 commit comments

Comments
 (0)