2
2
ESPNOW.ino
3
3
4
4
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.
5
17
**********************************************************************/
6
18
7
19
#ifdef COMPILE_ESPNOW
10
22
// Constants
11
23
// ****************************************
12
24
13
- const uint8_t peerBroadcast [6 ] = {0xff , 0xff , 0xff , 0xff , 0xff , 0xff };
25
+ const uint8_t espnowBroadcastAddr [6 ] = {0xff , 0xff , 0xff , 0xff , 0xff , 0xff };
14
26
15
27
// ****************************************
16
28
// Types
@@ -29,10 +41,7 @@ typedef struct _ESP_NOW_PAIR_MESSAGE
29
41
// Locals
30
42
// ****************************************
31
43
32
- bool espNowDebug;
33
- bool espNowDisplay;
34
44
ESPNOWState espNowState;
35
- bool espNowVerbose;
36
45
37
46
// *********************************************************************
38
47
// Add a peer to the ESP-NOW network
@@ -47,22 +56,22 @@ esp_err_t espNowAddPeer(const uint8_t * peerMac)
47
56
peerInfo.encrypt = false ;
48
57
49
58
// Add the peer
50
- if (espNowDebug )
59
+ if (settings. debugEspNow )
51
60
systemPrintf (" Calling esp_now_add_peer\r\n " );
52
61
esp_err_t result = esp_now_add_peer (&peerInfo);
53
62
if (result != ESP_OK)
54
63
{
55
64
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 ],
59
68
result);
60
69
}
61
- else if (espNowDebug )
70
+ else if (settings. debugEspNow )
62
71
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 ]);
66
75
return result;
67
76
}
68
77
@@ -197,16 +206,13 @@ void espNowRxHandler(const esp_now_recv_info *mac,
197
206
// else Pair CRC failed
198
207
}
199
208
}
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);
210
216
}
211
217
212
218
// *********************************************************************
@@ -227,7 +233,7 @@ void espNowSetState(ESPNOWState newState)
227
233
const char * oldName;
228
234
char oLine[80 ];
229
235
230
- if (espNowDebug == true )
236
+ if (settings. debugEspNow == true )
231
237
{
232
238
// Get the old state name
233
239
if (espNowState < ESPNOW_MAX)
@@ -269,7 +275,7 @@ bool espNowStart()
269
275
started = false ;
270
276
271
277
// 5. Call esp_now_init
272
- if (espNowDebug && espNowVerbose )
278
+ if (settings. debugEspNow )
273
279
systemPrintf (" Calling esp_now_init\r\n " );
274
280
status = esp_now_init ();
275
281
if (status != ESP_OK)
@@ -279,7 +285,7 @@ bool espNowStart()
279
285
}
280
286
281
287
// 9. Set receive callback [esp_now_register_recv_cb(espnowOnDataReceived)]
282
- if (espNowDebug && espNowVerbose )
288
+ if (settings. debugEspNow )
283
289
systemPrintf (" Calling esp_now_register_recv_cb\r\n " );
284
290
status = esp_now_register_recv_cb (espNowRxHandler);
285
291
if (status != ESP_OK)
@@ -290,20 +296,20 @@ bool espNowStart()
290
296
291
297
// 10. Add peers from settings
292
298
// i. Set ESP-NOW state, call espNowSetState(ESPNOW_PAIRED)
293
- if (espNowDebug && espNowVerbose )
299
+ if (settings. debugEspNow )
294
300
systemPrintf (" Calling espNowSetState\r\n " );
295
301
espNowSetState (ESPNOW_PAIRED);
296
302
297
303
// ii. Loop through peers listed in settings, for each
298
304
for (index = 0 ; index < ESPNOW_MAX_PEERS; index++)
299
305
{
300
306
// a. Determine if peer exists, call esp_now_is_peer_exist
301
- if (espNowDebug && espNowVerbose )
307
+ if (settings. debugEspNow )
302
308
systemPrintf (" Calling esp_now_is_peer_exist\r\n " );
303
309
if (esp_now_is_peer_exist (settings.espnowPeers [index]) == false )
304
310
{
305
311
// b. Add peer if necessary, call espnowAddPeer
306
- if (espNowDebug && espNowVerbose )
312
+ if (settings. debugEspNow )
307
313
systemPrintf (" Calling espNowAddPeer\r\n " );
308
314
status = espNowAddPeer (&settings.espnowPeers [index][0 ]);
309
315
if (status != ESP_OK)
@@ -326,7 +332,7 @@ bool espNowStart()
326
332
}
327
333
328
334
// ESP-NOW has started successfully
329
- if (espNowDebug )
335
+ if (settings. debugEspNow )
330
336
systemPrintf (" ESP-NOW online\r\n " );
331
337
332
338
started = true ;
@@ -352,34 +358,34 @@ bool espNowStop()
352
358
stopped = false ;
353
359
354
360
// 3. esp_now_unregister_recv_cb()
355
- if (espNowDebug )
361
+ if (settings. debugEspNow )
356
362
systemPrintf (" Calling esp_now_unregister_recv_cb\r\n " );
357
363
status = esp_now_unregister_recv_cb ();
358
364
if (status != ESP_OK)
359
365
{
360
366
systemPrintf (" ERROR: Failed to clear ESP_NOW RX callback, status: %d\r\n " , status);
361
367
break ;
362
368
}
363
- if (espNowDebug && espNowVerbose )
369
+ if (settings. debugEspNow )
364
370
systemPrintf (" ESP-NOW: RX callback removed\r\n " );
365
371
366
- if (espNowDisplay )
372
+ if (settings. debugEspNow )
367
373
systemPrintf (" ESP-NOW offline\r\n " );
368
374
369
375
// 4. Remove all peers by calling espnowRemovePeer
370
- if (espNowDebug && espNowVerbose )
376
+ if (settings. debugEspNow )
371
377
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 ))
373
379
{
374
- if (espNowDebug && espNowVerbose )
380
+ if (settings. debugEspNow )
375
381
systemPrintf (" Calling esp_now_del_peer\r\n " );
376
- status = esp_now_del_peer (peerBroadcast );
382
+ status = esp_now_del_peer (espnowBroadcastAddr );
377
383
if (status != ESP_OK)
378
384
{
379
385
systemPrintf (" ERROR: Failed to delete broadcast peer, status: %d\r\n " , status);
380
386
break ;
381
387
}
382
- if (espNowDebug && espNowVerbose )
388
+ if (settings. debugEspNow )
383
389
systemPrintf (" ESP-NOW removed broadcast peer\r\n " );
384
390
}
385
391
@@ -389,14 +395,14 @@ bool espNowStop()
389
395
esp_now_peer_info_t peerInfo;
390
396
391
397
// Get the next unicast peer
392
- if (espNowDebug && espNowVerbose )
398
+ if (settings. debugEspNow )
393
399
systemPrintf (" Calling esp_now_fetch_peer\r\n " );
394
400
status = esp_now_fetch_peer (true , &peerInfo);
395
401
if (status != ESP_OK)
396
402
break ;
397
403
398
404
// Remove the unicast peer
399
- if (espNowDebug && espNowVerbose )
405
+ if (settings. debugEspNow )
400
406
systemPrintf (" Calling esp_now_del_peer\r\n " );
401
407
status = esp_now_del_peer (peerInfo.peer_addr );
402
408
if (status != ESP_OK)
@@ -408,7 +414,7 @@ bool espNowStop()
408
414
status);
409
415
break ;
410
416
}
411
- if (espNowDebug && espNowVerbose )
417
+ if (settings. debugEspNow )
412
418
systemPrintf (" ESP-NOW removed peer %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
413
419
peerInfo.peer_addr [0 ], peerInfo.peer_addr [1 ],
414
420
peerInfo.peer_addr [2 ], peerInfo.peer_addr [3 ],
@@ -421,7 +427,7 @@ bool espNowStop()
421
427
}
422
428
423
429
// Get the number of peers
424
- if (espNowDebug && espNowVerbose )
430
+ if (settings. debugEspNow )
425
431
{
426
432
systemPrintf (" Calling esp_now_get_peer_num\r\n " );
427
433
status = esp_now_get_peer_num (&peerCount);
@@ -437,7 +443,7 @@ bool espNowStop()
437
443
espNowSetState (ESPNOW_OFF);
438
444
439
445
// 9. Turn off ESP-NOW. call esp_now_deinit
440
- if (espNowDebug && espNowVerbose )
446
+ if (settings. debugEspNow )
441
447
systemPrintf (" Calling esp_now_deinit\r\n " );
442
448
status = esp_now_deinit ();
443
449
if (status != ESP_OK)
@@ -449,7 +455,7 @@ bool espNowStop()
449
455
// 11. Restart WiFi if necessary
450
456
451
457
// ESP-NOW has stopped successfully
452
- if (espNowDebug )
458
+ if (settings. debugEspNow )
453
459
systemPrintf (" ESP-NOW stopped\r\n " );
454
460
stopped = true ;
455
461
} while (0 );
0 commit comments