Skip to content

Commit d75eeae

Browse files
authored
Merge pull request #281 from sparkfun/ESPNowPair
Add E-Pair Submenu
2 parents 45a7872 + fbafee4 commit d75eeae

File tree

10 files changed

+174
-94
lines changed

10 files changed

+174
-94
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: 14 additions & 7 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,12 +2154,12 @@ 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);
21582161
printTextCenter("Config", 12 * 2, QW_FONT_8X16, 1, false);
2159-
printTextCenter("Pair", 12 * 3, QW_FONT_8X16, 1, true);
2162+
printTextCenter("E-Pair", 12 * 3, QW_FONT_8X16, 1, true);
21602163
}
21612164
else if (setupState == STATE_PROFILE)
21622165
paintDisplaySetupProfile("Base");
@@ -2191,12 +2194,12 @@ 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);
21982201
printTextCenter("Config", 12 * 2, QW_FONT_8X16, 1, false);
2199-
printTextCenter("Pair", 12 * 3, QW_FONT_8X16, 1, true);
2202+
printTextCenter("E-Pair", 12 * 3, QW_FONT_8X16, 1, true);
22002203
}
22012204
else if (setupState == STATE_PROFILE)
22022205
paintDisplaySetupProfile("Rover");
@@ -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: 58 additions & 53 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,13 +147,17 @@ 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
{
152-
if(espnowState == ESPNOW_OFF) return;
159+
#ifdef COMPILE_ESPNOW
160+
if (espnowState == ESPNOW_OFF) return;
153161

154162
if (wifiState == WIFI_OFF)
155163
{
@@ -178,13 +186,11 @@ 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

187-
//Begin broadcasting our MAC and wait for remote unit to respond
193+
//Start ESP-Now if needed, put ESP-Now into broadcast state
188194
void espnowBeginPairing()
189195
{
190196
espnowStart();
@@ -194,60 +200,45 @@ void espnowBeginPairing()
194200
espnowAddPeer(broadcastMac, false); // Encryption is not supported for multicast addresses
195201

196202
espnowSetState(ESPNOW_PAIRING);
203+
}
197204

198-
//Begin sending our MAC every 250ms until a remote device sends us there info
199-
randomSeed(millis());
200-
201-
Serial.println("Begin pairing. Place other unit in pairing mode. Press any key to exit.");
202-
while (Serial.available()) Serial.read();
203-
204-
while (1)
205+
//Regularly call during pairing to see if we've received a Pairing message
206+
bool espnowIsPaired()
207+
{
208+
#ifdef COMPILE_ESPNOW
209+
if (espnowState == ESPNOW_MAC_RECEIVED)
205210
{
206-
if (Serial.available()) break;
211+
//Remove broadcast peer
212+
uint8_t broadcastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
213+
espnowRemovePeer(broadcastMac);
207214

208-
int timeout = 1000 + random(0, 100); //Delay 1000 to 1100ms
209-
for (int x = 0 ; x < timeout ; x++)
215+
if (esp_now_is_peer_exist(receivedMAC) == true)
216+
log_d("Peer already exists");
217+
else
210218
{
211-
delay(1);
219+
//Add new peer to system
220+
espnowAddPeer(receivedMAC);
212221

213-
if (espnowState == ESPNOW_MAC_RECEIVED)
214-
{
215-
//Remove broadcast peer
216-
espnowRemovePeer(broadcastMac);
217-
218-
if (esp_now_is_peer_exist(receivedMAC) == true)
219-
log_d("Peer already exists");
220-
else
221-
{
222-
//Add new peer to system
223-
espnowAddPeer(receivedMAC);
224-
225-
//Record this MAC to peer list
226-
memcpy(settings.espnowPeers[settings.espnowPeerCount], receivedMAC, 6);
227-
settings.espnowPeerCount++;
228-
settings.espnowPeerCount %= ESPNOW_MAX_PEERS;
229-
}
230-
231-
//Send message directly to the received MAC (not unicast), then exit
232-
espnowSendPairMessage(receivedMAC);
233-
234-
espnowSetState(ESPNOW_PAIRED);
235-
Serial.println("Pairing compete");
236-
return;
237-
}
222+
//Record this MAC to peer list
223+
memcpy(settings.espnowPeers[settings.espnowPeerCount], receivedMAC, 6);
224+
settings.espnowPeerCount++;
225+
settings.espnowPeerCount %= ESPNOW_MAX_PEERS;
238226
}
239227

240-
espnowSendPairMessage(broadcastMac); //Send unit's MAC address over broadcast, no ack, no encryption
228+
//Send message directly to the received MAC (not unicast), then exit
229+
espnowSendPairMessage(receivedMAC);
241230

242-
Serial.println("Scanning for other radio...");
231+
espnowSetState(ESPNOW_PAIRED);
232+
return (true);
243233
}
244-
245-
Serial.println("User pressed button. Pairing canceled.");
234+
#endif
235+
return (false);
246236
}
247237

248238
//Create special pair packet to a given MAC
249239
esp_err_t espnowSendPairMessage(uint8_t *sendToMac)
250240
{
241+
#ifdef COMPILE_ESPNOW
251242
// Assemble message to send
252243
PairMessage pairMessage;
253244

@@ -263,6 +254,9 @@ esp_err_t espnowSendPairMessage(uint8_t *sendToMac)
263254
pairMessage.crc += unitMACAddress[x];
264255

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

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

274268
esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
275269
{
270+
#ifdef COMPILE_ESPNOW
276271
esp_now_peer_info_t peerInfo;
277272

278273
memcpy(peerInfo.peer_addr, peerMac, 6);
@@ -286,15 +281,23 @@ esp_err_t espnowAddPeer(uint8_t *peerMac, bool encrypt)
286281
if (result != ESP_OK)
287282
log_d("Failed to add peer");
288283
return (result);
284+
#else
285+
return (ESP_OK);
286+
#endif
289287
}
290288

291289
//Remove a given MAC address from the peer list
292290
esp_err_t espnowRemovePeer(uint8_t *peerMac)
293291
{
292+
#ifdef COMPILE_ESPNOW
294293
esp_err_t result = esp_now_del_peer(peerMac);
295294
if (result != ESP_OK)
296295
log_d("Failed to remove peer");
297296
return (result);
297+
#else
298+
return (ESP_OK);
299+
#endif
300+
298301
}
299302

300303
//Update the state of the ESP Now state machine
@@ -303,6 +306,8 @@ void espnowSetState(ESPNOWState newState)
303306
if (espnowState == newState)
304307
Serial.print("*");
305308
espnowState = newState;
309+
310+
Serial.print("espnowState: ");
306311
switch (newState)
307312
{
308313
case ESPNOW_OFF:
@@ -328,6 +333,7 @@ void espnowSetState(ESPNOWState newState)
328333

329334
void espnowProcessRTCM(byte incoming)
330335
{
336+
#ifdef COMPILE_ESPNOW
331337
if (espnowState == ESPNOW_PAIRED)
332338
{
333339
//Move this byte into ESP NOW to send buffer
@@ -344,6 +350,5 @@ void espnowProcessRTCM(byte incoming)
344350
espnowOutgoingRTCM = true;
345351
}
346352
}
353+
#endif
347354
}
348-
349-
#endif //ifdef COMPILE_ESPNOW

Firmware/RTK_Surveyor/Form.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ void createSettingsString(char* settingsCSV)
387387
char apDaysRemaining[20];
388388
if (strlen(settings.pointPerfectCurrentKey) > 0)
389389
{
390+
#ifdef COMPILE_L_BAND
390391
uint8_t daysRemaining = daysFromEpoch(settings.pointPerfectNextKeyStart + settings.pointPerfectNextKeyDuration + 1);
391392
sprintf(apDaysRemaining, "%d", daysRemaining);
393+
#endif
392394
}
393395
else
394396
sprintf(apDaysRemaining, "No Keys");

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 3 additions & 3 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_AP //Requires WiFi. Comment out to remove Access Point functionality
30+
#define COMPILE_ESPNOW //Requires WiFi. Comment out to remove ESP-Now functionality.
2931
#define COMPILE_BT //Comment out to remove Bluetooth functionality
30-
#define COMPILE_AP //Comment out to remove Access Point functionality
3132
#define COMPILE_L_BAND //Comment out to remove L-Band functionality
32-
#define COMPILE_ESPNOW //Comment out to remove ESP-Now functionality
3333
#define COMPILE_IDLE_TASKS //Comment out to remove idle tasks
3434
#define ENABLE_DEVELOPER //Uncomment this line to enable special developer modes (don't check power button at startup)
3535

@@ -468,7 +468,7 @@ bool espnowOutgoingRTCM = false;
468468
| RTCM |--->|-->| |--------->| |-->|----->|TXD, MISO | |
469469
| | | | Bluetooth | | UART 2 | | | UART1 | |
470470
| NMEA + RTCM |<---|<--| |<-------+-| |<--|<-----|RXD, MOSI |<----'
471-
+-------------+ | '-----------' | '--------' |28 42| |
471+
+-------------+ | '-----------' | '--------' |28 43| |
472472
| | | | |
473473
.---------+ | | | | |
474474
/ uSD Card | | | | | |

0 commit comments

Comments
 (0)