Skip to content

Commit 87e1188

Browse files
authored
Merge pull request #532 from LeeLeahy2/minor-changes
Minor changes: Move code, update comments, rename routines, use settings
2 parents 7dd8628 + c8a1b3b commit 87e1188

File tree

2 files changed

+122
-118
lines changed

2 files changed

+122
-118
lines changed

Firmware/RTK_Everywhere/ESPNOW.ino

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
ESPNOW.ino
33
44
Handle the ESP-NOW events
5+
Use ESP NOW protocol to transmit RTCM between RTK Products via 2.4GHz
6+
7+
How pairing works:
8+
1. Device enters pairing mode
9+
2. Device adds the broadcast MAC (all 0xFFs) as peer
10+
3. Device waits for incoming pairing packet from remote
11+
4. If valid pairing packet received, add peer, immediately transmit a pairing packet to that peer and exit.
12+
13+
ESP NOW is bare metal, there is no guaranteed packet delivery. For RTCM byte transmissions using ESP NOW:
14+
We don't care about dropped packets or packets out of order. The ZED will check the integrity of the RTCM packet.
15+
We don't care if the ESP NOW packet is corrupt or not. RTCM has its own CRC. RTK needs valid RTCM once every
16+
few seconds so a single dropped frame is not critical.
517
**********************************************************************/
618

719
#ifdef COMPILE_ESPNOW
@@ -10,7 +22,7 @@
1022
// Constants
1123
//****************************************
1224

13-
const uint8_t peerBroadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
25+
const uint8_t espnowBroadcastAddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1426

1527
//****************************************
1628
// Types
@@ -29,10 +41,7 @@ typedef struct _ESP_NOW_PAIR_MESSAGE
2941
// Locals
3042
//****************************************
3143

32-
bool espNowDebug;
33-
bool espNowDisplay;
3444
ESPNOWState espNowState;
35-
bool espNowVerbose;
3645

3746
//*********************************************************************
3847
// Add a peer to the ESP-NOW network
@@ -47,22 +56,22 @@ esp_err_t espNowAddPeer(const uint8_t * peerMac)
4756
peerInfo.encrypt = false;
4857

4958
// Add the peer
50-
if (espNowDebug)
59+
if (settings.debugEspNow)
5160
systemPrintf("Calling esp_now_add_peer\r\n");
5261
esp_err_t result = esp_now_add_peer(&peerInfo);
5362
if (result != ESP_OK)
5463
{
5564
systemPrintf("ERROR: Failed to add ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x, result: %d\r\n",
56-
peerBroadcast[0], peerBroadcast[1],
57-
peerBroadcast[2], peerBroadcast[3],
58-
peerBroadcast[4], peerBroadcast[5],
65+
espnowBroadcastAddr[0], espnowBroadcastAddr[1],
66+
espnowBroadcastAddr[2], espnowBroadcastAddr[3],
67+
espnowBroadcastAddr[4], espnowBroadcastAddr[5],
5968
result);
6069
}
61-
else if (espNowDebug)
70+
else if (settings.debugEspNow)
6271
systemPrintf("Added ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x\r\n",
63-
peerBroadcast[0], peerBroadcast[1],
64-
peerBroadcast[2], peerBroadcast[3],
65-
peerBroadcast[4], peerBroadcast[5]);
72+
espnowBroadcastAddr[0], espnowBroadcastAddr[1],
73+
espnowBroadcastAddr[2], espnowBroadcastAddr[3],
74+
espnowBroadcastAddr[4], espnowBroadcastAddr[5]);
6675
return result;
6776
}
6877

@@ -197,16 +206,13 @@ void espNowRxHandler(const esp_now_recv_info *mac,
197206
// else Pair CRC failed
198207
}
199208
}
200-
else
201-
{
202-
if (espNowDisplay == true)
203-
systemPrintf("*** ESP-NOW: RX %02x:%02x:%02x:%02x:%02x:%02x --> %02x:%02x:%02x:%02x:%02x:%02x, %d bytes, rssi: %d\r\n",
204-
mac->src_addr[0], mac->src_addr[1], mac->src_addr[2],
205-
mac->src_addr[3], mac->src_addr[4], mac->src_addr[5],
206-
mac->des_addr[0], mac->des_addr[1], mac->des_addr[2],
207-
mac->des_addr[3], mac->des_addr[4], mac->des_addr[5],
208-
len, packetRSSI);
209-
}
209+
else if (settings.debugEspNow)
210+
systemPrintf("*** ESP-NOW: RX %02x:%02x:%02x:%02x:%02x:%02x --> %02x:%02x:%02x:%02x:%02x:%02x, %d bytes, rssi: %d\r\n",
211+
mac->src_addr[0], mac->src_addr[1], mac->src_addr[2],
212+
mac->src_addr[3], mac->src_addr[4], mac->src_addr[5],
213+
mac->des_addr[0], mac->des_addr[1], mac->des_addr[2],
214+
mac->des_addr[3], mac->des_addr[4], mac->des_addr[5],
215+
len, packetRSSI);
210216
}
211217

212218
//*********************************************************************
@@ -227,7 +233,7 @@ void espNowSetState(ESPNOWState newState)
227233
const char * oldName;
228234
char oLine[80];
229235

230-
if (espNowDebug == true)
236+
if (settings.debugEspNow == true)
231237
{
232238
// Get the old state name
233239
if (espNowState < ESPNOW_MAX)
@@ -269,7 +275,7 @@ bool espNowStart()
269275
started = false;
270276

271277
// 5. Call esp_now_init
272-
if (espNowDebug && espNowVerbose)
278+
if (settings.debugEspNow)
273279
systemPrintf("Calling esp_now_init\r\n");
274280
status = esp_now_init();
275281
if (status != ESP_OK)
@@ -279,7 +285,7 @@ bool espNowStart()
279285
}
280286

281287
// 9. Set receive callback [esp_now_register_recv_cb(espnowOnDataReceived)]
282-
if (espNowDebug && espNowVerbose)
288+
if (settings.debugEspNow)
283289
systemPrintf("Calling esp_now_register_recv_cb\r\n");
284290
status = esp_now_register_recv_cb(espNowRxHandler);
285291
if (status != ESP_OK)
@@ -290,20 +296,20 @@ bool espNowStart()
290296

291297
// 10. Add peers from settings
292298
// i. Set ESP-NOW state, call espNowSetState(ESPNOW_PAIRED)
293-
if (espNowDebug && espNowVerbose)
299+
if (settings.debugEspNow)
294300
systemPrintf("Calling espNowSetState\r\n");
295301
espNowSetState(ESPNOW_PAIRED);
296302

297303
// ii. Loop through peers listed in settings, for each
298304
for (index = 0; index < ESPNOW_MAX_PEERS; index++)
299305
{
300306
// a. Determine if peer exists, call esp_now_is_peer_exist
301-
if (espNowDebug && espNowVerbose)
307+
if (settings.debugEspNow)
302308
systemPrintf("Calling esp_now_is_peer_exist\r\n");
303309
if (esp_now_is_peer_exist(settings.espnowPeers[index]) == false)
304310
{
305311
// b. Add peer if necessary, call espnowAddPeer
306-
if (espNowDebug && espNowVerbose)
312+
if (settings.debugEspNow)
307313
systemPrintf("Calling espNowAddPeer\r\n");
308314
status = espNowAddPeer(&settings.espnowPeers[index][0]);
309315
if (status != ESP_OK)
@@ -326,7 +332,7 @@ bool espNowStart()
326332
}
327333

328334
// ESP-NOW has started successfully
329-
if (espNowDebug)
335+
if (settings.debugEspNow)
330336
systemPrintf("ESP-NOW online\r\n");
331337

332338
started = true;
@@ -352,34 +358,34 @@ bool espNowStop()
352358
stopped = false;
353359

354360
// 3. esp_now_unregister_recv_cb()
355-
if (espNowDebug)
361+
if (settings.debugEspNow)
356362
systemPrintf("Calling esp_now_unregister_recv_cb\r\n");
357363
status = esp_now_unregister_recv_cb();
358364
if (status != ESP_OK)
359365
{
360366
systemPrintf("ERROR: Failed to clear ESP_NOW RX callback, status: %d\r\n", status);
361367
break;
362368
}
363-
if (espNowDebug && espNowVerbose)
369+
if (settings.debugEspNow)
364370
systemPrintf("ESP-NOW: RX callback removed\r\n");
365371

366-
if (espNowDisplay)
372+
if (settings.debugEspNow)
367373
systemPrintf("ESP-NOW offline\r\n");
368374

369375
// 4. Remove all peers by calling espnowRemovePeer
370-
if (espNowDebug && espNowVerbose)
376+
if (settings.debugEspNow)
371377
systemPrintf("Calling esp_now_is_peer_exist\r\n");
372-
if (esp_now_is_peer_exist(peerBroadcast))
378+
if (esp_now_is_peer_exist(espnowBroadcastAddr))
373379
{
374-
if (espNowDebug && espNowVerbose)
380+
if (settings.debugEspNow)
375381
systemPrintf("Calling esp_now_del_peer\r\n");
376-
status = esp_now_del_peer(peerBroadcast);
382+
status = esp_now_del_peer(espnowBroadcastAddr);
377383
if (status != ESP_OK)
378384
{
379385
systemPrintf("ERROR: Failed to delete broadcast peer, status: %d\r\n", status);
380386
break;
381387
}
382-
if (espNowDebug && espNowVerbose)
388+
if (settings.debugEspNow)
383389
systemPrintf("ESP-NOW removed broadcast peer\r\n");
384390
}
385391

@@ -389,14 +395,14 @@ bool espNowStop()
389395
esp_now_peer_info_t peerInfo;
390396

391397
// Get the next unicast peer
392-
if (espNowDebug && espNowVerbose)
398+
if (settings.debugEspNow)
393399
systemPrintf("Calling esp_now_fetch_peer\r\n");
394400
status = esp_now_fetch_peer(true, &peerInfo);
395401
if (status != ESP_OK)
396402
break;
397403

398404
// Remove the unicast peer
399-
if (espNowDebug && espNowVerbose)
405+
if (settings.debugEspNow)
400406
systemPrintf("Calling esp_now_del_peer\r\n");
401407
status = esp_now_del_peer(peerInfo.peer_addr);
402408
if (status != ESP_OK)
@@ -408,7 +414,7 @@ bool espNowStop()
408414
status);
409415
break;
410416
}
411-
if (espNowDebug && espNowVerbose)
417+
if (settings.debugEspNow)
412418
systemPrintf("ESP-NOW removed peer %02x:%02x:%02x:%02x:%02x:%02x\r\n",
413419
peerInfo.peer_addr[0], peerInfo.peer_addr[1],
414420
peerInfo.peer_addr[2], peerInfo.peer_addr[3],
@@ -421,7 +427,7 @@ bool espNowStop()
421427
}
422428

423429
// Get the number of peers
424-
if (espNowDebug && espNowVerbose)
430+
if (settings.debugEspNow)
425431
{
426432
systemPrintf("Calling esp_now_get_peer_num\r\n");
427433
status = esp_now_get_peer_num(&peerCount);
@@ -437,7 +443,7 @@ bool espNowStop()
437443
espNowSetState(ESPNOW_OFF);
438444

439445
// 9. Turn off ESP-NOW. call esp_now_deinit
440-
if (espNowDebug && espNowVerbose)
446+
if (settings.debugEspNow)
441447
systemPrintf("Calling esp_now_deinit\r\n");
442448
status = esp_now_deinit();
443449
if (status != ESP_OK)
@@ -449,7 +455,7 @@ bool espNowStop()
449455
// 11. Restart WiFi if necessary
450456

451457
// ESP-NOW has stopped successfully
452-
if (espNowDebug)
458+
if (settings.debugEspNow)
453459
systemPrintf("ESP-NOW stopped\r\n");
454460
stopped = true;
455461
} while (0);

0 commit comments

Comments
 (0)