Skip to content

Commit 3f5223f

Browse files
committed
Decouple WiFi and ESPNOW states.
Make wifiStart/Stop and esnowStart/Stop friendly and independent.
1 parent e1a2563 commit 3f5223f

File tree

7 files changed

+204
-104
lines changed

7 files changed

+204
-104
lines changed

Firmware/RTK_Surveyor/Base.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void SFE_UBLOX_GNSS::processRTCM(uint8_t incoming)
229229
{
230230
ntripServerProcessRTCM(incoming);
231231

232-
if (wifiState == WIFI_ESPNOW_PAIRED)
232+
if (wifiState == ESPNOW_PAIRED)
233233
{
234234
//Move this byte into ESP NOW to send buffer
235235
espnowOutgoing[espnowOutgoingSpot++] = incoming;

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ void beginRadio()
842842
{
843843
if (settings.radioType == RADIO_EXTERNAL)
844844
{
845-
wifiStop();
845+
espnowStop();
846846

847847
//Nothing to start. UART2 of ZED is connected to external Radio port and is configured at configureUbloxModule()
848848
}

Firmware/RTK_Surveyor/Display.ino

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Constants
33
//----------------------------------------
44

5-
//A bitfield is used to flag which icon needs to be illuminated
5+
//A bitfield is used to flag which icon needs to be illuminated
66
//systemState will dictate most of the icons needed
77

88
//Left top
@@ -602,16 +602,26 @@ void paintBatteryLevel()
602602
//Display Bluetooth icon, Bluetooth MAC, WiFi, or ESP NOW depending on connection state
603603
uint32_t paintWirelessIcon()
604604
{
605-
uint32_t icons;
605+
uint32_t icons = 0;
606606

607607
//TODO resolve if both BT and WiFi are on/connected
608608
icons = 0;
609609
if (online.display == true)
610610
{
611+
//Deal with each icon individiually
612+
//The ESPNOW_PAIRED is the only state where data is moving between units,
613+
//and thus is the only state that affects icon placement
614+
611615
//Bluetooth icon if paired, or Bluetooth MAC address if not paired
612-
if (bluetoothGetState() == BT_CONNECTED && wifiState == WIFI_OFF)
616+
if (bluetoothGetState() == BT_CONNECTED)
613617
{
614-
icons = ICON_BT_SYMBOL;
618+
if (espnowState != ESPNOW_PAIRED)
619+
icons |= ICON_BT_SYMBOL;
620+
else
621+
icons |= ICON_BT_SYMBOL_RIGHT;
622+
623+
//TODO deal with arrows when ESP Now paired
624+
615625
if (systemState <= STATE_BASE_NOT_STARTED)
616626
{
617627
if (online.rxRtcmCorrectionData)
@@ -620,58 +630,57 @@ uint32_t paintWirelessIcon()
620630
else if (systemState <= STATE_BUBBLE_LEVEL)
621631
icons |= ICON_UP_ARROW;
622632
}
623-
else if (wifiState == WIFI_NOTCONNECTED)
633+
else
634+
{
635+
if (espnowState != ESPNOW_PAIRED)
636+
icons |= ICON_MAC_ADDRESS;
637+
else
638+
icons |= ICON_MAC_ADDRESS_2DIGIT;
639+
}
640+
641+
if (wifiState == WIFI_NOTCONNECTED)
624642
{
625643
//Blink WiFi icon
626-
blinking_icons ^= ICON_WIFI_SYMBOL_LEFT;
627-
if (blinking_icons & ICON_WIFI_SYMBOL_LEFT)
628-
icons = ICON_WIFI_SYMBOL_LEFT;
644+
if (espnowState != ESPNOW_PAIRED)
645+
{
646+
blinking_icons ^= ICON_WIFI_SYMBOL_LEFT;
647+
if (blinking_icons & ICON_WIFI_SYMBOL_LEFT)
648+
icons |= ICON_WIFI_SYMBOL_LEFT;
649+
}
650+
else
651+
{
652+
//Move to center
653+
blinking_icons ^= ICON_WIFI_SYMBOL_CENTER;
654+
if (blinking_icons & ICON_WIFI_SYMBOL_CENTER)
655+
icons |= ICON_WIFI_SYMBOL_CENTER;
656+
}
629657
}
630658
else if (wifiState == WIFI_CONNECTED)
631659
{
632-
icons = ICON_WIFI_SYMBOL_LEFT;
660+
if (espnowState != ESPNOW_PAIRED)
661+
icons |= ICON_WIFI_SYMBOL_LEFT;
662+
else
663+
icons |= ICON_WIFI_SYMBOL_CENTER;
664+
665+
//TODO deal with arrows when ESP now paired
633666

634667
//If we are connected to NTRIP Client, show download arrow
635668
if ((online.ntripClient == true) && online.rxRtcmCorrectionData)
636669
icons |= ICON_DOWN_ARROW;
637670
else if (online.ntripServer == true)
638671
icons |= ICON_UP_ARROW;
639672
}
640-
else if (wifiState == WIFI_ESPNOW_PAIRED)
673+
674+
if (espnowState == ESPNOW_PAIRED)
641675
{
642-
//If ESPNOW is on, and we are in base mode, show animated icon
643-
if(systemState >= STATE_BASE_NOT_STARTED && systemState <= STATE_BASE_FIXED_TRANSMITTING)
644-
{
645-
//Display ESPNOW and BT (either MAC or symbol)
646-
icons = ICON_ESPNOW_SYMBOL;
647-
if (bluetoothGetState() == BT_CONNECTED)
648-
icons |= ICON_BT_SYMBOL_RIGHT;
649-
else
650-
icons |= ICON_MAC_ADDRESS_2DIGIT;
651-
}
652-
676+
//If ESPNOW is paired, and we are in base mode, show animated icon
677+
if (systemState >= STATE_BASE_NOT_STARTED && systemState <= STATE_BASE_FIXED_TRANSMITTING)
678+
icons |= ICON_ESPNOW_SYMBOL;
679+
653680
//If ESPNOW is on, and we've received a packet, move BT to make room
654681
else if (espnowRSSI > -255)
655-
{
656-
//Display ESPNOW and BT (either MAC or symbol)
657-
icons = ICON_ESPNOW_SYMBOL;
658-
if (bluetoothGetState() == BT_CONNECTED)
659-
icons |= ICON_BT_SYMBOL_RIGHT;
660-
else
661-
icons |= ICON_MAC_ADDRESS_2DIGIT; //Display 2 digit MAC
662-
}
663-
664-
//No ESPNOW icon
665-
else
666-
{
667-
if (bluetoothGetState() == BT_CONNECTED)
668-
icons = ICON_BT_SYMBOL;
669-
else
670-
icons = ICON_MAC_ADDRESS; //Display full MAC
671-
}
682+
icons |= ICON_ESPNOW_SYMBOL;
672683
}
673-
else
674-
icons = ICON_MAC_ADDRESS;
675684
}
676685
return icons;
677686
}

Firmware/RTK_Surveyor/ESPNOW.ino

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ typedef struct PairMessage {
2626
#ifdef COMPILE_WIFI
2727
void espnowOnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
2828
{
29-
// Serial.print("Last Packet Send Status: ");
30-
// if (status == ESP_NOW_SEND_SUCCESS)
31-
// Serial.println("Delivery Success");
32-
// else
33-
// Serial.println("Delivery Fail");
29+
// Serial.print("Last Packet Send Status: ");
30+
// if (status == ESP_NOW_SEND_SUCCESS)
31+
// Serial.println("Delivery Success");
32+
// else
33+
// Serial.println("Delivery Fail");
3434
}
3535
#endif
3636

3737
// Callback when data is received
3838
void espnowOnDataRecieved(const uint8_t *mac, const uint8_t *incomingData, int len)
3939
{
4040
#ifdef COMPILE_WIFI
41-
if (wifiState == WIFI_ESPNOW_PAIRING)
41+
if (espnowState == ESPNOW_PAIRING)
4242
{
4343
if (len == sizeof(PairMessage)) //First error check
4444
{
@@ -53,7 +53,7 @@ void espnowOnDataRecieved(const uint8_t *mac, const uint8_t *incomingData, int l
5353
if (tempCRC == pairMessage.crc) //2nd error check
5454
{
5555
memcpy(&receivedMAC, pairMessage.macAddress, 6);
56-
wifiSetState(WIFI_ESPNOW_MAC_RECEIVED);
56+
espnowSetState(ESPNOW_MAC_RECEIVED);
5757
}
5858
//else Pair CRC failed
5959
}
@@ -86,17 +86,29 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
8686
}
8787
#endif
8888

89+
//If WiFi is already enabled, simply add the LR protocol
90+
//If the radio is off entirely, start the radio, turn on only the LR protocol
8991
void espnowStart()
9092
{
9193
#ifdef COMPILE_WIFI
92-
// If we are in WIFI_OFF or WIFI_ON, CONNECTED, NOTCONNECTED, doesn't matter
93-
// General WiFi will be turned off/disconnected and replaced by ESP NOW
9494

95-
// Set device as a Wi-Fi Station
96-
WiFi.mode(WIFI_STA);
97-
98-
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
99-
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR);
95+
if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
96+
{
97+
//Radio is off, turn it on
98+
WiFi.mode(WIFI_STA);
99+
}
100+
//If WiFi is on but ESP NOW is off, then enable LR protocol
101+
else if (wifiState > WIFI_OFF && espnowState == ESPNOW_OFF)
102+
{
103+
//Enable WiFi + ESP-Now
104+
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
105+
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
106+
}
107+
//If ESP-Now is active, WiFi is active, do nothing
108+
else
109+
{
110+
Serial.println("Wi-Fi on, ESP-Now on");
111+
}
100112

101113
// Init ESP-NOW
102114
if (esp_now_init() != ESP_OK) {
@@ -108,18 +120,18 @@ void espnowStart()
108120
esp_wifi_set_promiscuous(true);
109121
esp_wifi_set_promiscuous_rx_cb(&promiscuous_rx_cb);
110122

111-
// Register send callbacks
123+
// Register callbacks
112124
//esp_now_register_send_cb(espnowOnDataSent);
113125
esp_now_register_recv_cb(espnowOnDataRecieved);
114126

115127
if (settings.espnowPeerCount == 0)
116128
{
117-
wifiSetState(WIFI_ESPNOW_ON);
129+
espnowSetState(ESPNOW_ON);
118130
}
119131
else
120132
{
121133
//If we already have peers, move to paired state
122-
wifiSetState(WIFI_ESPNOW_PAIRED);
134+
espnowSetState(ESPNOW_PAIRED);
123135

124136
log_d("Adding %d espnow peers", settings.espnowPeerCount);
125137
for (int x = 0 ; x < settings.espnowPeerCount ; x++)
@@ -140,27 +152,60 @@ void espnowStart()
140152
#endif
141153
}
142154

143-
//Begin broadcasting our MAC and wait for remote unit to respond
144-
void espnowBeginPairing()
155+
//If WiFi is already enabled, simply remove the LR protocol
156+
//If WiFi is off, stop the radio entirely
157+
void espnowStop()
145158
{
146-
#ifndef COMPILE_WIFI
147-
return;
148-
#endif
159+
#ifdef COMPILE_WIFI
160+
if(espnowState == ESPNOW_OFF) return;
149161

150-
if (wifiState != WIFI_ESPNOW_ON
151-
|| wifiState != WIFI_ESPNOW_PAIRING
152-
|| wifiState != WIFI_ESPNOW_MAC_RECEIVED
153-
|| wifiState != WIFI_ESPNOW_PAIRED
154-
)
162+
if (wifiState > WIFI_OFF)
155163
{
156-
espnowStart();
164+
//Radio is on, turn it off entirely
165+
WiFi.mode(WIFI_OFF);
166+
Serial.println("WiFi Radio off entirely");
167+
}
168+
//If WiFi is on, then disable LR protocol
169+
else if (wifiState > WIFI_OFF)
170+
{
171+
// Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
172+
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
173+
Serial.println("WiFi protocol on, LR protocol off");
174+
}
175+
176+
// Turn off promiscuous WiFi mode
177+
esp_wifi_set_promiscuous(false);
178+
esp_wifi_set_promiscuous_rx_cb(NULL);
179+
180+
// Deregister callbacks
181+
//esp_now_unregister_send_cb();
182+
esp_now_unregister_recv_cb();
183+
184+
// Deinit ESP-NOW
185+
if (esp_now_deinit() != ESP_OK) {
186+
Serial.println("Error deinitializing ESP-NOW");
187+
return;
157188
}
158189

190+
espnowSetState(ESPNOW_OFF);
191+
192+
Serial.println("ESP NOW Off");
193+
#else
194+
Serial.println("Error - WiFi not compiled");
195+
#endif
196+
}
197+
198+
//Begin broadcasting our MAC and wait for remote unit to respond
199+
void espnowBeginPairing()
200+
{
201+
#ifdef COMPILE_WIFI
202+
espnowStart();
203+
159204
// To begin pairing, we must add the broadcast MAC to the peer list
160205
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
161206
espnowAddPeer(broadcastMac, false); // Encryption is not supported for multicast addresses
162207

163-
wifiSetState(WIFI_ESPNOW_PAIRING);
208+
espnowSetState(ESPNOW_PAIRING);
164209

165210
//Begin sending our MAC every 250ms until a remote device sends us there info
166211
randomSeed(millis());
@@ -177,7 +222,7 @@ void espnowBeginPairing()
177222
{
178223
delay(1);
179224

180-
if (wifiState == WIFI_ESPNOW_MAC_RECEIVED)
225+
if (espnowState == ESPNOW_MAC_RECEIVED)
181226
{
182227
//Remove broadcast peer
183228
espnowRemovePeer(broadcastMac);
@@ -198,7 +243,7 @@ void espnowBeginPairing()
198243
//Send message directly to the received MAC (not unicast), then exit
199244
espnowSendPairMessage(receivedMAC);
200245

201-
wifiSetState(WIFI_ESPNOW_PAIRED);
246+
espnowSetState(ESPNOW_PAIRED);
202247
Serial.println("Pairing compete");
203248
return;
204249
}
@@ -210,6 +255,9 @@ void espnowBeginPairing()
210255
}
211256

212257
Serial.println("User pressed button. Pairing canceled.");
258+
#else
259+
Serial.println("WiFi not compiled");
260+
#endif
213261
}
214262

215263
//Create special pair packet to a given MAC
@@ -275,3 +323,32 @@ esp_err_t espnowRemovePeer(uint8_t *peerMac)
275323
return (ESP_OK);
276324
#endif
277325
}
326+
327+
//Update the state of the ESP Now state machine
328+
void espnowSetState (byte newState)
329+
{
330+
if (espnowState == newState)
331+
Serial.print("*");
332+
espnowState = newState;
333+
switch (newState)
334+
{
335+
default:
336+
Serial.printf("Unknown ESPNOW state: %d\r\n", newState);
337+
break;
338+
case ESPNOW_OFF:
339+
Serial.println("ESPNOW_OFF");
340+
break;
341+
case ESPNOW_ON:
342+
Serial.println("ESPNOW_ON");
343+
break;
344+
case ESPNOW_PAIRING:
345+
Serial.println("ESPNOW_PAIRING");
346+
break;
347+
case ESPNOW_MAC_RECEIVED:
348+
Serial.println("ESPNOW_MAC_RECEIVED");
349+
break;
350+
case ESPNOW_PAIRED:
351+
Serial.println("ESPNOW_PAIRED");
352+
break;
353+
}
354+
}

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const int FIRMWARE_VERSION_MAJOR = 2;
2626
const int FIRMWARE_VERSION_MINOR = 4;
2727

2828
#define COMPILE_WIFI //Comment out to remove WiFi functionality
29-
#define COMPILE_BT //Comment out to remove Bluetooth functionality
29+
//#define COMPILE_BT //Comment out to remove Bluetooth functionality
3030
#define COMPILE_AP //Comment out to remove Access Point functionality
3131
#define ENABLE_DEVELOPER //Uncomment this line to enable special developer modes (don't check power button at startup)
3232

@@ -835,7 +835,7 @@ void updateRadio()
835835
{
836836
if (settings.radioType == RADIO_ESPNOW)
837837
{
838-
if (wifiState == WIFI_ESPNOW_PAIRED)
838+
if (espnowState == ESPNOW_PAIRED)
839839
{
840840
//If it's been longer than a few ms since we last added a byte to the buffer
841841
//then we've reached the end of the RTCM stream. Send partial buffer.

0 commit comments

Comments
 (0)