Skip to content

Commit f5bfac7

Browse files
committed
ESP-NOW: Fix examples and improve logging
1 parent dcc3076 commit f5bfac7

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino

+5-4
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ void setup() {
6767
Serial.begin(115200);
6868
while (!Serial) delay(10);
6969

70+
// Initialize the Wi-Fi module
71+
WiFi.mode(WIFI_STA);
72+
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
73+
while(!WiFi.STA.started()) delay(100);
74+
7075
Serial.println("ESP-NOW Example - Broadcast Master");
7176
Serial.println("Wi-Fi parameters:");
7277
Serial.println(" Mode: STA");
7378
Serial.println(" MAC Address: " + WiFi.macAddress());
7479
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
7580

76-
// Initialize the Wi-Fi module
77-
WiFi.mode(WIFI_STA);
78-
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
79-
8081
// Register the broadcast peer
8182
if (!broadcast_peer.begin()) {
8283
Serial.println("Failed to initialize broadcast peer");

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public:
4545
return true;
4646
}
4747

48-
// Function to print the received messages from the master
49-
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
48+
// Function to print the received messages from the master
49+
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
5050
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
5151
Serial.printf(" Message: %s\n", (char *)data);
5252
}
@@ -85,16 +85,17 @@ void setup() {
8585
Serial.begin(115200);
8686
while (!Serial) delay(10);
8787

88+
// Initialize the Wi-Fi module
89+
WiFi.mode(WIFI_STA);
90+
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
91+
while(!WiFi.STA.started()) delay(100);
92+
8893
Serial.println("ESP-NOW Example - Broadcast Slave");
8994
Serial.println("Wi-Fi parameters:");
9095
Serial.println(" Mode: STA");
9196
Serial.println(" MAC Address: " + WiFi.macAddress());
9297
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
9398

94-
// Initialize the Wi-Fi module
95-
WiFi.mode(WIFI_STA);
96-
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
97-
9899
// Initialize the ESP-NOW protocol
99100
if (!ESP_NOW.begin()) {
100101
Serial.println("Failed to initialize ESP-NOW");

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino

+7-6
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ public:
162162
void onSent(bool success) {
163163
bool broadcast = memcmp(addr(), ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
164164
if (broadcast) {
165-
log_v("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
165+
log_i("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
166166
}
167167
else {
168-
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
168+
log_i("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
169169
}
170170
}
171171
};
@@ -253,16 +253,17 @@ void setup() {
253253
Serial.begin(115200);
254254
while (!Serial) delay(10);
255255

256+
// Initialize the Wi-Fi module
257+
WiFi.mode(WIFI_STA);
258+
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
259+
while(!WiFi.STA.started()) delay(100);
260+
256261
Serial.println("ESP-NOW Network Example");
257262
Serial.println("Wi-Fi parameters:");
258263
Serial.println(" Mode: STA");
259264
Serial.println(" MAC Address: " + WiFi.macAddress());
260265
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
261266

262-
// Initialize the Wi-Fi module
263-
WiFi.mode(WIFI_STA);
264-
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
265-
266267
// Generate yhis device's priority based on the 3 last bytes of the MAC address
267268
WiFi.macAddress(self_mac);
268269
self_priority = self_mac[3] << 16 | self_mac[4] << 8 | self_mac[5];

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void setup() {
5454
Serial.println(ESPNOW_WIFI_CHANNEL);
5555
WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
5656

57+
while(!(WiFi.STA.started() || WiFi.AP.started())) delay(100);
58+
5759
Serial.print("MAC Address: ");
5860
Serial.println(ESPNOW_WIFI_MODE == WIFI_AP ? WiFi.softAPmacAddress() : WiFi.macAddress());
5961

Diff for: libraries/ESP_NOW/src/ESP32_NOW.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ class ESP_NOW_Peer {
4141

4242
//optional callbacks to be implemented by the upper class
4343
virtual void onReceive(const uint8_t * data, size_t len, bool broadcast) {
44-
log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "broadcast" : "");
44+
log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "(broadcast)" : "");
45+
}
46+
47+
virtual void onSent(bool success) {
48+
log_i("Message transmission to peer " MACSTR " %s", MAC2STR(mac), success ? "successful" : "failed");
4549
}
46-
virtual void onSent(bool success) { log_i("Message reported as sent %s", success ? "successfully" : "unsuccessfully"); }
4750
};
4851

4952
class ESP_NOW_Class : public Print {

0 commit comments

Comments
 (0)