Skip to content

ESP-NOW: Fix examples and improve logging #9455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ void setup() {
Serial.begin(115200);
while (!Serial) delay(10);

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
while(!WiFi.STA.started()) delay(100);

Serial.println("ESP-NOW Example - Broadcast Master");
Serial.println("Wi-Fi parameters:");
Serial.println(" Mode: STA");
Serial.println(" MAC Address: " + WiFi.macAddress());
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);

// Register the broadcast peer
if (!broadcast_peer.begin()) {
Serial.println("Failed to initialize broadcast peer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public:
return true;
}

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

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
while(!WiFi.STA.started()) delay(100);

Serial.println("ESP-NOW Example - Broadcast Slave");
Serial.println("Wi-Fi parameters:");
Serial.println(" Mode: STA");
Serial.println(" MAC Address: " + WiFi.macAddress());
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);

// Initialize the ESP-NOW protocol
if (!ESP_NOW.begin()) {
Serial.println("Failed to initialize ESP-NOW");
Expand Down
13 changes: 7 additions & 6 deletions libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ public:
void onSent(bool success) {
bool broadcast = memcmp(addr(), ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
if (broadcast) {
log_v("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
log_i("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
}
else {
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
log_i("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
}
}
};
Expand Down Expand Up @@ -253,16 +253,17 @@ void setup() {
Serial.begin(115200);
while (!Serial) delay(10);

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
while(!WiFi.STA.started()) delay(100);

Serial.println("ESP-NOW Network Example");
Serial.println("Wi-Fi parameters:");
Serial.println(" Mode: STA");
Serial.println(" MAC Address: " + WiFi.macAddress());
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);

// Initialize the Wi-Fi module
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);

// Generate yhis device's priority based on the 3 last bytes of the MAC address
WiFi.macAddress(self_mac);
self_priority = self_mac[3] << 16 | self_mac[4] << 8 | self_mac[5];
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void setup() {
Serial.println(ESPNOW_WIFI_CHANNEL);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

while(!(WiFi.STA.started() || WiFi.AP.started())) delay(100);

Serial.print("MAC Address: ");
Serial.println(ESPNOW_WIFI_MODE == WIFI_AP ? WiFi.softAPmacAddress() : WiFi.macAddress());

Expand Down
7 changes: 5 additions & 2 deletions libraries/ESP_NOW/src/ESP32_NOW.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ class ESP_NOW_Peer {

//optional callbacks to be implemented by the upper class
virtual void onReceive(const uint8_t * data, size_t len, bool broadcast) {
log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "broadcast" : "");
log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "(broadcast)" : "");
}

virtual void onSent(bool success) {
log_i("Message transmission to peer " MACSTR " %s", MAC2STR(mac), success ? "successful" : "failed");
}
virtual void onSent(bool success) { log_i("Message reported as sent %s", success ? "successfully" : "unsuccessfully"); }
};

class ESP_NOW_Class : public Print {
Expand Down