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
+ Set the peer MAC address according to the device that will receive the data.
6
7
To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR".
7
8
*/
8
9
13
14
// 0: AP mode, 1: Station mode
14
15
#define ESPNOW_WIFI_MODE_STATION 0
15
16
16
- // Channel to be used by the ESP-NOW protocol from 1 to 13
17
+ // Channel to be used by the ESP-NOW protocol
17
18
#define ESPNOW_WIFI_CHANNEL 1
18
19
19
20
#if ESPNOW_WIFI_MODE_STATION // ESP-NOW using WiFi Station mode
20
- #define ESPNOW_WIFI_IF WIFI_IF_STA
21
- #define GET_IF_MAC WiFi.macAddress
22
-
23
- // Station mode MAC addresses of the devices in the network
24
- // Device 1 - F4:12:FA:42:B6:E8
25
- const MacAddress mac1 ({0xF4 , 0x12 , 0xFA , 0x42 , 0xB6 , 0xE8 });
26
- // Device 2 - F4:12:FA:40:64:4C
27
- const MacAddress mac2 ({0xF4 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
21
+ #define ESPNOW_WIFI_IF WIFI_IF_STA
22
+ #define GET_IF_MAC WiFi.macAddress
23
+
24
+ // Set the Station MAC address of the device that will receive the data
25
+ // For example: F4:12:FA:40:64:4C
26
+ const MacAddress peer_mac ({0xF4 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
28
27
#else // ESP-NOW using WiFi AP mode
29
- #define ESPNOW_WIFI_IF WIFI_IF_AP
30
- #define GET_IF_MAC WiFi.softAPmacAddress
31
-
32
- // AP mode MAC addresses of the devices in the network
33
- // Device 1 - F6:12:FA:42:B6:E8
34
- const MacAddress mac1 ({0xF6 , 0x12 , 0xFA , 0x42 , 0xB6 , 0xE8 });
35
- // Device 2 - F6:12:FA:40:64:4C
36
- const MacAddress mac2 ({0xF6 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
28
+ #define ESPNOW_WIFI_IF WIFI_IF_AP
29
+ #define GET_IF_MAC WiFi.softAPmacAddress
30
+
31
+ // Set the AP MAC address of the device that will receive the data
32
+ // For example: F6:12:FA:40:64:4C
33
+ const MacAddress peer_mac ({0xF6 , 0x12 , 0xFA , 0x40 , 0x64 , 0x4C });
37
34
#endif
38
35
39
- ESP_NOW_Serial_Class *esp_now_serial ;
36
+ ESP_NOW_Serial_Class NowSerial (peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF) ;
40
37
41
38
void setup () {
42
- MacAddress current_mac;
43
-
44
39
Serial.begin (115200 );
45
40
46
41
Serial.print (" WiFi Mode: " );
47
42
48
43
#if ESPNOW_WIFI_MODE_STATION
49
44
Serial.println (" STA" );
50
45
WiFi.mode (ESPNOW_WIFI_MODE);
46
+ // ToDo: Set the channel using WiFi.setChannel() when using Station mode
51
47
esp_wifi_set_channel (ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
52
48
#else
53
49
Serial.println (" AP" );
@@ -57,46 +53,24 @@ void setup() {
57
53
Serial.print (" Channel: " );
58
54
Serial.println (ESPNOW_WIFI_CHANNEL);
59
55
60
- String mac = GET_IF_MAC ();
61
56
Serial.print (" MAC Address: " );
62
- current_mac.fromString (mac);
63
- Serial.println (mac);
64
-
65
- WiFi.disconnect ();
66
-
67
- if (current_mac == mac1)
68
- {
69
- Serial.println (" I'm the Device 1\n " );
70
- // Pass the MAC address of the other device to the ESP_NOW_Serial_Class
71
- esp_now_serial = new ESP_NOW_Serial_Class (mac2, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);
72
- }
73
- else if (current_mac == mac2)
74
- {
75
- Serial.println (" I'm the Device 2\n " );
76
- // Pass the MAC address of the other device to the ESP_NOW_Serial_Class
77
- esp_now_serial = new ESP_NOW_Serial_Class (mac1, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);
78
- }
79
- else
80
- {
81
- Serial.println (" Unknown MAC address. Please update the mac addresses in the sketch\n " );
82
- while (1 );
83
- }
57
+ Serial.println (GET_IF_MAC ());
84
58
85
59
// Start the ESP-NOW communication
86
60
Serial.println (" ESP-NOW communication starting..." );
87
- esp_now_serial-> begin (115200 );
61
+ NowSerial. begin (115200 );
88
62
Serial.println (" You can now send data between the devices using the Serial Monitor\n " );
89
63
}
90
64
91
65
void loop () {
92
- while (esp_now_serial-> available () > 0 )
66
+ while (NowSerial. available ())
93
67
{
94
- Serial.write (esp_now_serial-> read ());
68
+ Serial.write (NowSerial. read ());
95
69
}
96
70
97
- while (Serial.available () && esp_now_serial-> availableForWrite ())
71
+ while (Serial.available () && NowSerial. availableForWrite ())
98
72
{
99
- if (esp_now_serial-> write (Serial.read ()) <= 0 )
73
+ if (NowSerial. write (Serial.read ()))
100
74
{
101
75
Serial.println (" Failed to send data" );
102
76
break ;
0 commit comments