3
3
Lucas Saavedra Vaz - 2024
4
4
Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration.
5
5
Note that different MAC addresses are used for different interfaces.
6
+ The devices can be in different modes (AP or Station) and still communicate using ESP-NOW.
7
+ The only requirement is that the devices are on the same Wi-Fi channel.
6
8
Set the peer MAC address according to the device that will receive the data.
9
+
7
10
Example setup:
8
- - Device 1: AP mode, peer MAC address set to the Station MAC address of Device 2
9
- - Device 2: Station mode, peer MAC address set to the AP MAC address of Device 1
11
+ - Device 1: AP mode with MAC address F6:12:FA:42:B6:E8
12
+ Peer MAC address set to the Station MAC address of Device 2 (F4:12:FA:40:64:4C)
13
+ - Device 2: Station mode with MAC address F4:12:FA:40:64:4C
14
+ Peer MAC address set to the AP MAC address of Device 1 (F6:12:FA:42:B6:E8)
10
15
11
16
The device running this sketch will also receive and print data from any device that has its MAC address set as the peer MAC address.
12
17
To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR".
16
21
#include " MacAddress.h"
17
22
#include " WiFi.h"
18
23
24
+ #include " esp_wifi.h"
25
+
19
26
// 0: AP mode, 1: Station mode
20
- #define ESPNOW_WIFI_MODE_STATION 0
27
+ #define ESPNOW_WIFI_MODE_STATION 1
21
28
22
29
// Channel to be used by the ESP-NOW protocol
23
30
#define ESPNOW_WIFI_CHANNEL 1
26
33
#define ESPNOW_WIFI_IF WIFI_IF_STA
27
34
#define GET_IF_MAC WiFi.macAddress
28
35
29
- // Set the Station MAC address of the device that will receive the data
30
- // For example: F4 :12:FA:40:64:4C
31
- const MacAddress peer_mac ({0xF4 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
36
+ // Set the MAC address of the device that will receive the data
37
+ // For example: F6 :12:FA:42:B6:E8
38
+ const MacAddress peer_mac ({0xF6 , 0x12 , 0xFA , 0x42 , 0xB6 , 0xE8 });
32
39
#else // ESP-NOW using WiFi AP mode
33
40
#define ESPNOW_WIFI_IF WIFI_IF_AP
34
41
#define GET_IF_MAC WiFi.softAPmacAddress
35
42
36
- // Set the AP MAC address of the device that will receive the data
37
- // For example: F6 :12:FA:40:64:4C
38
- const MacAddress peer_mac ({0xF6 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
43
+ // Set the MAC address of the device that will receive the data
44
+ // For example: F4 :12:FA:40:64:4C
45
+ const MacAddress peer_mac ({0xF4 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
39
46
#endif
40
47
41
48
ESP_NOW_Serial_Class NowSerial (peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);
@@ -47,7 +54,7 @@ void setup() {
47
54
48
55
#if ESPNOW_WIFI_MODE_STATION
49
56
Serial.println (" STA" );
50
- WiFi.mode (ESPNOW_WIFI_MODE );
57
+ WiFi.mode (WIFI_STA );
51
58
// ToDo: Set the channel using WiFi.setChannel() when using Station mode
52
59
esp_wifi_set_channel (ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
53
60
#else
@@ -75,7 +82,7 @@ void loop() {
75
82
76
83
while (Serial.available () && NowSerial.availableForWrite ())
77
84
{
78
- if (NowSerial.write (Serial.read ()))
85
+ if (NowSerial.write (Serial.read ()) <= 0 )
79
86
{
80
87
Serial.println (" Failed to send data" );
81
88
break ;
0 commit comments