Skip to content

fix(esp_now): Fix broadcast example and use nullptr #11490

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -58,7 +58,7 @@ public:
uint32_t msg_count = 0;

// Create a broadcast peer object
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);

/* Main */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public:
/* Global Variables */

// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;
// Note: Using pointers instead of objects to prevent dangling pointers when the vector reallocates
std::vector<ESP_NOW_Peer_Class *> masters;

/* Callbacks */

Expand All @@ -62,13 +63,14 @@ void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, i
Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
Serial.println("Registering the peer as a master");

ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

masters.push_back(new_master);
if (!masters.back().add_peer()) {
ESP_NOW_Peer_Class *new_master = new ESP_NOW_Peer_Class(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
if (!new_master->add_peer()) {
Serial.println("Failed to register the new master");
delete new_master;
return;
}
masters.push_back(new_master);
Comment on lines +66 to +72
Copy link
Preview

Copilot AI Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Since master peers are now allocated using raw pointers and stored in a vector, consider using smart pointers (e.g., std::unique_ptr) to ensure proper resource management and avoid potential memory leaks.

Suggested change
ESP_NOW_Peer_Class *new_master = new ESP_NOW_Peer_Class(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
if (!new_master->add_peer()) {
Serial.println("Failed to register the new master");
delete new_master;
return;
}
masters.push_back(new_master);
auto new_master = std::make_unique<ESP_NOW_Peer_Class>(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, nullptr);
if (!new_master->add_peer()) {
Serial.println("Failed to register the new master");
return;
}
masters.push_back(std::move(new_master));

Copilot uses AI. Check for mistakes.

Serial.printf("Successfully registered master " MACSTR " (total masters: %zu)\n", MAC2STR(new_master->addr()), masters.size());
} else {
// The slave will only receive broadcast messages
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
Expand Down Expand Up @@ -103,11 +105,23 @@ void setup() {
}

// Register the new peer callback
ESP_NOW.onNewPeer(register_new_master, NULL);
ESP_NOW.onNewPeer(register_new_master, nullptr);

Serial.println("Setup complete. Waiting for a master to broadcast a message...");
}

void loop() {
delay(1000);
// Print debug information every 10 seconds
static unsigned long last_debug = 0;
if (millis() - last_debug > 10000) {
last_debug = millis();
Serial.printf("Registered masters: %zu\n", masters.size());
for (size_t i = 0; i < masters.size(); i++) {
if (masters[i]) {
Serial.printf(" Master %zu: " MACSTR "\n", i, MAC2STR(masters[i]->addr()));
}
}
}

delay(100);
}
13 changes: 8 additions & 5 deletions libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public:
}

bool send_message(const uint8_t *data, size_t len) {
if (data == NULL || len == 0) {
if (data == nullptr || len == 0) {
log_e("Data to be sent is NULL or has a length of 0");
return false;
}
Expand Down Expand Up @@ -169,9 +169,12 @@ public:

/* Peers */

std::vector<ESP_NOW_Network_Peer *> peers; // Create a vector to store the peer pointers
ESP_NOW_Network_Peer broadcast_peer(ESP_NOW.BROADCAST_ADDR, 0, NULL); // Register the broadcast peer (no encryption support for the broadcast address)
ESP_NOW_Network_Peer *master_peer = nullptr; // Pointer to peer that is the master
// Create a vector to store the peer pointers
std::vector<ESP_NOW_Network_Peer *> peers;
// Register the broadcast peer (no encryption support for the broadcast address)
ESP_NOW_Network_Peer broadcast_peer(ESP_NOW.BROADCAST_ADDR, 0, nullptr);
// Pointer to the peer that is the master
ESP_NOW_Network_Peer *master_peer = nullptr;

/* Helper functions */

Expand Down Expand Up @@ -279,7 +282,7 @@ void setup() {
}

// Register the callback to be called when a new peer is found
ESP_NOW.onNewPeer(register_new_peer, NULL);
ESP_NOW.onNewPeer(register_new_peer, nullptr);

Serial.println("Setup complete. Broadcasting own priority to find the master...");
memset(&new_msg, 0, sizeof(new_msg));
Expand Down
42 changes: 21 additions & 21 deletions libraries/ESP_NOW/src/ESP32_NOW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include "esp32-hal.h"
#include "esp_wifi.h"

static void (*new_cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) = NULL;
static void *new_arg = NULL; // * tx_arg = NULL, * rx_arg = NULL,
static void (*new_cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) = nullptr;
static void *new_arg = nullptr; // * tx_arg = nullptr, * rx_arg = nullptr,
static bool _esp_now_has_begun = false;
static ESP_NOW_Peer *_esp_now_peers[ESP_NOW_MAX_TOTAL_PEER_NUM];

static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, ESP_NOW_Peer *_peer = NULL) {
static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, ESP_NOW_Peer *_peer = nullptr) {
log_v(MACSTR, MAC2STR(mac_addr));
if (esp_now_is_peer_exist(mac_addr)) {
log_e("Peer Already Exists");
Expand All @@ -26,16 +26,16 @@ static esp_err_t _esp_now_add_peer(const uint8_t *mac_addr, uint8_t channel, wif
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
peer.channel = channel;
peer.ifidx = iface;
peer.encrypt = lmk != NULL;
peer.encrypt = lmk != nullptr;
if (lmk) {
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
}

esp_err_t result = esp_now_add_peer(&peer);
if (result == ESP_OK) {
if (_peer != NULL) {
if (_peer != nullptr) {
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] == NULL) {
if (_esp_now_peers[i] == nullptr) {
_esp_now_peers[i] = _peer;
return ESP_OK;
}
Expand Down Expand Up @@ -67,8 +67,8 @@ static esp_err_t _esp_now_del_peer(const uint8_t *mac_addr) {
}

for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i] = NULL;
if (_esp_now_peers[i] != nullptr && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i] = nullptr;
break;
}
}
Expand All @@ -87,7 +87,7 @@ static esp_err_t _esp_now_modify_peer(const uint8_t *mac_addr, uint8_t channel,
memcpy(peer.peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
peer.channel = channel;
peer.ifidx = iface;
peer.encrypt = lmk != NULL;
peer.encrypt = lmk != nullptr;
if (lmk) {
memcpy(peer.lmk, lmk, ESP_NOW_KEY_LEN);
}
Expand All @@ -111,17 +111,17 @@ static void _esp_now_rx_cb(const esp_now_recv_info_t *info, const uint8_t *data,
bool broadcast = memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
log_v("%s from " MACSTR ", data length : %u", broadcast ? "Broadcast" : "Unicast", MAC2STR(info->src_addr), len);
log_buf_v(data, len);
if (!esp_now_is_peer_exist(info->src_addr) && new_cb != NULL) {
if (!esp_now_is_peer_exist(info->src_addr) && new_cb != nullptr) {
log_v("Calling new_cb, peer not found.");
new_cb(info, data, len, new_arg);
return;
}
//find the peer and call it's callback
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL) {
if (_esp_now_peers[i] != nullptr) {
log_v("Checking peer " MACSTR, MAC2STR(_esp_now_peers[i]->addr()));
}
if (_esp_now_peers[i] != NULL && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
if (_esp_now_peers[i] != nullptr && memcmp(info->src_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
log_v("Calling onReceive");
_esp_now_peers[i]->onReceive(data, len, broadcast);
return;
Expand All @@ -133,7 +133,7 @@ static void _esp_now_tx_cb(const uint8_t *mac_addr, esp_now_send_status_t status
log_v(MACSTR " : %s", MAC2STR(mac_addr), (status == ESP_NOW_SEND_SUCCESS) ? "SUCCESS" : "FAILED");
//find the peer and call it's callback
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
if (_esp_now_peers[i] != nullptr && memcmp(mac_addr, _esp_now_peers[i]->addr(), ESP_NOW_ETH_ALEN) == 0) {
_esp_now_peers[i]->onSent(status == ESP_NOW_SEND_SUCCESS);
return;
}
Expand Down Expand Up @@ -197,7 +197,7 @@ bool ESP_NOW_Class::end() {
}
//remove all peers
for (uint8_t i = 0; i < ESP_NOW_MAX_TOTAL_PEER_NUM; i++) {
if (_esp_now_peers[i] != NULL) {
if (_esp_now_peers[i] != nullptr) {
removePeer(*_esp_now_peers[i]);
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ size_t ESP_NOW_Class::write(const uint8_t *data, size_t len) {
if (len > ESP_NOW_MAX_DATA_LEN) {
len = ESP_NOW_MAX_DATA_LEN;
}
esp_err_t result = esp_now_send(NULL, data, len);
esp_err_t result = esp_now_send(nullptr, data, len);
if (result == ESP_OK) {
return len;
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
Expand Down Expand Up @@ -292,7 +292,7 @@ ESP_NOW_Peer::ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interf
}
chan = channel;
ifc = iface;
encrypt = lmk != NULL;
encrypt = lmk != nullptr;
if (encrypt) {
memcpy(key, lmk, 16);
}
Expand All @@ -305,7 +305,7 @@ bool ESP_NOW_Peer::add() {
if (added) {
return true;
}
if (_esp_now_add_peer(mac, chan, ifc, encrypt ? key : NULL, this) != ESP_OK) {
if (_esp_now_add_peer(mac, chan, ifc, encrypt ? key : nullptr, this) != ESP_OK) {
return false;
}
log_v("Peer added - " MACSTR, MAC2STR(mac));
Expand Down Expand Up @@ -350,7 +350,7 @@ bool ESP_NOW_Peer::setChannel(uint8_t channel) {
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}

wifi_interface_t ESP_NOW_Peer::getInterface() const {
Expand All @@ -362,22 +362,22 @@ bool ESP_NOW_Peer::setInterface(wifi_interface_t iface) {
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}

bool ESP_NOW_Peer::isEncrypted() const {
return encrypt;
}

bool ESP_NOW_Peer::setKey(const uint8_t *lmk) {
encrypt = lmk != NULL;
encrypt = lmk != nullptr;
if (encrypt) {
memcpy(key, lmk, 16);
}
if (!_esp_now_has_begun || !added) {
return true;
}
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : NULL) == ESP_OK;
return _esp_now_modify_peer(mac, chan, ifc, encrypt ? key : nullptr) == ESP_OK;
}

size_t ESP_NOW_Peer::send(const uint8_t *data, int len) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP_NOW/src/ESP32_NOW.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ESP_NOW_Class : public Print {
ESP_NOW_Class();
~ESP_NOW_Class();

bool begin(const uint8_t *pmk = NULL /* 16 bytes */);
bool begin(const uint8_t *pmk = nullptr /* 16 bytes */);
bool end();

int getTotalPeerCount();
Expand Down Expand Up @@ -50,7 +50,7 @@ class ESP_NOW_Peer {
bool remove();
size_t send(const uint8_t *data, int len);

ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel = 0, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = NULL);
ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel = 0, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = nullptr);

public:
virtual ~ESP_NOW_Peer() {}
Expand Down
Loading
Loading