Skip to content

Commit 8a6025c

Browse files
committed
Fix example
1 parent 6d04c83 commit 8a6025c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
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+
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.
68
Set the peer MAC address according to the device that will receive the data.
9+
710
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)
1015
1116
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.
1217
To properly visualize the data being sent, set the line ending in the Serial Monitor to "Both NL & CR".
@@ -16,8 +21,10 @@
1621
#include "MacAddress.h"
1722
#include "WiFi.h"
1823

24+
#include "esp_wifi.h"
25+
1926
// 0: AP mode, 1: Station mode
20-
#define ESPNOW_WIFI_MODE_STATION 0
27+
#define ESPNOW_WIFI_MODE_STATION 1
2128

2229
// Channel to be used by the ESP-NOW protocol
2330
#define ESPNOW_WIFI_CHANNEL 1
@@ -26,16 +33,16 @@
2633
#define ESPNOW_WIFI_IF WIFI_IF_STA
2734
#define GET_IF_MAC WiFi.macAddress
2835

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});
3239
#else // ESP-NOW using WiFi AP mode
3340
#define ESPNOW_WIFI_IF WIFI_IF_AP
3441
#define GET_IF_MAC WiFi.softAPmacAddress
3542

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});
3946
#endif
4047

4148
ESP_NOW_Serial_Class NowSerial(peer_mac, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IF);
@@ -47,7 +54,7 @@ void setup() {
4754

4855
#if ESPNOW_WIFI_MODE_STATION
4956
Serial.println("STA");
50-
WiFi.mode(ESPNOW_WIFI_MODE);
57+
WiFi.mode(WIFI_STA);
5158
// ToDo: Set the channel using WiFi.setChannel() when using Station mode
5259
esp_wifi_set_channel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
5360
#else
@@ -75,7 +82,7 @@ void loop() {
7582

7683
while (Serial.available() && NowSerial.availableForWrite())
7784
{
78-
if (NowSerial.write(Serial.read()))
85+
if (NowSerial.write(Serial.read()) <= 0)
7986
{
8087
Serial.println("Failed to send data");
8188
break;

0 commit comments

Comments
 (0)