Skip to content

Commit 8751b98

Browse files
committed
Apply suggested changes
1 parent 271f94b commit 8751b98

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino

+22-48
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Lucas Saavedra Vaz - 2024
44
Send data between two ESP32s using the ESP-NOW protocol in one-to-one (unicast) configuration.
55
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.
67
To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR".
78
*/
89

@@ -13,41 +14,36 @@
1314
// 0: AP mode, 1: Station mode
1415
#define ESPNOW_WIFI_MODE_STATION 0
1516

16-
// Channel to be used by the ESP-NOW protocol from 1 to 13
17+
// Channel to be used by the ESP-NOW protocol
1718
#define ESPNOW_WIFI_CHANNEL 1
1819

1920
#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});
2827
#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});
3734
#endif
3835

39-
ESP_NOW_Serial_Class *esp_now_serial;
36+
ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);
4037

4138
void setup() {
42-
MacAddress current_mac;
43-
4439
Serial.begin(115200);
4540

4641
Serial.print("WiFi Mode: ");
4742

4843
#if ESPNOW_WIFI_MODE_STATION
4944
Serial.println("STA");
5045
WiFi.mode(ESPNOW_WIFI_MODE);
46+
// ToDo: Set the channel using WiFi.setChannel() when using Station mode
5147
esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
5248
#else
5349
Serial.println("AP");
@@ -57,46 +53,24 @@ void setup() {
5753
Serial.print("Channel: ");
5854
Serial.println(ESPNOW_WIFI_CHANNEL);
5955

60-
String mac = GET_IF_MAC();
6156
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());
8458

8559
// Start the ESP-NOW communication
8660
Serial.println("ESP-NOW communication starting...");
87-
esp_now_serial->begin(115200);
61+
NowSerial.begin(115200);
8862
Serial.println("You can now send data between the devices using the Serial Monitor\n");
8963
}
9064

9165
void loop() {
92-
while (esp_now_serial->available() > 0)
66+
while (NowSerial.available())
9367
{
94-
Serial.write(esp_now_serial->read());
68+
Serial.write(NowSerial.read());
9569
}
9670

97-
while (Serial.available() && esp_now_serial->availableForWrite())
71+
while (Serial.available() && NowSerial.availableForWrite())
9872
{
99-
if (esp_now_serial->write(Serial.read()) <= 0)
73+
if (NowSerial.write(Serial.read()))
10074
{
10175
Serial.println("Failed to send data");
10276
break;

libraries/ESP_NOW/src/ESP32_NOW.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class ESP_NOW_Peer {
2020
size_t send(const uint8_t * data, int len);
2121

2222
public:
23-
const MacAddress BROADCAST_ADDR(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
24-
2523
ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel=0, wifi_interface_t iface=WIFI_IF_AP, const uint8_t *lmk=NULL);
2624
virtual ~ESP_NOW_Peer() {}
2725

@@ -46,6 +44,8 @@ class ESP_NOW_Peer {
4644

4745
class ESP_NOW_Class : public Print {
4846
public:
47+
const MacAddress BROADCAST_ADDR = MacAddress(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
48+
4949
ESP_NOW_Class();
5050
~ESP_NOW_Class();
5151

0 commit comments

Comments
 (0)