Skip to content

Commit a5b13af

Browse files
committed
Update ESP-NOW test sketches
1 parent e662aa7 commit a5b13af

File tree

2 files changed

+106
-62
lines changed

2 files changed

+106
-62
lines changed

Firmware/Test Sketches/ESPNOW_Receive/ESPNOW_Receive.ino

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
A receiver does not need to have the broadcastMac added to its peer list. It will receive a broadcast
77
no matter what.
88
9-
If desired, onDataRecieve should check the received MAC against the list of friendly/paired MACs
10-
in order to throw out broadcasted packets that may not be valid data.
9+
Interestingly, the receiver does not have peers added. It will 'receive' packets that either have
10+
its MAC address or the broadcast MAC address in the packet.
1111
12-
Add peers
13-
Add callback to deal with allowed incoming data
14-
Update test sketches
12+
The transmitter needs to have either this remote's MAC address added as a peer, or the broadcast MAC added as a peer.
1513
*/
1614

1715
#include <esp_now.h>
@@ -42,7 +40,7 @@ void setup()
4240
{
4341
Serial.begin(115200);
4442
delay(250);
45-
Serial.println("Remote to Central - This is Central");
43+
Serial.println("ESP-NOW Example - This device is the receiver");
4644

4745
uint8_t unitMACAddress[6];
4846
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
@@ -57,7 +55,7 @@ void setup()
5755
else
5856
Serial.println("ESP-NOW started");
5957

60-
esp_now_register_recv_cb(onDataReceive);
58+
esp_now_register_recv_cb((esp_now_recv_cb_t)onDataReceive);
6159
}
6260

6361
void loop()
@@ -72,22 +70,3 @@ void loop()
7270
}
7371
}
7472
}
75-
76-
// Add a given MAC address to the peer list
77-
esp_err_t espnowAddPeer(uint8_t *peerMac)
78-
{
79-
esp_now_peer_info_t peerInfo;
80-
81-
memcpy(peerInfo.peer_addr, peerMac, 6);
82-
peerInfo.channel = 0;
83-
peerInfo.ifidx = WIFI_IF_STA;
84-
peerInfo.encrypt = false;
85-
86-
esp_err_t result = esp_now_add_peer(&peerInfo);
87-
if (result != ESP_OK)
88-
Serial.printf("Failed to add peer: 0x%02X%02X%02X%02X%02X%02X\r\n", peerMac[0], peerMac[1], peerMac[2], peerMac[3], peerMac[4], peerMac[5]);
89-
else
90-
Serial.printf("Added peer: 0x%02X%02X%02X%02X%02X%02X\r\n", peerMac[0], peerMac[1], peerMac[2], peerMac[3], peerMac[4], peerMac[5]);
91-
92-
return (result);
93-
}
Lines changed: 101 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
/*
2-
ESP-NOW tranmit to a specific peer
2+
ESP-NOW transmit to a specific peer
33
By: Nathan Seidle
44
SparkFun Electronics
55
Date: September 25th, 2025
66
License: Public domain / don't care.
77
8-
In this example we transmit 'hello world' to a specified peer.
8+
In this example we transmit 'This is test #1' to a specified peer or broadcast.
99
10-
Run ESPNOW_Transmit and ESPNOW_Recieve on two ESP32s. This example uses Broadcast ESP-NOW
11-
so no MAC addresses should be needed. ESP-NOW communication should flow correctly between the two units.
12-
13-
If you assign a remote MAC, once data is flowing, hold the remote in reset to see the delivery failures.
10+
Run ESPNOW_Transmit and ESPNOW_Recieve on two ESP32s.
11+
12+
The remote will print its MAC address. Reload this transmit example with that remote's MAC address.
13+
14+
The transmit unit must have the remote MAC or the broadcast MAC added to the peer list.
15+
16+
If the broadcast MAC is added to the peer list, then any device will receive data sent to the broadcast MAC.
1417
18+
Interestingly, the remote devices don't have to have peers added. A remote will 'receive' packets that either
19+
have its MAC address or the broadcast MAC address in the packet.
1520
*/
1621

1722
#include <esp_now.h>
1823
#include <esp_mac.h> //Needed for esp_read_mac()
1924
#include <WiFi.h> //Needed because ESP-NOW requires WiFi.mode()
2025

2126
uint8_t broadcastMac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
22-
uint8_t remoteMac[] = {0xB8, 0xD6, 0x1A, 0x0C, 0xA3, 0xDC}; //Modify this with the MAC address of the remote unit you want to transmit to
23-
uint8_t mockMac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; //Dummy MAC for testing
24-
25-
unsigned long lastSend = 0;
27+
uint8_t remoteMac[] = {0x64, 0xB7, 0x08, 0x86, 0xA4, 0xD0}; //Modify this with the MAC address of the remote unit you want to transmit to
2628

27-
int channelNumber = 0;
2829
uint8_t packetCounter = 0; // Intentionally 8-bit so it rolls over
2930

3031
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
@@ -40,7 +41,7 @@ void setup()
4041
{
4142
Serial.begin(115200);
4243
delay(250);
43-
Serial.println("Remote to Central - This is Remote Transmitter");
44+
Serial.println("ESP-NOW Example - This device is the transmitter");
4445

4546
uint8_t unitMACAddress[6];
4647
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
@@ -55,34 +56,19 @@ void setup()
5556
return;
5657
}
5758

58-
esp_now_register_send_cb(onDataSent);
59+
esp_now_register_send_cb((esp_now_send_cb_t)onDataSent);
5960

60-
//espnowAddPeer(remoteMac); // Register a remote address to deliver data to
61-
espnowAddPeer(mockMac);
62-
espnowAddPeer(broadcastMac); //Remote this line to remove broadcast transmissions
61+
Serial.println();
62+
Serial.println("a) Add remote MAC to the peer list");
63+
Serial.println("b) Add broadcast MAC to the peer list");
64+
Serial.println("c) Clear the peer list");
65+
Serial.println("1) Send data to remote MAC");
66+
Serial.println("2) Send data to broadcast MAC");
67+
Serial.println("r) Reset");
6368
}
6469

6570
void loop()
6671
{
67-
if (millis() - lastSend > 1000)
68-
{
69-
lastSend = millis();
70-
71-
char espnowData[100];
72-
sprintf(espnowData, "This is test #: %d", packetCounter++);
73-
74-
esp_err_t result = ESP_OK;
75-
76-
//result = esp_now_send(0, (uint8_t *)&espnowData, strlen(espnowData)); // Send packet to all peers on the list, excluding broadcast peer.
77-
//result = esp_now_send(broadcastMac, (uint8_t *)&espnowData, strlen(espnowData)); // Send packet over broadcast
78-
result = esp_now_send(remoteMac, (uint8_t *)&espnowData, strlen(espnowData)); // Send packet to a specific peer
79-
80-
if (result == ESP_OK)
81-
Serial.println("Sent with success"); // We will always get a success with broadcastMac packets, presumably because they do not have delivery confirmation.
82-
else
83-
Serial.println("Error sending the data");
84-
}
85-
8672
if (Serial.available())
8773
{
8874
byte incoming = Serial.read();
@@ -91,6 +77,31 @@ void loop()
9177
Serial.println("Reset");
9278
ESP.restart();
9379
}
80+
else if (incoming == 'a')
81+
{
82+
Serial.println("Adding peer to list");
83+
espnowAddPeer(remoteMac); // Register a remote address to deliver data to
84+
}
85+
else if (incoming == 'b')
86+
{
87+
Serial.println("Adding broadcast to list");
88+
espnowAddPeer(broadcastMac); // Register a remote address to deliver data to
89+
}
90+
else if (incoming == 'c')
91+
{
92+
Serial.println("Clearing broadcast list");
93+
espnowClearPeerList();
94+
}
95+
else if (incoming == '1')
96+
{
97+
Serial.println("Send to peer list");
98+
espnowSendMessage(0); // Send packet to all peers on the list, excluding broadcast peer.
99+
}
100+
else if (incoming == '2')
101+
{
102+
Serial.println("Send via broadcast");
103+
espnowSendMessage(broadcastMac);// Send packet over broadcast
104+
}
94105
}
95106
}
96107

@@ -109,4 +120,58 @@ esp_err_t espnowAddPeer(uint8_t *peerMac)
109120
Serial.printf("Failed to add peer: 0x%02X%02X%02X%02X%02X%02X\r\n", peerMac[0], peerMac[1], peerMac[2], peerMac[3], peerMac[4], peerMac[5]);
110121
}
111122
return (result);
112-
}
123+
}
124+
125+
// Send data packet to a given MAC
126+
void espnowSendMessage(const uint8_t *sendToMac)
127+
{
128+
char espnowData[100];
129+
sprintf(espnowData, "This is test #: %d", packetCounter++);
130+
131+
esp_err_t result = esp_now_send(sendToMac, (uint8_t *)&espnowData, sizeof(espnowData)); // Send packet to a specific peer
132+
133+
if (result == ESP_OK)
134+
Serial.println("Sent with success"); // We will always get a success with broadcastMac packets, presumably because they do not have delivery confirmation.
135+
else
136+
Serial.println("Error sending the data");
137+
}
138+
139+
void espnowClearPeerList()
140+
{
141+
// if (esp_now_is_peer_exist(broadcastMac))
142+
// {
143+
// status = espNowRemovePeer(broadcastMac);
144+
// if (status != ESP_OK)
145+
// Serial.printf("ERROR: Failed to delete broadcast peer, status: %d\r\n", status);
146+
// else
147+
// Serial.printf("ESP-NOW removed broadcast peer\r\n");
148+
// }
149+
150+
// Remove all peers
151+
while (1)
152+
{
153+
esp_now_peer_info_t peerInfo;
154+
155+
// Get the next peer
156+
esp_err_t status = esp_now_fetch_peer(true, &peerInfo);
157+
if (status != ESP_OK)
158+
{
159+
Serial.println("All done!");
160+
break;
161+
}
162+
163+
// Remove the unicast peer
164+
status = esp_now_del_peer(peerInfo.peer_addr);
165+
if (status != ESP_OK)
166+
{
167+
Serial.printf("ERROR: Failed to delete peer %02X:%02X:%02X:%02X:%02X:%02X, status: %d\r\n",
168+
peerInfo.peer_addr[0], peerInfo.peer_addr[1], peerInfo.peer_addr[2], peerInfo.peer_addr[3],
169+
peerInfo.peer_addr[4], peerInfo.peer_addr[5], status);
170+
break;
171+
}
172+
173+
Serial.printf("ESP-NOW removed peer %02X:%02X:%02X:%02X:%02X:%02X\r\n", peerInfo.peer_addr[0],
174+
peerInfo.peer_addr[1], peerInfo.peer_addr[2], peerInfo.peer_addr[3], peerInfo.peer_addr[4],
175+
peerInfo.peer_addr[5]);
176+
}
177+
}

0 commit comments

Comments
 (0)