Skip to content

Commit 2f7ea43

Browse files
committed
Add pairing states
1 parent 459f543 commit 2f7ea43

File tree

9 files changed

+127
-59
lines changed

9 files changed

+127
-59
lines changed

Firmware/RTK_Surveyor/Base.ino

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,5 @@ void SFE_UBLOX_GNSS::processRTCM(uint8_t incoming)
229229
{
230230
ntripServerProcessRTCM(incoming);
231231

232-
#ifdef COMPILE_ESPNOW
233232
espnowProcessRTCM(incoming);
234-
#endif
235233
}

Firmware/RTK_Surveyor/Display.ino

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ void updateDisplay()
331331
//Do nothing. Quick, fall through state.
332332
break;
333333

334-
case (STATE_ESPNOW_PAIR):
335-
paintEspNowPair();
334+
case (STATE_ESPNOW_PAIRING_NOT_STARTED):
335+
paintEspNowPairing();
336+
break;
337+
case (STATE_ESPNOW_PAIRING):
338+
paintEspNowPairing();
336339
break;
337340

338341
case (STATE_SHUTDOWN):
@@ -2151,7 +2154,7 @@ void paintDisplaySetup()
21512154
printTextCenter("Bubble", 12 * 2, QW_FONT_8X16, 1, false);
21522155
printTextCenter("Config", 12 * 3, QW_FONT_8X16, 1, true);
21532156
}
2154-
else if (setupState == STATE_ESPNOW_PAIR)
2157+
else if (setupState == STATE_ESPNOW_PAIRING_NOT_STARTED)
21552158
{
21562159
printTextCenter("Base", 12 * 0, QW_FONT_8X16, 1, false);
21572160
printTextCenter("Bubble", 12 * 1, QW_FONT_8X16, 1, false);
@@ -2191,7 +2194,7 @@ void paintDisplaySetup()
21912194
printTextCenter("Bubble", 12 * 2, QW_FONT_8X16, 1, false);
21922195
printTextCenter("Config", 12 * 3, QW_FONT_8X16, 1, true);
21932196
}
2194-
else if (setupState == STATE_ESPNOW_PAIR)
2197+
else if (setupState == STATE_ESPNOW_PAIRING_NOT_STARTED)
21952198
{
21962199
printTextCenter("Rover", 12 * 0, QW_FONT_8X16, 1, false);
21972200
printTextCenter("Bubble", 12 * 1, QW_FONT_8X16, 1, false);
@@ -2521,7 +2524,11 @@ void paintKeyProvisionFail(uint16_t displayTime)
25212524
}
25222525

25232526
//Show screen while ESP-Now is pairing
2524-
void paintEspNowPair()
2527+
void paintEspNowPairing()
25252528
{
25262529
displayMessage("ESP-Now Pairing", 0);
25272530
}
2531+
void paintEspNowPaired()
2532+
{
2533+
displayMessage("ESP-Now Paired", 2000);
2534+
}

Firmware/RTK_Surveyor/ESPNOW.ino

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
We don't care if the ESP NOW packet is corrupt or not. RTCM has its own CRC. RTK needs valid RTCM once every
1313
few seconds so a single dropped frame is not critical.
1414
*/
15-
#ifdef COMPILE_ESPNOW
1615

1716
//Create a struct for ESP NOW pairing
1817
typedef struct PairMessage {
@@ -23,6 +22,7 @@ typedef struct PairMessage {
2322
} PairMessage;
2423

2524
// Callback when data is sent
25+
#ifdef COMPILE_ESPNOW
2626
void espnowOnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
2727
{
2828
// Serial.print("Last Packet Send Status: ");
@@ -31,10 +31,12 @@ void espnowOnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
3131
// else
3232
// Serial.println("Delivery Fail");
3333
}
34+
#endif
3435

3536
// Callback when data is received
3637
void espnowOnDataRecieved(const uint8_t *mac, const uint8_t *incomingData, int len)
3738
{
39+
#ifdef COMPILE_ESPNOW
3840
if (espnowState == ESPNOW_PAIRING)
3941
{
4042
if (len == sizeof(PairMessage)) //First error check
@@ -66,10 +68,12 @@ void espnowOnDataRecieved(const uint8_t *mac, const uint8_t *incomingData, int l
6668
espnowIncomingRTCM = true;
6769
lastEspnowRssiUpdate = millis();
6870
}
71+
#endif
6972
}
7073

7174
// Callback for all RX Packets
7275
// Get RSSI of all incoming management packets: https://esp32.com/viewtopic.php?t=13889
76+
#ifdef COMPILE_ESPNOW
7377
void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
7478
{
7579
// All espnow traffic uses action frames which are a subtype of the mgmnt frames so filter out everything else.
@@ -79,11 +83,13 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
7983
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buf;
8084
packetRSSI = ppkt->rx_ctrl.rssi;
8185
}
86+
#endif
8287

8388
//If WiFi is already enabled, simply add the LR protocol
8489
//If the radio is off entirely, start the radio, turn on only the LR protocol
8590
void espnowStart()
8691
{
92+
#ifdef COMPILE_ESPNOW
8793
if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
8894
{
8995
//Radio is off, turn it on
@@ -97,21 +103,19 @@ void espnowStart()
97103
//Enable WiFi + ESP-Now
98104
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
99105
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
100-
Serial.println("Wi-Fi on, ESP-Now added to protocols");
106+
Serial.println("WiFi on, ESP-Now added to protocols");
101107
}
102108
//If ESP-Now is active, WiFi is active, do nothing
103109
else
104110
{
105-
Serial.println("Wi-Fi already on, ESP-Now already on");
111+
Serial.println("WiFi already on, ESP-Now already on");
106112
}
107113

108114
// Init ESP-NOW
109115
if (esp_now_init() != ESP_OK) {
110-
Serial.println("Error initializing ESP-NOW");
116+
Serial.println("Error starting ESP-Now");
111117
return;
112118
}
113-
else
114-
Serial.println("ESP-NOW Initialized");
115119

116120
// Use promiscuous callback to capture RSSI of packet
117121
esp_wifi_set_promiscuous(true);
@@ -143,12 +147,16 @@ void espnowStart()
143147
}
144148
}
145149
}
150+
151+
Serial.println("ESP-Now Started");
152+
#endif
146153
}
147154

148155
//If WiFi is already enabled, simply remove the LR protocol
149156
//If WiFi is off, stop the radio entirely
150157
void espnowStop()
151158
{
159+
#ifdef COMPILE_ESPNOW
152160
if (espnowState == ESPNOW_OFF) return;
153161

154162
if (wifiState == WIFI_OFF)
@@ -178,10 +186,8 @@ void espnowStop()
178186
Serial.println("Error deinitializing ESP-NOW");
179187
return;
180188
}
181-
189+
#endif
182190
espnowSetState(ESPNOW_OFF);
183-
184-
Serial.println("ESP NOW Off");
185191
}
186192

187193
//Start ESP-Now if needed, put ESP-Now into broadcast state
@@ -199,9 +205,11 @@ void espnowBeginPairing()
199205
//Regularly call during pairing to see if we've received a Pairing message
200206
bool espnowIsPaired()
201207
{
208+
#ifdef COMPILE_ESPNOW
202209
if (espnowState == ESPNOW_MAC_RECEIVED)
203210
{
204211
//Remove broadcast peer
212+
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
205213
espnowRemovePeer(broadcastMac);
206214

207215
if (esp_now_is_peer_exist(receivedMAC) == true)
@@ -221,15 +229,16 @@ bool espnowIsPaired()
221229
espnowSendPairMessage(receivedMAC);
222230

223231
espnowSetState(ESPNOW_PAIRED);
224-
Serial.println("Pairing compete");
225-
return(true);
232+
return (true);
226233
}
227-
return(false);
234+
#endif
235+
return (false);
228236
}
229237

230238
//Create special pair packet to a given MAC
231239
esp_err_t espnowSendPairMessage(uint8_t *sendToMac)
232240
{
241+
#ifdef COMPILE_ESPNOW
233242
// Assemble message to send
234243
PairMessage pairMessage;
235244

@@ -245,6 +254,9 @@ esp_err_t espnowSendPairMessage(uint8_t *sendToMac)
245254
pairMessage.crc += unitMACAddress[x];
246255

247256
return (esp_now_send(sendToMac, (uint8_t *) &pairMessage, sizeof(pairMessage))); //Send packet to given MAC
257+
#else
258+
return (ESP_OK);
259+
#endif
248260
}
249261

250262
//Add a given MAC address to the peer list
@@ -255,6 +267,7 @@ esp_err_t espnowAddPeer(uint8_t *peerMac)
255267

256268
esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
257269
{
270+
#ifdef COMPILE_ESPNOW
258271
esp_now_peer_info_t peerInfo;
259272

260273
memcpy(peerInfo.peer_addr, peerMac, 6);
@@ -268,15 +281,23 @@ esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
268281
if (result != ESP_OK)
269282
log_d("Failed to add peer");
270283
return (result);
284+
#else
285+
return (ESP_OK);
286+
#endif
271287
}
272288

273289
//Remove a given MAC address from the peer list
274290
esp_err_t espnowRemovePeer(uint8_t *peerMac)
275291
{
292+
#ifdef COMPILE_ESPNOW
276293
esp_err_t result = esp_now_del_peer(peerMac);
277294
if (result != ESP_OK)
278295
log_d("Failed to remove peer");
279296
return (result);
297+
#else
298+
return (ESP_OK);
299+
#endif
300+
280301
}
281302

282303
//Update the state of the ESP Now state machine
@@ -285,6 +306,8 @@ void espnowSetState(ESPNOWState newState)
285306
if (espnowState == newState)
286307
Serial.print("*");
287308
espnowState = newState;
309+
310+
Serial.print("espnowState: ");
288311
switch (newState)
289312
{
290313
case ESPNOW_OFF:
@@ -310,6 +333,7 @@ void espnowSetState(ESPNOWState newState)
310333

311334
void espnowProcessRTCM(byte incoming)
312335
{
336+
#ifdef COMPILE_ESPNOW
313337
if (espnowState == ESPNOW_PAIRED)
314338
{
315339
//Move this byte into ESP NOW to send buffer
@@ -326,6 +350,5 @@ void espnowProcessRTCM(byte incoming)
326350
espnowOutgoingRTCM = true;
327351
}
328352
}
353+
#endif
329354
}
330-
331-
#endif //ifdef COMPILE_ESPNOW

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ 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
30-
#define COMPILE_AP //Comment out to remove Access Point functionality
31-
#define COMPILE_L_BAND //Comment out to remove L-Band functionality
32-
#define COMPILE_ESPNOW //Comment out to remove ESP-Now functionality
29+
//#define COMPILE_AP //Requires WiFi. Comment out to remove Access Point functionality
30+
//#define COMPILE_ESPNOW //Requires WiFi. Comment out to remove ESP-Now functionality.
31+
//#define COMPILE_BT //Comment out to remove Bluetooth functionality
32+
//#define COMPILE_L_BAND //Comment out to remove L-Band functionality
3333
#define ENABLE_DEVELOPER //Uncomment this line to enable special developer modes (don't check power button at startup)
3434

3535
//Define the RTK board identifier:

Firmware/RTK_Surveyor/States.ino

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void updateSystemState()
240240

241241
bluetoothStop();
242242
bluetoothStart(); //Restart Bluetooth with 'Base' identifier
243-
243+
244244
startUART2Tasks(); //Start monitoring the UART1 from ZED for NMEA and UBX data (enables logging)
245245

246246
if (configureUbloxModuleBase() == true)
@@ -900,12 +900,35 @@ void updateSystemState()
900900
break;
901901
#endif //COMPILE_L_BAND
902902

903-
case (STATE_ESPNOW_PAIR):
903+
case (STATE_ESPNOW_PAIRING_NOT_STARTED):
904904
{
905+
#ifdef COMPILE_ESPNOW
906+
paintEspNowPairing();
905907

906-
espnowIsPaired()
908+
//Start ESP-Now if needed, put ESP-Now into broadcast state
909+
espnowBeginPairing();
907910

908-
//Display 'ESP-Now Pairing' while we wait
911+
changeState(STATE_ESPNOW_PAIRING);
912+
#else
913+
changeState(STATE_ROVER_NOT_STARTED);
914+
#endif
915+
}
916+
break;
917+
918+
case (STATE_ESPNOW_PAIRING):
919+
{
920+
#ifdef COMPILE_ESPNOW
921+
if (espnowIsPaired() == true)
922+
{
923+
paintEspNowPaired();
924+
changeState(STATE_ROVER_NOT_STARTED);
925+
}
926+
else
927+
{
928+
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
929+
espnowSendPairMessage(broadcastMac); //Send unit's MAC address over broadcast, no ack, no encryption
930+
}
931+
#endif
909932
}
910933
break;
911934

@@ -1051,8 +1074,11 @@ void changeState(SystemState newState)
10511074
break;
10521075
#endif //COMPILE_L_BAND
10531076

1054-
case (STATE_ESPNOW_PAIR):
1055-
Serial.print("State: ESP-Now Pair");
1077+
case (STATE_ESPNOW_PAIRING_NOT_STARTED):
1078+
Serial.print("State: ESP-Now Pairing Not Started");
1079+
break;
1080+
case (STATE_ESPNOW_PAIRING):
1081+
Serial.print("State: ESP-Now Pairing");
10561082
break;
10571083

10581084
case (STATE_SHUTDOWN):

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ void ButtonCheckTask(void *e)
412412
case STATE_BUBBLE_LEVEL:
413413
case STATE_WIFI_CONFIG_NOT_STARTED:
414414
case STATE_WIFI_CONFIG:
415-
case STATE_ESPNOW_PAIR:
415+
case STATE_ESPNOW_PAIRING_NOT_STARTED:
416+
case STATE_ESPNOW_PAIRING:
416417
lastSystemState = systemState; //Remember this state to return after we mark an event
417418
requestChangeState(STATE_DISPLAY_SETUP);
418419
setupState = STATE_MARK_EVENT;
@@ -464,9 +465,9 @@ void ButtonCheckTask(void *e)
464465
setupState = STATE_WIFI_CONFIG_NOT_STARTED;
465466
break;
466467
case STATE_WIFI_CONFIG_NOT_STARTED:
467-
setupState = STATE_ESPNOW_PAIR;
468+
setupState = STATE_ESPNOW_PAIRING_NOT_STARTED;
468469
break;
469-
case STATE_ESPNOW_PAIR:
470+
case STATE_ESPNOW_PAIRING_NOT_STARTED:
470471
//If only one active profile do not show any profiles
471472
index = getProfileNumberFromUnit(0);
472473
displayProfile = getProfileNumberFromUnit(1);
@@ -531,7 +532,8 @@ void ButtonCheckTask(void *e)
531532
case STATE_BUBBLE_LEVEL:
532533
case STATE_WIFI_CONFIG_NOT_STARTED:
533534
case STATE_WIFI_CONFIG:
534-
case STATE_ESPNOW_PAIR:
535+
case STATE_ESPNOW_PAIRING_NOT_STARTED:
536+
case STATE_ESPNOW_PAIRING:
535537
lastSystemState = systemState; //Remember this state to return after we mark an event
536538
requestChangeState(STATE_DISPLAY_SETUP);
537539
setupState = STATE_MARK_EVENT;
@@ -583,9 +585,9 @@ void ButtonCheckTask(void *e)
583585
setupState = STATE_WIFI_CONFIG_NOT_STARTED;
584586
break;
585587
case STATE_WIFI_CONFIG_NOT_STARTED:
586-
setupState = STATE_ESPNOW_PAIR;
588+
setupState = STATE_ESPNOW_PAIRING_NOT_STARTED;
587589
break;
588-
case STATE_ESPNOW_PAIR:
590+
case STATE_ESPNOW_PAIRING_NOT_STARTED:
589591
//If only one active profile do not show any profiles
590592
index = getProfileNumberFromUnit(0);
591593
displayProfile = getProfileNumberFromUnit(1);
@@ -618,11 +620,6 @@ void ButtonCheckTask(void *e)
618620
}
619621
}
620622

621-
TODO once a user has selected 'Pair', send this once
622-
//Start ESP-Now if needed, put ESP-Now into broadcast state
623-
espnowBeginPairing();
624-
625-
626623
void idleTask(void *e)
627624
{
628625
int cpu = xPortGetCoreID();

0 commit comments

Comments
 (0)