1
1
/*
2
- Transmit dummy data over ESP-NOW
2
+ ESP-NOW tranmit to a specific peer
3
+ By: Nathan Seidle
4
+ SparkFun Electronics
5
+ Date: September 25th, 2025
6
+ License: Public domain / don't care.
3
7
4
- In this example, we don't have a paired MAC, this example simply broadcasts .
8
+ In this example we transmit 'hello world' to a specified peer .
5
9
6
- To send a broadcast, the 0xFF broadcastMac has to be added to the peer list, *and*
7
- we have to address the message to the broadcastMac, *not* 0 to send to all peers on the list.
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.
8
14
9
- A receiver does not need to have the broadcastMac added to its peer list. It will receive a broadcast
10
- no matter what.
11
15
*/
12
16
13
17
#include < esp_now.h>
14
- #include < WiFi.h>
18
+ #include < esp_mac.h> // Needed for esp_read_mac()
19
+ #include < WiFi.h> // Needed because ESP-NOW requires WiFi.mode()
15
20
16
21
uint8_t broadcastMac[] = {0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
17
- uint8_t roverMac [] = {0x64 , 0xB7 , 0x08 , 0x3D , 0xFD , 0xAC };
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
18
23
uint8_t mockMac[] = {0xAA , 0xBB , 0xCC , 0xDD , 0xEE , 0xFF }; // Dummy MAC for testing
19
24
20
- esp_now_peer_info_t peerInfo;
21
-
22
25
unsigned long lastSend = 0 ;
23
26
27
+ int channelNumber = 0 ;
28
+ uint8_t packetCounter = 0 ; // Intentionally 8-bit so it rolls over
29
+
24
30
void onDataSent (const uint8_t *mac_addr, esp_now_send_status_t status)
25
31
{
26
32
Serial.print (" Last Packet Send Status: " );
@@ -33,9 +39,15 @@ void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
33
39
void setup ()
34
40
{
35
41
Serial.begin (115200 );
36
- delay (500 );
37
- Serial.println (" Point to Point - Base Transmitter" );
42
+ delay (250 );
43
+ Serial.println (" Remote to Central - This is Remote Transmitter" );
44
+
45
+ uint8_t unitMACAddress[6 ];
46
+ esp_read_mac (unitMACAddress, ESP_MAC_WIFI_STA);
47
+ Serial.printf (" Hi! My MAC address is: {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}\r\n " ,
48
+ unitMACAddress[0 ], unitMACAddress[1 ], unitMACAddress[2 ], unitMACAddress[3 ], unitMACAddress[4 ], unitMACAddress[5 ]);
38
49
50
+ // ESP-NOW must have WiFi initialized
39
51
WiFi.mode (WIFI_STA);
40
52
41
53
if (esp_now_init () != ESP_OK) {
@@ -45,27 +57,28 @@ void setup()
45
57
46
58
esp_now_register_send_cb (onDataSent);
47
59
48
- // espnowAddPeer(roverMac ); // Register a remote address if we want to deliver data there
60
+ // espnowAddPeer(remoteMac ); // Register a remote address to deliver data to
49
61
espnowAddPeer (mockMac);
50
- espnowAddPeer (broadcastMac);
62
+ espnowAddPeer (broadcastMac); // Remote this line to remove broadcast transmissions
51
63
}
52
64
53
65
void loop ()
54
66
{
55
- if (millis () - lastSend > 500 )
67
+ if (millis () - lastSend > 1000 )
56
68
{
57
69
lastSend = millis ();
58
70
59
- uint8_t espnowData[] = " This is the test string." ;
71
+ char espnowData[100 ];
72
+ sprintf (espnowData, " This is test #: %d" , packetCounter++);
60
73
61
74
esp_err_t result = ESP_OK;
62
75
63
- result = esp_now_send (0 , (uint8_t *)&espnowData, sizeof (espnowData)); // Send packet to all peers on the list, excluding broadcast peer.
64
- // result = esp_now_send(broadcastMac, (uint8_t *)&espnowData, sizeof (espnowData)); // Send packet over broadcast
65
- // result = esp_now_send(roverMac , (uint8_t *)&espnowData, sizeof (espnowData)); // Send packet to a specific peer
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
66
79
67
80
if (result == ESP_OK)
68
- Serial.println (" Sent with success" );
81
+ Serial.println (" Sent with success" ); // We will always get a success with broadcastMac packets, presumably because they do not have delivery confirmation.
69
82
else
70
83
Serial.println (" Error sending the data" );
71
84
}
@@ -81,28 +94,19 @@ void loop()
81
94
}
82
95
}
83
96
84
- // Add a given MAC address to the peer list
85
97
esp_err_t espnowAddPeer (uint8_t *peerMac)
86
- {
87
- return (espnowAddPeer (peerMac, true )); // Encrypt by default
88
- }
89
-
90
- esp_err_t espnowAddPeer (uint8_t *peerMac, bool encrypt)
91
98
{
92
99
esp_now_peer_info_t peerInfo;
93
100
94
101
memcpy (peerInfo.peer_addr , peerMac, 6 );
95
102
peerInfo.channel = 0 ;
96
103
peerInfo.ifidx = WIFI_IF_STA;
97
- // memcpy(peerInfo.lmk, "RTKProductsLMK56", 16);
98
- // peerInfo.encrypt = encrypt;
99
104
peerInfo.encrypt = false ;
100
105
101
106
esp_err_t result = esp_now_add_peer (&peerInfo);
102
107
if (result != ESP_OK)
103
108
{
104
- Serial.printf (" Failed to add peer: 0x%02X%02X%02X%02X%02X%02X\r\n " , peerMac[0 ], peerMac[1 ], peerMac[2 ],
105
- peerMac[3 ], peerMac[4 ], peerMac[5 ]);
109
+ 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 ]);
106
110
}
107
111
return (result);
108
- }
112
+ }
0 commit comments