31
31
32
32
#include < esp_mac.h> // For the MAC2STR and MACSTR macros
33
33
34
- #include < algorithm>
35
34
#include < vector>
36
35
37
36
/* Definitions */
@@ -82,14 +81,14 @@ typedef struct {
82
81
uint32_t count;
83
82
uint32_t priority;
84
83
uint32_t data;
84
+ bool ready;
85
85
char str[7 ];
86
86
} __attribute__((packed)) esp_now_data_t ;
87
87
88
88
/* Global Variables */
89
89
90
90
uint32_t self_priority = 0 ; // Priority of this device
91
91
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
93
92
bool device_is_master = false ; // Flag to indicate if this device is the master
94
93
bool master_decided = false ; // Flag to indicate if the master has been decided
95
94
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 {
107
106
public:
108
107
uint32_t priority;
109
108
bool peer_is_master = false ;
109
+ bool peer_ready = false ;
110
110
111
111
ESP_NOW_Network_Peer (const uint8_t *mac_addr, uint32_t priority = 0 , const uint8_t *lmk = (const uint8_t *)ESPNOW_EXAMPLE_LMK);
112
112
~ESP_NOW_Network_Peer ();
@@ -147,8 +147,14 @@ bool ESP_NOW_Network_Peer::send_message(const uint8_t *data, size_t len) {
147
147
}
148
148
149
149
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
+
150
157
if (!broadcast) {
151
- esp_now_data_t *msg = (esp_now_data_t *)data;
152
158
recv_msg_count++;
153
159
if (device_is_master) {
154
160
Serial.printf (" Received a message from peer " MACSTR " \n " , MAC2STR (addr ()));
@@ -212,6 +218,16 @@ uint32_t calc_average() {
212
218
return avg;
213
219
}
214
220
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
+
215
231
/* Callbacks */
216
232
217
233
// 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
220
236
int priority = msg->priority ;
221
237
222
238
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. " );
224
240
fail_reboot ();
225
241
}
226
242
227
243
if (current_peer_count < ESPNOW_PEER_COUNT) {
228
244
Serial.printf (" New peer found: " MACSTR " with priority %d\n " , MAC2STR (info->src_addr ), priority);
229
245
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" );
232
248
delete new_peer;
233
249
return ;
234
250
}
235
251
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
- }
241
252
current_peer_count++;
242
253
if (current_peer_count == ESPNOW_PEER_COUNT) {
243
254
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 ;
245
256
}
246
257
}
247
258
}
@@ -298,8 +309,9 @@ void loop() {
298
309
299
310
// Check if all peers have been found
300
311
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" );
303
315
// Check which device has the highest priority
304
316
master_decided = true ;
305
317
uint32_t highest_priority = check_highest_priority ();
@@ -318,8 +330,7 @@ void loop() {
318
330
}
319
331
Serial.println (" The master has been decided" );
320
332
} else {
321
- Serial.printf (" %d...\n " , check_count + 1 );
322
- check_count++;
333
+ Serial.println (" Waiting for all peers to be ready..." );
323
334
}
324
335
}
325
336
} else {
0 commit comments