Skip to content

Commit 89a1090

Browse files
committed
Change to station before setting WiFi protocols.
Prior to this, the WiFi radio could be on, but be in AP mode. If the old code ran, the protocols would not get set correctly because of mode was not correctly set to station (getMode != WIFI_OFF).
1 parent 04eef22 commit 89a1090

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Firmware/RTK_Surveyor/ESPNOW.ino

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void espnowOnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
3232
// else
3333
// systemPrintln("Delivery Fail");
3434
}
35-
#endif // COMPILE_ESPNOW
35+
#endif // COMPILE_ESPNOW
3636

3737
// Callback when data is received
3838
void espnowOnDataReceived(const uint8_t *mac, const uint8_t *incomingData, int len)
@@ -73,7 +73,7 @@ void espnowOnDataReceived(const uint8_t *mac, const uint8_t *incomingData, int l
7373
espnowIncomingRTCM = true;
7474
lastEspnowRssiUpdate = millis();
7575
}
76-
#endif // COMPILE_ESPNOW
76+
#endif // COMPILE_ESPNOW
7777
}
7878

7979
// Callback for all RX Packets
@@ -88,7 +88,7 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
8888
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buf;
8989
packetRSSI = ppkt->rx_ctrl.rssi;
9090
}
91-
#endif // COMPILE_ESPNOW
91+
#endif // COMPILE_ESPNOW
9292

9393
// If WiFi is already enabled, simply add the LR protocol
9494
// If the radio is off entirely, start the radio, turn on only the LR protocol
@@ -100,7 +100,7 @@ void espnowStart()
100100

101101
if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
102102
{
103-
if (WiFi.getMode() == WIFI_OFF)
103+
if (WiFi.getMode() != WIFI_STA)
104104
WiFi.mode(WIFI_STA);
105105

106106
// Radio is off, turn it on
@@ -114,6 +114,9 @@ void espnowStart()
114114
// If WiFi is on but ESP NOW is off, then enable LR protocol
115115
else if (wifiState > WIFI_OFF && espnowState == ESPNOW_OFF)
116116
{
117+
if (WiFi.getMode() != WIFI_STA)
118+
WiFi.mode(WIFI_STA);
119+
117120
// Enable WiFi + ESP-Now
118121
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
119122
// esp_wifi_set_protocol requires WiFi to be started
@@ -188,7 +191,7 @@ void espnowStart()
188191
}
189192

190193
systemPrintln("ESP-Now Started");
191-
#endif // COMPILE_ESPNOW
194+
#endif // COMPILE_ESPNOW
192195
}
193196

194197
// If WiFi is already enabled, simply remove the LR protocol
@@ -216,6 +219,9 @@ void espnowStop()
216219
for (int x = 0; x < settings.espnowPeerCount; x++)
217220
espnowRemovePeer(settings.espnowPeers[x]);
218221

222+
if (WiFi.getMode() != WIFI_STA)
223+
WiFi.mode(WIFI_STA);
224+
219225
// Leave WiFi with default settings (no WIFI_PROTOCOL_LR for ESP NOW)
220226
// esp_wifi_set_protocol requires WiFi to be started
221227
response = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
@@ -247,7 +253,7 @@ void espnowStop()
247253
wifiStart(); // Force WiFi to restart
248254
}
249255

250-
#endif // COMPILE_ESPNOW
256+
#endif // COMPILE_ESPNOW
251257
}
252258

253259
// Start ESP-Now if needed, put ESP-Now into broadcast state
@@ -300,7 +306,7 @@ bool espnowIsPaired()
300306
espnowSetState(ESPNOW_PAIRED);
301307
return (true);
302308
}
303-
#endif // COMPILE_ESPNOW
309+
#endif // COMPILE_ESPNOW
304310
return (false);
305311
}
306312

@@ -321,9 +327,9 @@ esp_err_t espnowSendPairMessage(uint8_t *sendToMac)
321327
pairMessage.crc += wifiMACAddress[x];
322328

323329
return (esp_now_send(sendToMac, (uint8_t *)&pairMessage, sizeof(pairMessage))); // Send packet to given MAC
324-
#else // COMPILE_ESPNOW
330+
#else // COMPILE_ESPNOW
325331
return (ESP_OK);
326-
#endif // COMPILE_ESPNOW
332+
#endif // COMPILE_ESPNOW
327333
}
328334

329335
// Add a given MAC address to the peer list
@@ -349,9 +355,9 @@ esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
349355
log_d("Failed to add peer: 0x%02X%02X%02X%02X%02X%02X", peerMac[0], peerMac[1], peerMac[2], peerMac[3],
350356
peerMac[4], peerMac[5]);
351357
return (result);
352-
#else // COMPILE_ESPNOW
358+
#else // COMPILE_ESPNOW
353359
return (ESP_OK);
354-
#endif // COMPILE_ESPNOW
360+
#endif // COMPILE_ESPNOW
355361
}
356362

357363
// Remove a given MAC address from the peer list
@@ -363,9 +369,9 @@ esp_err_t espnowRemovePeer(uint8_t *peerMac)
363369
log_d("Failed to remove peer: %s", esp_err_to_name(response));
364370

365371
return (response);
366-
#else // COMPILE_ESPNOW
372+
#else // COMPILE_ESPNOW
367373
return (ESP_OK);
368-
#endif // COMPILE_ESPNOW
374+
#endif // COMPILE_ESPNOW
369375
}
370376

371377
// Update the state of the ESP Now state machine
@@ -428,7 +434,7 @@ void espnowProcessRTCM(byte incoming)
428434
espnowOutgoingRTCM = true;
429435
}
430436
}
431-
#endif // COMPILE_ESPNOW
437+
#endif // COMPILE_ESPNOW
432438
}
433439

434440
// A blocking function that is used to pair two devices

Firmware/RTK_Surveyor/WiFi.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ void wifiShutdown()
440440
// If ESP-Now is active, change protocol to only Long Range and re-start WiFi
441441
if (espnowState > ESPNOW_OFF)
442442
{
443-
if (WiFi.getMode() == WIFI_OFF)
443+
if (WiFi.getMode() != WIFI_STA)
444444
WiFi.mode(WIFI_STA);
445445

446446
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
@@ -477,7 +477,7 @@ bool wifiConnect(unsigned long timeout)
477477
displayWiFiConnect();
478478

479479
// Before we can issue esp_wifi_() commands WiFi must be started
480-
if (WiFi.getMode() == WIFI_OFF)
480+
if (WiFi.getMode() != WIFI_STA)
481481
WiFi.mode(WIFI_STA);
482482

483483
// Verify that the necessary protocols are set

0 commit comments

Comments
 (0)