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
1817typedef struct PairMessage {
@@ -23,6 +22,7 @@ typedef struct PairMessage {
2322} PairMessage;
2423
2524// Callback when data is sent
25+ #ifdef COMPILE_ESPNOW
2626void 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
3637void 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
7377void 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
8590void 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
150157void 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
188194void 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
249239esp_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
274268esp_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
292290esp_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
329334void 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
0 commit comments