Skip to content

Commit ad193ae

Browse files
committed
Update examples
1 parent 2b43448 commit ad193ae

File tree

3 files changed

+104
-139
lines changed

3 files changed

+104
-139
lines changed

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino

+31-43
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,44 @@
2626

2727
class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
2828
public:
29-
ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
30-
~ESP_NOW_Broadcast_Peer();
29+
// Constructor of the class using the broadcast address
30+
ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
31+
: ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}
3132

32-
bool begin();
33-
bool send_message(const uint8_t *data, size_t len);
34-
35-
// ESP_NOW_Peer interfaces
36-
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
37-
void _onSent(bool success);
38-
};
39-
40-
/* Methods */
41-
42-
// Constructor of the class using the broadcast address
43-
ESP_NOW_Broadcast_Peer::ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk)
44-
: ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}
45-
46-
// Destructor of the class
47-
ESP_NOW_Broadcast_Peer::~ESP_NOW_Broadcast_Peer() {
48-
remove();
49-
}
33+
// Destructor of the class
34+
~ESP_NOW_Broadcast_Peer() {
35+
remove();
36+
}
5037

51-
// Function to properly initialize the ESP-NOW and register the broadcast peer
52-
bool ESP_NOW_Broadcast_Peer::begin() {
53-
if (!ESP_NOW.begin() || !add()) {
54-
log_e("Failed to initialize ESP-NOW or register the broadcast peer");
55-
return false;
38+
// Function to properly initialize the ESP-NOW and register the broadcast peer
39+
bool begin() {
40+
if (!ESP_NOW.begin() || !add()) {
41+
log_e("Failed to initialize ESP-NOW or register the broadcast peer");
42+
return false;
43+
}
44+
return true;
5645
}
57-
return true;
58-
}
5946

60-
// Function to send a message to all devices within the network
61-
bool ESP_NOW_Broadcast_Peer::send_message(const uint8_t *data, size_t len) {
62-
if (!send(data, len)) {
63-
log_e("Failed to broadcast message");
64-
return false;
47+
// Function to send a message to all devices within the network
48+
bool send_message(const uint8_t *data, size_t len) {
49+
if (!send(data, len)) {
50+
log_e("Failed to broadcast message");
51+
return false;
52+
}
53+
return true;
6554
}
66-
return true;
67-
}
6855

69-
void ESP_NOW_Broadcast_Peer::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
70-
// The broadcast peer will never receive any data. Rather, it will only send data.
71-
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
72-
// It is still required to be implemented because it is a pure virtual method.
73-
}
56+
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
57+
// The broadcast peer will never receive any data. Rather, it will only send data.
58+
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
59+
// It is still required to be implemented because it is a pure virtual method.
60+
}
7461

75-
void ESP_NOW_Broadcast_Peer::_onSent(bool success) {
76-
// As broadcast messages does not require any acknowledgment, this method will never be called.
77-
// It is still required to be implemented because it is a pure virtual method.
78-
}
62+
void _onSent(bool success) {
63+
// As broadcast messages does not require any acknowledgment, this method will never be called.
64+
// It is still required to be implemented because it is a pure virtual method.
65+
}
66+
};
7967

8068
/* Global Variables */
8169

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino

+26-37
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,35 @@
2727

2828
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
2929
public:
30-
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
31-
~ESP_NOW_Peer_Class();
32-
33-
bool add_peer();
34-
35-
// ESP_NOW_Peer interfaces
36-
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
37-
void _onSent(bool success);
38-
};
39-
40-
/* Methods */
41-
42-
// Constructor of the class
43-
ESP_NOW_Peer_Class::ESP_NOW_Peer_Class(const uint8_t *mac_addr,
44-
uint8_t channel,
45-
wifi_interface_t iface,
46-
const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
47-
48-
// Destructor of the class
49-
ESP_NOW_Peer_Class::~ESP_NOW_Peer_Class() {}
50-
51-
// Function to register the master peer
52-
bool ESP_NOW_Peer_Class::add_peer() {
53-
if (!add()) {
54-
log_e("Failed to register the broadcast peer");
55-
return false;
30+
// Constructor of the class
31+
ESP_NOW_Peer_Class(const uint8_t *mac_addr,
32+
uint8_t channel,
33+
wifi_interface_t iface,
34+
const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
35+
36+
// Destructor of the class
37+
~ESP_NOW_Peer_Class() {}
38+
39+
// Function to register the master peer
40+
bool add_peer() {
41+
if (!add()) {
42+
log_e("Failed to register the broadcast peer");
43+
return false;
44+
}
45+
return true;
5646
}
57-
return true;
58-
}
5947

60-
// Function to print the received messages from the master
61-
void ESP_NOW_Peer_Class::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
62-
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
63-
Serial.printf(" Message: %s\n", (char *)data);
64-
}
48+
// Function to print the received messages from the master
49+
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
50+
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
51+
Serial.printf(" Message: %s\n", (char *)data);
52+
}
6553

66-
void ESP_NOW_Peer_Class::_onSent(bool success) {
67-
// In this example the slave will never send any data, so this method will never be called.
68-
// It is still required to be implemented because it is a pure virtual method.
69-
}
54+
void _onSent(bool success) {
55+
// In this example the slave will never send any data, so this method will never be called.
56+
// It is still required to be implemented because it is a pure virtual method.
57+
}
58+
};
7059

7160
/* Global Variables */
7261

Diff for: libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino

+47-59
Original file line numberDiff line numberDiff line change
@@ -108,79 +108,67 @@ public:
108108
bool peer_is_master = false;
109109
bool peer_ready = false;
110110

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-
~ESP_NOW_Network_Peer();
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+
: ESP_NOW_Peer(mac_addr, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IFACE, lmk)
113+
, priority(priority) {}
113114

114-
bool begin();
115-
bool send_message(const uint8_t *data, size_t len);
115+
~ESP_NOW_Network_Peer() {}
116116

117-
// ESP_NOW_Peer interfaces
118-
void _onReceive(const uint8_t *data, size_t len, bool broadcast);
119-
void _onSent(bool success);
120-
};
121-
122-
/* Methods */
123-
124-
ESP_NOW_Network_Peer::ESP_NOW_Network_Peer(const uint8_t *mac_addr, uint32_t priority, const uint8_t *lmk)
125-
: ESP_NOW_Peer(mac_addr, ESPNOW_WIFI_CHANNEL, ESPNOW_WIFI_IFACE, lmk)
126-
, priority(priority) {}
127-
128-
ESP_NOW_Network_Peer::~ESP_NOW_Network_Peer() {}
129-
130-
bool ESP_NOW_Network_Peer::begin() {
131-
// In this example the ESP-NOW protocol will already be initialized as we require it to receive broadcast messages.
132-
if (!add()) {
133-
log_e("Failed to initialize ESP-NOW or register the peer");
134-
return false;
117+
bool begin() {
118+
// In this example the ESP-NOW protocol will already be initialized as we require it to receive broadcast messages.
119+
if (!add()) {
120+
log_e("Failed to initialize ESP-NOW or register the peer");
121+
return false;
122+
}
123+
return true;
135124
}
136-
return true;
137-
}
138125

139-
bool ESP_NOW_Network_Peer::send_message(const uint8_t *data, size_t len) {
140-
if (data == NULL || len == 0) {
141-
log_e("Data to be sent is NULL or has a length of 0");
142-
return false;
126+
bool send_message(const uint8_t *data, size_t len) {
127+
if (data == NULL || len == 0) {
128+
log_e("Data to be sent is NULL or has a length of 0");
129+
return false;
130+
}
131+
132+
// Call the parent class method to send the data
133+
return send(data, len);
143134
}
144135

145-
// Call the parent class method to send the data
146-
return send(data, len);
147-
}
136+
void _onReceive(const uint8_t *data, size_t len, bool broadcast) {
137+
esp_now_data_t *msg = (esp_now_data_t *)data;
148138

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;
139+
if (peer_ready == false && msg->ready == true) {
140+
Serial.printf("Peer " MACSTR " reported ready\n", MAC2STR(addr()));
141+
peer_ready = true;
142+
}
151143

152-
if (peer_ready == false && msg->ready == true) {
153-
Serial.printf("Peer " MACSTR " reported ready\n", MAC2STR(addr()));
154-
peer_ready = true;
144+
if (!broadcast) {
145+
recv_msg_count++;
146+
if (device_is_master) {
147+
Serial.printf("Received a message from peer " MACSTR "\n", MAC2STR(addr()));
148+
Serial.printf(" Count: %lu\n", msg->count);
149+
Serial.printf(" Random data: %lu\n", msg->data);
150+
last_data.push_back(msg->data);
151+
last_data.erase(last_data.begin());
152+
} else if (peer_is_master) {
153+
Serial.println("Received a message from the master");
154+
Serial.printf(" Average data: %lu\n", msg->data);
155+
}
156+
else {
157+
Serial.printf("Peer " MACSTR " says: %s\n", MAC2STR(addr()), msg->str);
158+
}
159+
}
155160
}
156161

157-
if (!broadcast) {
158-
recv_msg_count++;
159-
if (device_is_master) {
160-
Serial.printf("Received a message from peer " MACSTR "\n", MAC2STR(addr()));
161-
Serial.printf(" Count: %lu\n", msg->count);
162-
Serial.printf(" Random data: %lu\n", msg->data);
163-
last_data.push_back(msg->data);
164-
last_data.erase(last_data.begin());
165-
} else if (peer_is_master) {
166-
Serial.println("Received a message from the master");
167-
Serial.printf(" Average data: %lu\n", msg->data);
162+
void _onSent(bool success) {
163+
bool broadcast = memcmp(addr(), ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
164+
if (broadcast) {
165+
log_v("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
168166
}
169167
else {
170-
Serial.printf("Peer " MACSTR " says: %s\n", MAC2STR(addr()), msg->str);
168+
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
171169
}
172170
}
173-
}
174-
175-
void ESP_NOW_Network_Peer::_onSent(bool success) {
176-
bool broadcast = memcmp(addr(), ESP_NOW.BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
177-
if (broadcast) {
178-
log_v("Broadcast message reported as sent %s", success ? "successfully" : "unsuccessfully");
179-
}
180-
else {
181-
log_v("Unicast message reported as sent %s to peer " MACSTR, success ? "successfully" : "unsuccessfully", MAC2STR(addr()));
182-
}
183-
}
171+
};
184172

185173
/* Peers */
186174

0 commit comments

Comments
 (0)