12
12
13
13
const uint8_t peerBroadcast[6 ] = {0xff , 0xff , 0xff , 0xff , 0xff , 0xff };
14
14
15
+ // ****************************************
16
+ // Types
17
+ // ****************************************
18
+
19
+ // Create a struct for ESP NOW pairing
20
+ typedef struct _ESP_NOW_PAIR_MESSAGE
21
+ {
22
+ uint8_t macAddress[6 ];
23
+ bool encrypt;
24
+ uint8_t channel;
25
+ uint8_t crc; // Simple check - add MAC together and limit to 8 bit
26
+ } ESP_NOW_PAIR_MESSAGE;
27
+
15
28
// ****************************************
16
29
// Locals
17
30
// ****************************************
18
31
19
32
bool espNowDebug;
20
33
bool espNowDisplay;
21
- ESP_NOW_STATE espNowState;
34
+ ESPNOWState espNowState;
22
35
bool espNowVerbose;
23
36
24
37
// *********************************************************************
@@ -57,7 +70,7 @@ esp_err_t espNowAddPeer(const uint8_t * peerMac)
57
70
// Get the current ESP-NOW state
58
71
ESPNOWState espnowGetState ()
59
72
{
60
- return espnowState ;
73
+ return espNowState ;
61
74
}
62
75
63
76
// ----------------------------------------------------------------------
@@ -94,8 +107,8 @@ ESPNOWState espnowGetState()
94
107
// A. Validate message CRC
95
108
// B. If valid CRC
96
109
// i. Save peer MAC address
97
- // ii. espnowSetState(ESP_NOW_MAC_RECEIVED )
98
- // 14. Else if ESP_NOW_MAC_RECEIVED state
110
+ // ii. espnowSetState(ESPNOW_MAC_RECEIVED )
111
+ // 14. Else if ESPNOW_MAC_RECEIVED state
99
112
// A. If ESP-NOW is corrections source, correctionLastSeen(CORR_ESPNOW)
100
113
// i. gnss->pushRawData
101
114
// 15. Set espnowIncomingRTCM
@@ -110,44 +123,44 @@ ESPNOWState espnowGetState()
110
123
// 7. esp_wifi_get_protocol
111
124
// 8. Turn off long range protocol if necessary, call esp_wifi_set_protocol
112
125
// 9. Turn off ESP-NOW. call esp_now_deinit
113
- // 10. Set ESP-NOW state, call espnowSetState(ESP_NOW_OFF )
126
+ // 10. Set ESP-NOW state, call espnowSetState(ESPNOW_OFF )
114
127
// 11. Restart WiFi if necessary
115
128
// ----------------------------------------------------------------------
116
129
117
130
// *********************************************************************
118
131
// Update the state of the ESP Now state machine
119
132
//
120
- // +--------------------- +
121
- // | ESP_NOW_OFF |
122
- // +--------------------- +
133
+ // +--------------------+
134
+ // | ESPNOW_OFF |
135
+ // +--------------------+
123
136
// | |
124
137
// | | No pairs listed
125
138
// | V
126
- // | +---------------------- +
127
- // | | ESP_NOW_BROADCASTING |
128
- // | +---------------------- +
139
+ // | +---------------------+
140
+ // | | ESPNOW_BROADCASTING |
141
+ // | +---------------------+
129
142
// | |
130
143
// | |
131
144
// | V
132
- // | +--------------------- +
133
- // | | ESP_NOW_PAIRING |
134
- // | +--------------------- +
145
+ // | +--------------------+
146
+ // | | ESPNOW_PAIRING |
147
+ // | +--------------------+
135
148
// | |
136
149
// | |
137
150
// | V
138
- // | +--------------------- +
139
- // | | ESP_NOW_MAC_RECEIVED |
140
- // | +--------------------- +
151
+ // | +--------------------+
152
+ // | | ESPNOW_MAC_RECEIVED |
153
+ // | +--------------------+
141
154
// | |
142
155
// | |
143
156
// | V
144
- // | +--------------------- +
145
- // '->| ESP_NOW_PAIRED |
146
- // +--------------------- +
157
+ // | +--------------------+
158
+ // '->| ESPNOW_PAIRED |
159
+ // +--------------------+
147
160
//
148
- // Send RTCM in either ESP_NOW_BROADCASTING or ESP_NOW_PAIRED state.
149
- // Receive RTCM in ESP_NOW_BROADCASTING, ESP_NOW_MAC_RECEIVED and
150
- // ESP_NOW_PAIRED states.
161
+ // Send RTCM in either ESPNOW_BROADCASTING or ESPNOW_PAIRED state.
162
+ // Receive RTCM in ESPNOW_BROADCASTING, ESPNOW_MAC_RECEIVED and
163
+ // ESPNOW_PAIRED states.
151
164
// *********************************************************************
152
165
153
166
// *********************************************************************
@@ -164,7 +177,7 @@ void espNowRxHandler(const esp_now_recv_info *mac,
164
177
165
178
uint8_t receivedMAC[6 ];
166
179
167
- if (espNowState == ESP_NOW_PAIRING )
180
+ if (espNowState == ESPNOW_PAIRING )
168
181
{
169
182
ESP_NOW_PAIR_MESSAGE pairMessage;
170
183
if (len == sizeof (pairMessage)) // First error check
@@ -179,7 +192,7 @@ void espNowRxHandler(const esp_now_recv_info *mac,
179
192
if (tempCRC == pairMessage.crc ) // 2nd error check
180
193
{
181
194
memcpy (&receivedMAC, pairMessage.macAddress , 6 );
182
- espNowSetState (ESP_NOW_MAC_RECEIVED );
195
+ espNowSetState (ESPNOW_MAC_RECEIVED );
183
196
}
184
197
// else Pair CRC failed
185
198
}
@@ -198,15 +211,15 @@ void espNowRxHandler(const esp_now_recv_info *mac,
198
211
199
212
// *********************************************************************
200
213
// Update the state of the ESP-NOW subsystem
201
- void espNowSetState (ESP_NOW_STATE newState)
214
+ void espNowSetState (ESPNOWState newState)
202
215
{
203
216
const char * name[] =
204
217
{
205
- " ESP_NOW_OFF " ,
206
- " ESP_NOW_BROADCASTING " ,
207
- " ESP_NOW_PAIRING " ,
208
- " ESP_NOW_MAC_RECEIVED " ,
209
- " ESP_NOW_PAIRED " ,
218
+ " ESPNOW_OFF " ,
219
+ " ESPNOW_BROADCASTING " ,
220
+ " ESPNOW_PAIRING " ,
221
+ " ESPNOW_MAC_RECEIVED " ,
222
+ " ESPNOW_PAIRED " ,
210
223
};
211
224
const int nameCount = sizeof (name) / sizeof (name[0 ]);
212
225
const char * newName;
@@ -217,7 +230,7 @@ void espNowSetState(ESP_NOW_STATE newState)
217
230
if (espNowDebug == true )
218
231
{
219
232
// Get the old state name
220
- if (espNowState < ESP_NOW_MAX )
233
+ if (espNowState < ESPNOW_MAX )
221
234
oldName = name[espNowState];
222
235
else
223
236
{
@@ -226,7 +239,7 @@ void espNowSetState(ESP_NOW_STATE newState)
226
239
}
227
240
228
241
// Get the new state name
229
- if (newState < ESP_NOW_MAX )
242
+ if (newState < ESPNOW_MAX )
230
243
newName = name[newState];
231
244
else
232
245
{
@@ -276,10 +289,10 @@ bool espNowStart()
276
289
}
277
290
278
291
// 10. Add peers from settings
279
- // i. Set ESP-NOW state, call espNowSetState(ESP_NOW_PAIRED )
292
+ // i. Set ESP-NOW state, call espNowSetState(ESPNOW_PAIRED )
280
293
if (espNowDebug && espNowVerbose)
281
294
systemPrintf (" Calling espNowSetState\r\n " );
282
- espNowSetState (ESP_NOW_PAIRED );
295
+ espNowSetState (ESPNOW_PAIRED );
283
296
284
297
// ii. Loop through peers listed in settings, for each
285
298
for (index = 0 ; index < ESPNOW_MAX_PEERS; index++)
@@ -421,7 +434,7 @@ bool espNowStop()
421
434
}
422
435
423
436
// Stop the ESP-NOW state machine
424
- espNowSetState (ESP_NOW_OFF );
437
+ espNowSetState (ESPNOW_OFF );
425
438
426
439
// 9. Turn off ESP-NOW. call esp_now_deinit
427
440
if (espNowDebug && espNowVerbose)
@@ -467,15 +480,15 @@ void updateEspnow()
467
480
{
468
481
if (settings.enableEspNow == true )
469
482
{
470
- if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING)
483
+ if (espNowState == ESPNOW_PAIRED || espNowState == ESPNOW_BROADCASTING)
471
484
{
472
485
// If it's been longer than a few ms since we last added a byte to the buffer
473
486
// then we've reached the end of the RTCM stream. Send partial buffer.
474
487
if (espnowOutgoingSpot > 0 && (millis () - espnowLastAdd) > 50 )
475
488
{
476
- if (espnowState == ESPNOW_PAIRED)
489
+ if (espNowState == ESPNOW_PAIRED)
477
490
esp_now_send (0 , (uint8_t *)&espnowOutgoing, espnowOutgoingSpot); // Send partial packet to all peers
478
- else // if (espnowState == ESPNOW_BROADCASTING)
491
+ else // if (espNowState == ESPNOW_BROADCASTING)
479
492
{
480
493
uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
481
494
esp_now_send (broadcastMac, (uint8_t *)&espnowOutgoing,
@@ -499,20 +512,11 @@ void updateEspnow()
499
512
}
500
513
}
501
514
502
- // Create a struct for ESP NOW pairing
503
- typedef struct PairMessage
504
- {
505
- uint8_t macAddress[6 ];
506
- bool encrypt;
507
- uint8_t channel;
508
- uint8_t crc; // Simple check - add MAC together and limit to 8 bit
509
- } PairMessage;
510
-
511
515
// Callback when data is sent
512
516
void espnowOnDataSent (const uint8_t *mac_addr, esp_now_send_status_t status)
513
517
{
514
518
// systemPrint("Last Packet Send Status: ");
515
- // if (status == ESP_NOW_SEND_SUCCESS )
519
+ // if (status == ESPNOW_SEND_SUCCESS )
516
520
// systemPrintln("Delivery Success");
517
521
// else
518
522
// systemPrintln("Delivery Fail");
@@ -521,11 +525,11 @@ void espnowOnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
521
525
// Callback when data is received
522
526
void espnowOnDataReceived (const esp_now_recv_info *mac, const uint8_t *incomingData, int len)
523
527
{
524
- if (espnowState == ESPNOW_PAIRING)
528
+ if (espNowState == ESPNOW_PAIRING)
525
529
{
526
- if (len == sizeof (PairMessage )) // First error check
530
+ if (len == sizeof (ESP_NOW_PAIR_MESSAGE )) // First error check
527
531
{
528
- PairMessage pairMessage;
532
+ ESP_NOW_PAIR_MESSAGE pairMessage;
529
533
memcpy (&pairMessage, incomingData, sizeof (pairMessage));
530
534
531
535
// Check CRC
@@ -601,7 +605,7 @@ void espnowStart()
601
605
if (response != ESP_OK)
602
606
systemPrintf (" espnowStart: Failed to get protocols: %s\r\n " , esp_err_to_name (response));
603
607
604
- if ((WIFI_IS_RUNNING () == false ) && espnowState == ESPNOW_OFF)
608
+ if ((WIFI_IS_RUNNING () == false ) && espNowState == ESPNOW_OFF)
605
609
{
606
610
// Radio is off, turn it on
607
611
if (protocols != (WIFI_PROTOCOL_LR))
@@ -622,7 +626,7 @@ void espnowStart()
622
626
}
623
627
}
624
628
// If WiFi is on but ESP NOW is off, then enable LR protocol
625
- else if (WIFI_IS_RUNNING () && espnowState == ESPNOW_OFF)
629
+ else if (WIFI_IS_RUNNING () && espNowState == ESPNOW_OFF)
626
630
{
627
631
// Enable WiFi + ESP-Now
628
632
// Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
@@ -739,7 +743,7 @@ void espnowStart()
739
743
// If WiFi is off, stop the radio entirely
740
744
void espnowStop ()
741
745
{
742
- if (espnowState == ESPNOW_OFF)
746
+ if (espNowState == ESPNOW_OFF)
743
747
return ;
744
748
745
749
// Turn off promiscuous WiFi mode
@@ -827,7 +831,7 @@ void espnowBeginPairing()
827
831
// Regularly call during pairing to see if we've received a Pairing message
828
832
bool espnowIsPaired ()
829
833
{
830
- if (espnowState == ESPNOW_MAC_RECEIVED)
834
+ if (espNowState == ESPNOW_MAC_RECEIVED)
831
835
{
832
836
// Remove broadcast peer
833
837
uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
@@ -867,7 +871,7 @@ bool espnowIsPaired()
867
871
esp_err_t espnowSendPairMessage (uint8_t *sendToMac)
868
872
{
869
873
// Assemble message to send
870
- PairMessage pairMessage;
874
+ ESP_NOW_PAIR_MESSAGE pairMessage;
871
875
872
876
// Get unit MAC address
873
877
memcpy (pairMessage.macAddress , wifiMACAddress, 6 );
@@ -924,13 +928,13 @@ esp_err_t espnowRemovePeer(uint8_t *peerMac)
924
928
// Update the state of the ESP Now state machine
925
929
void espnowSetState (ESPNOWState newState)
926
930
{
927
- if (espnowState == newState && settings.debugEspNow == true )
931
+ if (espNowState == newState && settings.debugEspNow == true )
928
932
systemPrint (" *" );
929
- espnowState = newState;
933
+ espNowState = newState;
930
934
931
935
if (settings.debugEspNow == true )
932
936
{
933
- systemPrint (" espnowState : " );
937
+ systemPrint (" espNowState : " );
934
938
switch (newState)
935
939
{
936
940
case ESPNOW_OFF:
@@ -960,7 +964,7 @@ void espnowProcessRTCM(byte incoming)
960
964
// If we are paired,
961
965
// Or if the radio is broadcasting
962
966
// Then add bytes to the outgoing buffer
963
- if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING)
967
+ if (espNowState == ESPNOW_PAIRED || espNowState == ESPNOW_BROADCASTING)
964
968
{
965
969
// Move this byte into ESP NOW to send buffer
966
970
espnowOutgoing[espnowOutgoingSpot++] = incoming;
@@ -972,9 +976,9 @@ void espnowProcessRTCM(byte incoming)
972
976
{
973
977
espnowOutgoingSpot = 0 ; // Wrap
974
978
975
- if (espnowState == ESPNOW_PAIRED)
979
+ if (espNowState == ESPNOW_PAIRED)
976
980
esp_now_send (0 , (uint8_t *)&espnowOutgoing, sizeof (espnowOutgoing)); // Send packet to all peers
977
- else // if (espnowState == ESPNOW_BROADCASTING)
981
+ else // if (espNowState == ESPNOW_BROADCASTING)
978
982
{
979
983
uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
980
984
esp_now_send (broadcastMac, (uint8_t *)&espnowOutgoing,
@@ -1037,7 +1041,7 @@ void espnowStaticPairing()
1037
1041
// Returns the current channel being used by WiFi
1038
1042
uint8_t espnowGetChannel ()
1039
1043
{
1040
- if (espnowState == ESPNOW_OFF)
1044
+ if (espNowState == ESPNOW_OFF)
1041
1045
return 0 ;
1042
1046
1043
1047
bool originalPromiscuousMode = false ;
0 commit comments