Skip to content

Commit 6f6e889

Browse files
committed
Improve example
1 parent f7278fa commit 6f6e889

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino

+27-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
3333

34-
#include <algorithm>
3534
#include <vector>
3635

3736
/* Definitions */
@@ -82,14 +81,14 @@ typedef struct {
8281
uint32_t count;
8382
uint32_t priority;
8483
uint32_t data;
84+
bool ready;
8585
char str[7];
8686
} __attribute__((packed)) esp_now_data_t;
8787

8888
/* Global Variables */
8989

9090
uint32_t self_priority = 0; // Priority of this device
9191
uint8_t current_peer_count = 0; // Number of peers that have been found
92-
uint8_t check_count = 0; // Counter to wait after all peers have been found
9392
bool device_is_master = false; // Flag to indicate if this device is the master
9493
bool master_decided = false; // Flag to indicate if the master has been decided
9594
uint32_t sent_msg_count = 0; // Counter for the messages sent. Only starts counting after all peers have been found
@@ -107,6 +106,7 @@ class ESP_NOW_Network_Peer : public ESP_NOW_Peer {
107106
public:
108107
uint32_t priority;
109108
bool peer_is_master = false;
109+
bool peer_ready = false;
110110

111111
ESP_NOW_Network_Peer(const uint8_t *mac_addr, uint32_t priority = 0, const uint8_t *lmk = (const uint8_t *)ESPNOW_EXAMPLE_LMK);
112112
~ESP_NOW_Network_Peer();
@@ -147,8 +147,14 @@ bool ESP_NOW_Network_Peer::send_message(const uint8_t *data, size_t len) {
147147
}
148148

149149
void ESP_NOW_Network_Peer::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
150+
esp_now_data_t *msg = (esp_now_data_t *)data;
151+
152+
if (peer_ready == false && msg->ready == true) {
153+
Serial.printf("Peer " MACSTR " reported ready\n", MAC2STR(addr()));
154+
peer_ready = true;
155+
}
156+
150157
if (!broadcast) {
151-
esp_now_data_t *msg = (esp_now_data_t *)data;
152158
recv_msg_count++;
153159
if (device_is_master) {
154160
Serial.printf("Received a message from peer " MACSTR "\n", MAC2STR(addr()));
@@ -212,6 +218,16 @@ uint32_t calc_average() {
212218
return avg;
213219
}
214220

221+
// Function to check if all peers are ready
222+
bool check_all_peers_ready() {
223+
for (auto &peer : peers) {
224+
if (!peer->peer_ready) {
225+
return false;
226+
}
227+
}
228+
return true;
229+
}
230+
215231
/* Callbacks */
216232

217233
// Callback called when a new peer is found
@@ -220,28 +236,23 @@ void register_new_peer(const esp_now_recv_info_t *info, const uint8_t *data, int
220236
int priority = msg->priority;
221237

222238
if (priority == self_priority) {
223-
Serial.println("ERROR! Device has the same priority as this device");
239+
Serial.println("ERROR! Device has the same priority as this device. Unsupported behavior.");
224240
fail_reboot();
225241
}
226242

227243
if (current_peer_count < ESPNOW_PEER_COUNT) {
228244
Serial.printf("New peer found: " MACSTR " with priority %d\n", MAC2STR(info->src_addr), priority);
229245
ESP_NOW_Network_Peer *new_peer = new ESP_NOW_Network_Peer(info->src_addr, priority);
230-
if (!new_peer->begin()) {
231-
Serial.println("Failed to create the new peer");
246+
if (new_peer == nullptr || !new_peer->begin()) {
247+
Serial.println("Failed to create or register the new peer");
232248
delete new_peer;
233249
return;
234250
}
235251
peers.push_back(new_peer);
236-
if (!peers.back()->begin()) {
237-
Serial.println("Failed to register the new peer");
238-
peers.pop_back();
239-
return;
240-
}
241252
current_peer_count++;
242253
if (current_peer_count == ESPNOW_PEER_COUNT) {
243254
Serial.println("All peers have been found");
244-
Serial.println("Broadcasting the priority 3 more times to ensure that all devices have received it");
255+
new_msg.ready = true;
245256
}
246257
}
247258
}
@@ -298,8 +309,9 @@ void loop() {
298309

299310
// Check if all peers have been found
300311
if (current_peer_count == ESPNOW_PEER_COUNT) {
301-
// Transmit the priority 3 more times to ensure that all devices have received it
302-
if (check_count >= 3) {
312+
// Wait until all peers are ready
313+
if (check_all_peers_ready()) {
314+
Serial.println("All peers are ready");
303315
// Check which device has the highest priority
304316
master_decided = true;
305317
uint32_t highest_priority = check_highest_priority();
@@ -318,8 +330,7 @@ void loop() {
318330
}
319331
Serial.println("The master has been decided");
320332
} else {
321-
Serial.printf("%d...\n", check_count + 1);
322-
check_count++;
333+
Serial.println("Waiting for all peers to be ready...");
323334
}
324335
}
325336
} else {

0 commit comments

Comments
 (0)