@@ -21,15 +21,15 @@ void updateEspnow()
2121#ifdef COMPILE_ESPNOW
2222 if (settings.enableEspNow == true )
2323 {
24- if (espnowState == ESPNOW_PAIRED)
24+ if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING )
2525 {
2626 // If it's been longer than a few ms since we last added a byte to the buffer
2727 // then we've reached the end of the RTCM stream. Send partial buffer.
2828 if (espnowOutgoingSpot > 0 && (millis () - espnowLastAdd) > 50 )
2929 {
30- if (settings. espnowBroadcast == false )
30+ if (espnowState == ESPNOW_PAIRED )
3131 esp_now_send (0 , (uint8_t *)&espnowOutgoing, espnowOutgoingSpot); // Send partial packet to all peers
32- else
32+ else // if (espnowState == ESPNOW_BROADCASTING)
3333 {
3434 uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
3535 esp_now_send (broadcastMac, (uint8_t *)&espnowOutgoing,
@@ -189,8 +189,8 @@ void espnowStart()
189189 // If WiFi is on but ESP NOW is off, then enable LR protocol
190190 else if (wifiState > WIFI_STATE_OFF && espnowState == ESPNOW_OFF)
191191 {
192- // // Enable WiFi + ESP-Now
193- // // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
192+ // Enable WiFi + ESP-Now
193+ // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
194194 if (protocols != (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR))
195195 {
196196 response = esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N |
@@ -244,11 +244,35 @@ void espnowStart()
244244
245245 if (settings.espnowPeerCount == 0 )
246246 {
247- espnowSetState (ESPNOW_ON);
247+ // Enter broadcast mode
248+
249+ uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
250+
251+ if (esp_now_is_peer_exist (broadcastMac) == true )
252+ {
253+ if (settings.debugEspNow == true )
254+ systemPrintln (" Broadcast peer already exists" );
255+ }
256+ else
257+ {
258+ esp_err_t result = espnowAddPeer (broadcastMac, false ); // Encryption not support for broadcast MAC
259+ if (result != ESP_OK)
260+ {
261+ if (settings.debugEspNow == true )
262+ systemPrintln (" Failed to add broadcast peer" );
263+ }
264+ else
265+ {
266+ if (settings.debugEspNow == true )
267+ systemPrintln (" Broadcast peer added" );
268+ }
269+ }
270+
271+ espnowSetState (ESPNOW_BROADCASTING);
248272 }
249273 else
250274 {
251- // If we already have peers, move to paired state
275+ // If we have peers, move to paired state
252276 espnowSetState (ESPNOW_PAIRED);
253277
254278 if (settings.debugEspNow == true )
@@ -273,32 +297,6 @@ void espnowStart()
273297 }
274298 }
275299
276- if (settings.espnowBroadcast == true )
277- {
278- // Add broadcast peer if override is turned on
279- uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
280-
281- if (esp_now_is_peer_exist (broadcastMac) == true )
282- {
283- if (settings.debugEspNow == true )
284- systemPrintln (" Broadcast peer already exists" );
285- }
286- else
287- {
288- esp_err_t result = espnowAddPeer (broadcastMac, false ); // Encryption not support for broadcast MAC
289- if (result != ESP_OK)
290- {
291- if (settings.debugEspNow == true )
292- systemPrintln (" Failed to add broadcast peer" );
293- }
294- else
295- {
296- if (settings.debugEspNow == true )
297- systemPrintln (" Broadcast peer added" );
298- }
299- }
300- }
301-
302300 systemPrintln (" ESP-Now Started" );
303301#endif // COMPILE_ESPNOW
304302}
@@ -401,13 +399,9 @@ bool espnowIsPaired()
401399#ifdef COMPILE_ESPNOW
402400 if (espnowState == ESPNOW_MAC_RECEIVED)
403401 {
404-
405- if (settings.espnowBroadcast == false )
406- {
407- // Remove broadcast peer
408- uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
409- espnowRemovePeer (broadcastMac);
410- }
402+ // Remove broadcast peer
403+ uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
404+ espnowRemovePeer (broadcastMac);
411405
412406 if (esp_now_is_peer_exist (receivedMAC) == true )
413407 {
@@ -525,8 +519,8 @@ void espnowSetState(ESPNOWState newState)
525519 case ESPNOW_OFF:
526520 systemPrintln (" ESPNOW_OFF" );
527521 break ;
528- case ESPNOW_ON :
529- systemPrintln (" ESPNOW_ON " );
522+ case ESPNOW_BROADCASTING :
523+ systemPrintln (" ESPNOW_BROADCASTING " );
530524 break ;
531525 case ESPNOW_PAIRING:
532526 systemPrintln (" ESPNOW_PAIRING" );
@@ -547,31 +541,35 @@ void espnowSetState(ESPNOWState newState)
547541void espnowProcessRTCM (byte incoming)
548542{
549543#ifdef COMPILE_ESPNOW
550- if (espnowState == ESPNOW_PAIRED)
544+ // If we are paired,
545+ // Or if the radio is broadcasting
546+ // Then add bytes to the outgoing buffer
547+ if (espnowState == ESPNOW_PAIRED || espnowState == ESPNOW_BROADCASTING)
551548 {
552549 // Move this byte into ESP NOW to send buffer
553550 espnowOutgoing[espnowOutgoingSpot++] = incoming;
554551 espnowLastAdd = millis ();
552+ }
555553
556- if (espnowOutgoingSpot == sizeof (espnowOutgoing))
557- {
558- espnowOutgoingSpot = 0 ; // Wrap
554+ // Send buffer when full
555+ if (espnowOutgoingSpot == sizeof (espnowOutgoing))
556+ {
557+ espnowOutgoingSpot = 0 ; // Wrap
559558
560- if (settings. espnowBroadcast == false )
561- esp_now_send (0 , (uint8_t *)&espnowOutgoing, sizeof (espnowOutgoing)); // Send packet to all peers
562- else
563- {
564- uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
565- esp_now_send (broadcastMac, (uint8_t *)&espnowOutgoing,
566- sizeof (espnowOutgoing)); // Send packet via broadcast
567- }
559+ if (espnowState == ESPNOW_PAIRED )
560+ esp_now_send (0 , (uint8_t *)&espnowOutgoing, sizeof (espnowOutgoing)); // Send packet to all peers
561+ else // if (espnowState == ESPNOW_BROADCASTING)
562+ {
563+ uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
564+ esp_now_send (broadcastMac, (uint8_t *)&espnowOutgoing,
565+ sizeof (espnowOutgoing)); // Send packet via broadcast
566+ }
568567
569- delay (10 ); // We need a small delay between sending multiple packets
568+ delay (10 ); // We need a small delay between sending multiple packets
570569
571- espnowBytesSent += sizeof (espnowOutgoing);
570+ espnowBytesSent += sizeof (espnowOutgoing);
572571
573- espnowOutgoingRTCM = true ;
574- }
572+ espnowOutgoingRTCM = true ;
575573 }
576574#endif // COMPILE_ESPNOW
577575}
0 commit comments