@@ -26,19 +26,19 @@ typedef struct PairMessage {
2626#ifdef COMPILE_WIFI
2727void espnowOnDataSent (const uint8_t *mac_addr, esp_now_send_status_t status)
2828{
29- // Serial.print("Last Packet Send Status: ");
30- // if (status == ESP_NOW_SEND_SUCCESS)
31- // Serial.println("Delivery Success");
32- // else
33- // Serial.println("Delivery Fail");
29+ // Serial.print("Last Packet Send Status: ");
30+ // if (status == ESP_NOW_SEND_SUCCESS)
31+ // Serial.println("Delivery Success");
32+ // else
33+ // Serial.println("Delivery Fail");
3434}
3535#endif
3636
3737// Callback when data is received
3838void espnowOnDataRecieved (const uint8_t *mac, const uint8_t *incomingData, int len)
3939{
4040#ifdef COMPILE_WIFI
41- if (wifiState == WIFI_ESPNOW_PAIRING )
41+ if (espnowState == ESPNOW_PAIRING )
4242 {
4343 if (len == sizeof (PairMessage)) // First error check
4444 {
@@ -53,7 +53,7 @@ void espnowOnDataRecieved(const uint8_t *mac, const uint8_t *incomingData, int l
5353 if (tempCRC == pairMessage.crc ) // 2nd error check
5454 {
5555 memcpy (&receivedMAC, pairMessage.macAddress , 6 );
56- wifiSetState (WIFI_ESPNOW_MAC_RECEIVED );
56+ espnowSetState (ESPNOW_MAC_RECEIVED );
5757 }
5858 // else Pair CRC failed
5959 }
@@ -86,17 +86,29 @@ void promiscuous_rx_cb(void *buf, wifi_promiscuous_pkt_type_t type)
8686}
8787#endif
8888
89+ // If WiFi is already enabled, simply add the LR protocol
90+ // If the radio is off entirely, start the radio, turn on only the LR protocol
8991void espnowStart ()
9092{
9193#ifdef COMPILE_WIFI
92- // If we are in WIFI_OFF or WIFI_ON, CONNECTED, NOTCONNECTED, doesn't matter
93- // General WiFi will be turned off/disconnected and replaced by ESP NOW
9494
95- // Set device as a Wi-Fi Station
96- WiFi.mode (WIFI_STA);
97-
98- // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
99- esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_LR);
95+ if (wifiState == WIFI_OFF && espnowState == ESPNOW_OFF)
96+ {
97+ // Radio is off, turn it on
98+ WiFi.mode (WIFI_STA);
99+ }
100+ // If WiFi is on but ESP NOW is off, then enable LR protocol
101+ else if (wifiState > WIFI_OFF && espnowState == ESPNOW_OFF)
102+ {
103+ // Enable WiFi + ESP-Now
104+ // Enable long range, PHY rate of ESP32 will be 512Kbps or 256Kbps
105+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
106+ }
107+ // If ESP-Now is active, WiFi is active, do nothing
108+ else
109+ {
110+ Serial.println (" Wi-Fi on, ESP-Now on" );
111+ }
100112
101113 // Init ESP-NOW
102114 if (esp_now_init () != ESP_OK) {
@@ -108,18 +120,18 @@ void espnowStart()
108120 esp_wifi_set_promiscuous (true );
109121 esp_wifi_set_promiscuous_rx_cb (&promiscuous_rx_cb);
110122
111- // Register send callbacks
123+ // Register callbacks
112124 // esp_now_register_send_cb(espnowOnDataSent);
113125 esp_now_register_recv_cb (espnowOnDataRecieved);
114126
115127 if (settings.espnowPeerCount == 0 )
116128 {
117- wifiSetState (WIFI_ESPNOW_ON );
129+ espnowSetState (ESPNOW_ON );
118130 }
119131 else
120132 {
121133 // If we already have peers, move to paired state
122- wifiSetState (WIFI_ESPNOW_PAIRED );
134+ espnowSetState (ESPNOW_PAIRED );
123135
124136 log_d (" Adding %d espnow peers" , settings.espnowPeerCount );
125137 for (int x = 0 ; x < settings.espnowPeerCount ; x++)
@@ -140,27 +152,60 @@ void espnowStart()
140152#endif
141153}
142154
143- // Begin broadcasting our MAC and wait for remote unit to respond
144- void espnowBeginPairing ()
155+ // If WiFi is already enabled, simply remove the LR protocol
156+ // If WiFi is off, stop the radio entirely
157+ void espnowStop ()
145158{
146- #ifndef COMPILE_WIFI
147- return ;
148- #endif
159+ #ifdef COMPILE_WIFI
160+ if (espnowState == ESPNOW_OFF) return ;
149161
150- if (wifiState != WIFI_ESPNOW_ON
151- || wifiState != WIFI_ESPNOW_PAIRING
152- || wifiState != WIFI_ESPNOW_MAC_RECEIVED
153- || wifiState != WIFI_ESPNOW_PAIRED
154- )
162+ if (wifiState > WIFI_OFF)
155163 {
156- espnowStart ();
164+ // Radio is on, turn it off entirely
165+ WiFi.mode (WIFI_OFF);
166+ Serial.println (" WiFi Radio off entirely" );
167+ }
168+ // If WiFi is on, then disable LR protocol
169+ else if (wifiState > WIFI_OFF)
170+ {
171+ // Return protocol to default settings (no WIFI_PROTOCOL_LR for ESP NOW)
172+ esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
173+ Serial.println (" WiFi protocol on, LR protocol off" );
174+ }
175+
176+ // Turn off promiscuous WiFi mode
177+ esp_wifi_set_promiscuous (false );
178+ esp_wifi_set_promiscuous_rx_cb (NULL );
179+
180+ // Deregister callbacks
181+ // esp_now_unregister_send_cb();
182+ esp_now_unregister_recv_cb ();
183+
184+ // Deinit ESP-NOW
185+ if (esp_now_deinit () != ESP_OK) {
186+ Serial.println (" Error deinitializing ESP-NOW" );
187+ return ;
157188 }
158189
190+ espnowSetState (ESPNOW_OFF);
191+
192+ Serial.println (" ESP NOW Off" );
193+ #else
194+ Serial.println (" Error - WiFi not compiled" );
195+ #endif
196+ }
197+
198+ // Begin broadcasting our MAC and wait for remote unit to respond
199+ void espnowBeginPairing ()
200+ {
201+ #ifdef COMPILE_WIFI
202+ espnowStart ();
203+
159204 // To begin pairing, we must add the broadcast MAC to the peer list
160205 uint8_t broadcastMac[6 ] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
161206 espnowAddPeer (broadcastMac, false ); // Encryption is not supported for multicast addresses
162207
163- wifiSetState (WIFI_ESPNOW_PAIRING );
208+ espnowSetState (ESPNOW_PAIRING );
164209
165210 // Begin sending our MAC every 250ms until a remote device sends us there info
166211 randomSeed (millis ());
@@ -177,7 +222,7 @@ void espnowBeginPairing()
177222 {
178223 delay (1 );
179224
180- if (wifiState == WIFI_ESPNOW_MAC_RECEIVED )
225+ if (espnowState == ESPNOW_MAC_RECEIVED )
181226 {
182227 // Remove broadcast peer
183228 espnowRemovePeer (broadcastMac);
@@ -198,7 +243,7 @@ void espnowBeginPairing()
198243 // Send message directly to the received MAC (not unicast), then exit
199244 espnowSendPairMessage (receivedMAC);
200245
201- wifiSetState (WIFI_ESPNOW_PAIRED );
246+ espnowSetState (ESPNOW_PAIRED );
202247 Serial.println (" Pairing compete" );
203248 return ;
204249 }
@@ -210,6 +255,9 @@ void espnowBeginPairing()
210255 }
211256
212257 Serial.println (" User pressed button. Pairing canceled." );
258+ #else
259+ Serial.println (" WiFi not compiled" );
260+ #endif
213261}
214262
215263// Create special pair packet to a given MAC
@@ -275,3 +323,32 @@ esp_err_t espnowRemovePeer(uint8_t *peerMac)
275323 return (ESP_OK);
276324#endif
277325}
326+
327+ // Update the state of the ESP Now state machine
328+ void espnowSetState (byte newState)
329+ {
330+ if (espnowState == newState)
331+ Serial.print (" *" );
332+ espnowState = newState;
333+ switch (newState)
334+ {
335+ default :
336+ Serial.printf (" Unknown ESPNOW state: %d\r\n " , newState);
337+ break ;
338+ case ESPNOW_OFF:
339+ Serial.println (" ESPNOW_OFF" );
340+ break ;
341+ case ESPNOW_ON:
342+ Serial.println (" ESPNOW_ON" );
343+ break ;
344+ case ESPNOW_PAIRING:
345+ Serial.println (" ESPNOW_PAIRING" );
346+ break ;
347+ case ESPNOW_MAC_RECEIVED:
348+ Serial.println (" ESPNOW_MAC_RECEIVED" );
349+ break ;
350+ case ESPNOW_PAIRED:
351+ Serial.println (" ESPNOW_PAIRED" );
352+ break ;
353+ }
354+ }
0 commit comments