Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasssvaz committed Apr 3, 2024
1 parent 2b43448 commit ad193ae
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,44 @@

class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
public:
ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
~ESP_NOW_Broadcast_Peer();
// Constructor of the class using the broadcast address
ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
: ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}

bool begin();
bool send_message(const uint8_t *data, size_t len);

// ESP_NOW_Peer interfaces
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
void _onSent(bool success);
};

/* Methods */

// Constructor of the class using the broadcast address
ESP_NOW_Broadcast_Peer::ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
: ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}

// Destructor of the class
ESP_NOW_Broadcast_Peer::~ESP_NOW_Broadcast_Peer() {
remove();
}
// Destructor of the class
~ESP_NOW_Broadcast_Peer() {
remove();
}

// Function to properly initialize the ESP-NOW and register the broadcast peer
bool ESP_NOW_Broadcast_Peer::begin() {
if (!ESP_NOW.begin() || !add()) {
log_e("Failed to initialize ESP-NOW or register the broadcast peer");
return false;
// Function to properly initialize the ESP-NOW and register the broadcast peer
bool begin() {
if (!ESP_NOW.begin() || !add()) {
log_e("Failed to initialize ESP-NOW or register the broadcast peer");
return false;
}
return true;
}
return true;
}

// Function to send a message to all devices within the network
bool ESP_NOW_Broadcast_Peer::send_message(const uint8_t *data, size_t len) {
if (!send(data, len)) {
log_e("Failed to broadcast message");
return false;
// Function to send a message to all devices within the network
bool send_message(const uint8_t *data, size_t len) {
if (!send(data, len)) {
log_e("Failed to broadcast message");
return false;
}
return true;
}
return true;
}

void ESP_NOW_Broadcast_Peer::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
// The broadcast peer will never receive any data. Rather, it will only send data.
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
// It is still required to be implemented because it is a pure virtual method.
}
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
// The broadcast peer will never receive any data. Rather, it will only send data.
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
// It is still required to be implemented because it is a pure virtual method.
}

void ESP_NOW_Broadcast_Peer::_onSent(bool success) {
// As broadcast messages does not require any acknowledgment, this method will never be called.
// It is still required to be implemented because it is a pure virtual method.
}
void _onSent(bool success) {
// As broadcast messages does not require any acknowledgment, this method will never be called.
// It is still required to be implemented because it is a pure virtual method.
}
};

/* Global Variables */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,35 @@

class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
~ESP_NOW_Peer_Class();

bool add_peer();

// ESP_NOW_Peer interfaces
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
void _onSent(bool success);
};

/* Methods */

// Constructor of the class
ESP_NOW_Peer_Class::ESP_NOW_Peer_Class(const uint8_t *mac_addr,
uint8_t channel,
wifi_interface_t iface,
const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}

// Destructor of the class
ESP_NOW_Peer_Class::~ESP_NOW_Peer_Class() {}

// Function to register the master peer
bool ESP_NOW_Peer_Class::add_peer() {
if (!add()) {
log_e("Failed to register the broadcast peer");
return false;
// Constructor of the class
ESP_NOW_Peer_Class(const uint8_t *mac_addr,
uint8_t channel,
wifi_interface_t iface,
const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}

// Destructor of the class
~ESP_NOW_Peer_Class() {}

// Function to register the master peer
bool add_peer() {
if (!add()) {
log_e("Failed to register the broadcast peer");
return false;
}
return true;
}
return true;
}

// Function to print the received messages from the master
void ESP_NOW_Peer_Class::_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);
}
// 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);
}

void ESP_NOW_Peer_Class::_onSent(bool success) {
// In this example the slave will never send any data, so this method will never be called.
// It is still required to be implemented because it is a pure virtual method.
}
void _onSent(bool success) {
// In this example the slave will never send any data, so this method will never be called.
// It is still required to be implemented because it is a pure virtual method.
}
};

/* Global Variables */

Expand Down
106 changes: 47 additions & 59 deletions libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino
Original file line number Diff line number Diff line change
Expand Up @@ -108,79 +108,67 @@ public:
bool peer_is_master = false;
bool peer_ready = false;

ESP_NOW_Network_Peer(const uint8_t *mac_addr, uint32_t priority = 0, const uint8_t *lmk = (const uint8_t *)ESPNOW_EXAMPLE_LMK);
~ESP_NOW_Network_Peer();
ESP_NOW_Network_Peer(const uint8_t *mac_addr, uint32_t priority = 0, const uint8_t *lmk = (const uint8_t *)ESPNOW_EXAMPLE_LMK)
: ESP_NOW_Peer(mac_addr, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IFACE, lmk)
, priority(priority) {}

bool begin();
bool send_message(const uint8_t *data, size_t len);
~ESP_NOW_Network_Peer() {}

// ESP_NOW_Peer interfaces
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
void _onSent(bool success);
};

/* Methods */

ESP_NOW_Network_Peer::ESP_NOW_Network_Peer(const uint8_t *mac_addr, uint32_t priority, const uint8_t *lmk)
: ESP_NOW_Peer(mac_addr, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IFACE, lmk)
, priority(priority) {}

ESP_NOW_Network_Peer::~ESP_NOW_Network_Peer() {}

bool ESP_NOW_Network_Peer::begin() {
// In this example the ESP-NOW protocol will already be initialized as we require it to receive broadcast messages.
if (!add()) {
log_e("Failed to initialize ESP-NOW or register the peer");
return false;
bool begin() {
// In this example the ESP-NOW protocol will already be initialized as we require it to receive broadcast messages.
if (!add()) {
log_e("Failed to initialize ESP-NOW or register the peer");
return false;
}
return true;
}
return true;
}

bool ESP_NOW_Network_Peer::send_message(const uint8_t *data, size_t len) {
if (data == NULL || len == 0) {
log_e("Data to be sent is NULL or has a length of 0");
return false;
bool send_message(const uint8_t *data, size_t len) {
if (data == NULL || len == 0) {
log_e("Data to be sent is NULL or has a length of 0");
return false;
}

// Call the parent class method to send the data
return send(data, len);
}

// Call the parent class method to send the data
return send(data, len);
}
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
esp_now_data_t *msg = (esp_now_data_t *)data;

void ESP_NOW_Network_Peer::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
esp_now_data_t *msg = (esp_now_data_t *)data;
if (peer_ready == false && msg->ready == true) {
Serial.printf("Peer " MACSTR " reported ready\n", MAC2STR(addr()));
peer_ready = true;
}

if (peer_ready == false && msg->ready == true) {
Serial.printf("Peer " MACSTR " reported ready\n", MAC2STR(addr()));
peer_ready = true;
if (!broadcast) {
recv_msg_count++;
if (device_is_master) {
Serial.printf("Received a message from peer " MACSTR "\n", MAC2STR(addr()));
Serial.printf(" Count: %lu\n", msg->count);
Serial.printf(" Random data: %lu\n", msg->data);
last_data.push_back(msg->data);
last_data.erase(last_data.begin());
} else if (peer_is_master) {
Serial.println("Received a message from the master");
Serial.printf(" Average data: %lu\n", msg->data);
}
else {
Serial.printf("Peer " MACSTR " says: %s\n", MAC2STR(addr()), msg->str);
}
}
}

if (!broadcast) {
recv_msg_count++;
if (device_is_master) {
Serial.printf("Received a message from peer " MACSTR "\n", MAC2STR(addr()));
Serial.printf(" Count: %lu\n", msg->count);
Serial.printf(" Random data: %lu\n", msg->data);
last_data.push_back(msg->data);
last_data.erase(last_data.begin());
} else if (peer_is_master) {
Serial.println("Received a message from the master");
Serial.printf(" Average data: %lu\n", msg->data);
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");
}
else {
Serial.printf("Peer " MACSTR " says: %s\n", MAC2STR(addr()), msg->str);
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
}
}
}

void ESP_NOW_Network_Peer::_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");
}
else {
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
}
}
};

/* Peers */

Expand Down

0 comments on commit ad193ae

Please sign in to comment.