@@ -43,14 +43,14 @@ typedef struct _ESP_NOW_PAIR_MESSAGE
4343uint16_t espNowBytesSent; // May be more than 255
4444unsigned long espNowLastAdd; // Tracks how long since the last byte was added to the outgoing buffer
4545unsigned long espNowLastRssiUpdate;
46- uint8_t espNowOutgoing[250 ]; // ESP NOW has max of 250 characters
47- uint8_t espNowOutgoingSpot; // ESP Now has a max of 250 characters
46+ uint8_t espNowOutgoing[250 ]; // ESP NOW has max of 250 characters
47+ uint8_t espNowOutgoingSpot; // ESP Now has a max of 250 characters
4848uint8_t espNowReceivedMAC[6 ]; // Holds the MAC received during pairing
4949ESPNOWState espNowState;
5050
5151// *********************************************************************
5252// Add a peer to the ESP-NOW network
53- esp_err_t espNowAddPeer (const uint8_t * peerMac, bool encrypt)
53+ esp_err_t espNowAddPeer (const uint8_t *peerMac, bool encrypt)
5454{
5555 esp_now_peer_info_t peerInfo;
5656
@@ -66,14 +66,12 @@ esp_err_t espNowAddPeer(const uint8_t * peerMac, bool encrypt)
6666 esp_err_t result = esp_now_add_peer (&peerInfo);
6767 if (result != ESP_OK)
6868 {
69- systemPrintf (" ERROR: Failed to add ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x, result: %d (%s)\r\n " ,
70- peerMac[0 ], peerMac[1 ], peerMac[2 ], peerMac[3 ],
71- peerMac[4 ], peerMac[5 ], result, esp_err_to_name (result));
69+ systemPrintf (" ERROR: Failed to add ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X, result: %d (%s)\r\n " , peerMac[0 ],
70+ peerMac[1 ], peerMac[2 ], peerMac[3 ], peerMac[4 ], peerMac[5 ], result, esp_err_to_name (result));
7271 }
7372 else if (settings.debugEspNow )
74- systemPrintf (" Added ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
75- peerMac[0 ], peerMac[1 ], peerMac[2 ], peerMac[3 ],
76- peerMac[4 ], peerMac[5 ]);
73+ systemPrintf (" Added ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X\r\n " , peerMac[0 ], peerMac[1 ], peerMac[2 ],
74+ peerMac[3 ], peerMac[4 ], peerMac[5 ]);
7775 return result;
7876}
7977
@@ -88,12 +86,19 @@ esp_err_t espNowAddPeer(const uint8_t *peerMac)
8886// Start ESP-Now if needed, put ESP-Now into broadcast state
8987void espNowBeginPairing ()
9088{
89+ if (settings.enableEspNow == false )
90+ {
91+ settings.enableEspNow = true ;
92+ recordSystemSettings ();
93+
94+ if (settings.debugEspNow )
95+ systemPrintln (" espNowBeginPairing enabling ESP-NOW" );
96+ }
97+
9198 // Start ESP-NOW if necessary
99+ // If no peers are on file, automatically add the broadcast MAC to the peer list
92100 wifiEspNowOn (__FILE__, __LINE__);
93101
94- // To begin pairing, we must add the broadcast MAC to the peer list
95- espNowAddPeer (espNowBroadcastAddr, false ); // Encryption is not supported for multicast addresses
96-
97102 espNowSetState (ESPNOW_PAIRING);
98103}
99104
@@ -106,25 +111,22 @@ bool espNowIsPaired()
106111
107112// *********************************************************************
108113// Callback when data is received
109- void espNowOnDataReceived (const esp_now_recv_info *mac,
110- const uint8_t *incomingData,
111- int len)
114+ void espNowOnDataReceived (const esp_now_recv_info *mac, const uint8_t *incomingData, int len)
112115{
113- // typedef struct esp_now_recv_info {
114- // uint8_t * src_addr; // Source address of ESPNOW packet
115- // uint8_t * des_addr; // Destination address of ESPNOW packet
116- // wifi_pkt_rx_ctrl_t * rx_ctrl; // Rx control info of ESPNOW packet
117- // } esp_now_recv_info_t;
116+ // typedef struct esp_now_recv_info {
117+ // uint8_t * src_addr; // Source address of ESPNOW packet
118+ // uint8_t * des_addr; // Destination address of ESPNOW packet
119+ // wifi_pkt_rx_ctrl_t * rx_ctrl; // Rx control info of ESPNOW packet
120+ // } esp_now_recv_info_t;
118121
119122 // Display the packet
120123 if (settings.debugEspNow == true )
121124 {
122- systemPrintf (" *** ESP-NOW: RX %02x:%02x:%02x:%02x:%02x:%02x --> %02x:%02x:%02x:%02x:%02x:%02x, %d bytes, rssi: %d\r\n " ,
123- mac->src_addr [0 ], mac->src_addr [1 ], mac->src_addr [2 ],
124- mac->src_addr [3 ], mac->src_addr [4 ], mac->src_addr [5 ],
125- mac->des_addr [0 ], mac->des_addr [1 ], mac->des_addr [2 ],
126- mac->des_addr [3 ], mac->des_addr [4 ], mac->des_addr [5 ],
127- len, packetRSSI);
125+ systemPrintf (
126+ " *** ESP-NOW: RX %02X:%02X:%02X:%02X:%02X:%02X --> %02X:%02X:%02X:%02X:%02X:%02X, %d bytes, rssi: %d\r\n " ,
127+ mac->src_addr [0 ], mac->src_addr [1 ], mac->src_addr [2 ], mac->src_addr [3 ], mac->src_addr [4 ], mac->src_addr [5 ],
128+ mac->des_addr [0 ], mac->des_addr [1 ], mac->des_addr [2 ], mac->des_addr [3 ], mac->des_addr [4 ], mac->des_addr [5 ],
129+ len, packetRSSI);
128130 }
129131
130132 if (espNowState == ESPNOW_PAIRING)
@@ -294,16 +296,12 @@ esp_err_t espNowRemovePeer(const uint8_t *peerMac)
294296 if (settings.debugEspNow == true )
295297 {
296298 if (result != ESP_OK)
297- systemPrintf (" ERROR: Failed to remove ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x, result: %s\r\n " ,
298- peerMac[0 ], peerMac[1 ],
299- peerMac[2 ], peerMac[3 ],
300- peerMac[4 ], peerMac[5 ],
299+ systemPrintf (" ERROR: Failed to remove ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X, result: %s\r\n " ,
300+ peerMac[0 ], peerMac[1 ], peerMac[2 ], peerMac[3 ], peerMac[4 ], peerMac[5 ],
301301 esp_err_to_name (result));
302302 else
303- systemPrintf (" Removed ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
304- peerMac[0 ], peerMac[1 ],
305- peerMac[2 ], peerMac[3 ],
306- peerMac[4 ], peerMac[5 ]);
303+ systemPrintf (" Removed ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X\r\n " , peerMac[0 ], peerMac[1 ], peerMac[2 ],
304+ peerMac[3 ], peerMac[4 ], peerMac[5 ]);
307305 }
308306 return result;
309307}
@@ -331,18 +329,13 @@ esp_err_t espNowSendPairMessage(const uint8_t *sendToMac)
331329// Update the state of the ESP-NOW subsystem
332330void espNowSetState (ESPNOWState newState)
333331{
334- const char * name[] =
335- {
336- " ESPNOW_OFF" ,
337- " ESPNOW_BROADCASTING" ,
338- " ESPNOW_PAIRING" ,
339- " ESPNOW_MAC_RECEIVED" ,
340- " ESPNOW_PAIRED" ,
332+ const char *name[] = {
333+ " ESPNOW_OFF" , " ESPNOW_BROADCASTING" , " ESPNOW_PAIRING" , " ESPNOW_MAC_RECEIVED" , " ESPNOW_PAIRED" ,
341334 };
342335 const int nameCount = sizeof (name) / sizeof (name[0 ]);
343- const char * newName;
336+ const char *newName;
344337 char nLine[80 ];
345- const char * oldName;
338+ const char *oldName;
346339 char oLine[80 ];
347340
348341 if (settings.debugEspNow == true )
@@ -445,13 +438,10 @@ bool espNowStart()
445438 if (esp_now_is_peer_exist (settings.espnowPeers [index]))
446439 {
447440 if (settings.debugEspNow == true )
448- systemPrintf (" ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x, status: already exists\r\n " ,
449- settings.espnowPeers [index][0 ],
450- settings.espnowPeers [index][1 ],
451- settings.espnowPeers [index][2 ],
452- settings.espnowPeers [index][3 ],
453- settings.espnowPeers [index][4 ],
454- settings.espnowPeers [index][5 ]);
441+ systemPrintf (" ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X, status: already exists\r\n " ,
442+ settings.espnowPeers [index][0 ], settings.espnowPeers [index][1 ],
443+ settings.espnowPeers [index][2 ], settings.espnowPeers [index][3 ],
444+ settings.espnowPeers [index][4 ], settings.espnowPeers [index][5 ]);
455445 }
456446 else
457447 {
@@ -460,25 +450,20 @@ bool espNowStart()
460450 if (status == ESP_OK)
461451 {
462452 if (settings.debugEspNow == true )
463- systemPrintf (" Added ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
464- settings.espnowPeers [index][0 ],
465- settings.espnowPeers [index][1 ],
466- settings.espnowPeers [index][2 ],
467- settings.espnowPeers [index][3 ],
468- settings.espnowPeers [index][4 ],
469- settings.espnowPeers [index][5 ]);
453+ systemPrintf (" Added ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X\r\n " ,
454+ settings.espnowPeers [index][0 ], settings.espnowPeers [index][1 ],
455+ settings.espnowPeers [index][2 ], settings.espnowPeers [index][3 ],
456+ settings.espnowPeers [index][4 ], settings.espnowPeers [index][5 ]);
470457 }
471458 else
472459 {
473460 if (settings.debugEspNow == true )
474- systemPrintf (" ERROR: Failed to add ESP-NOW peer %02x:%02x:%02x:%02x:%02x:%02x, status: %d, %s\r\n " ,
475- settings.espnowPeers [index][0 ],
476- settings.espnowPeers [index][1 ],
477- settings.espnowPeers [index][2 ],
478- settings.espnowPeers [index][3 ],
479- settings.espnowPeers [index][4 ],
480- settings.espnowPeers [index][5 ],
481- status, esp_err_to_name (status));
461+ systemPrintf (
462+ " ERROR: Failed to add ESP-NOW peer %02X:%02X:%02X:%02X:%02X:%02X, status: %d, %s\r\n " ,
463+ settings.espnowPeers [index][0 ], settings.espnowPeers [index][1 ],
464+ settings.espnowPeers [index][2 ], settings.espnowPeers [index][3 ],
465+ settings.espnowPeers [index][4 ], settings.espnowPeers [index][5 ], status,
466+ esp_err_to_name (status));
482467 break ;
483468 }
484469 }
@@ -609,18 +594,15 @@ bool espNowStop()
609594 status = espNowRemovePeer (peerInfo.peer_addr );
610595 if (status != ESP_OK)
611596 {
612- systemPrintf (" ERROR: Failed to delete peer %02x:%02x:%02x:%02x:%02x:%02x, status: %d\r\n " ,
613- peerInfo.peer_addr [0 ], peerInfo.peer_addr [1 ],
614- peerInfo.peer_addr [2 ], peerInfo.peer_addr [3 ],
615- peerInfo.peer_addr [4 ], peerInfo.peer_addr [5 ],
616- status);
597+ systemPrintf (" ERROR: Failed to delete peer %02X:%02X:%02X:%02X:%02X:%02X, status: %d\r\n " ,
598+ peerInfo.peer_addr [0 ], peerInfo.peer_addr [1 ], peerInfo.peer_addr [2 ], peerInfo.peer_addr [3 ],
599+ peerInfo.peer_addr [4 ], peerInfo.peer_addr [5 ], status);
617600 break ;
618601 }
619602 if (settings.debugEspNow )
620- systemPrintf (" ESP-NOW removed peer %02x:%02x:%02x:%02x:%02x:%02x\r\n " ,
621- peerInfo.peer_addr [0 ], peerInfo.peer_addr [1 ],
622- peerInfo.peer_addr [2 ], peerInfo.peer_addr [3 ],
623- peerInfo.peer_addr [4 ], peerInfo.peer_addr [5 ]);
603+ systemPrintf (" ESP-NOW removed peer %02X:%02X:%02X:%02X:%02X:%02X\r\n " , peerInfo.peer_addr [0 ],
604+ peerInfo.peer_addr [1 ], peerInfo.peer_addr [2 ], peerInfo.peer_addr [3 ], peerInfo.peer_addr [4 ],
605+ peerInfo.peer_addr [5 ]);
624606 }
625607 if (status != ESP_ERR_ESPNOW_NOT_FOUND)
626608 {
@@ -703,4 +685,4 @@ void espNowUpdate()
703685 }
704686}
705687
706- #endif // COMPILE_ESPNOW
688+ #endif // COMPILE_ESPNOW
0 commit comments