Skip to content

Commit ab31ab8

Browse files
committed
Improve broadcast master example
1 parent 34168a1 commit ab31ab8

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino

+19-29
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
2626

27-
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
27+
class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
2828
public:
29-
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
30-
~ESP_NOW_Peer_Class();
29+
ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);
30+
~ESP_NOW_Broadcast_Peer();
3131

32-
bool add_peer();
32+
bool begin();
3333
bool send_message(const uint8_t *data, size_t len);
3434

3535
// ESP_NOW_Peer interfaces
@@ -39,42 +39,40 @@ public:
3939

4040
/* Methods */
4141

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) {}
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) {}
4745

4846
// Destructor of the class
49-
ESP_NOW_Peer_Class::~ESP_NOW_Peer_Class() {
47+
ESP_NOW_Broadcast_Peer::~ESP_NOW_Broadcast_Peer() {
5048
remove();
5149
}
5250

53-
// Function to register the broadcast peer
54-
bool ESP_NOW_Peer_Class::add_peer() {
55-
if (!add()) {
56-
log_e("Failed to register the broadcast peer");
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");
5755
return false;
5856
}
5957
return true;
6058
}
6159

6260
// Function to send a message to all devices within the network
63-
bool ESP_NOW_Peer_Class::send_message(const uint8_t *data, size_t len) {
61+
bool ESP_NOW_Broadcast_Peer::send_message(const uint8_t *data, size_t len) {
6462
if (!send(data, len)) {
6563
log_e("Failed to broadcast message");
6664
return false;
6765
}
6866
return true;
6967
}
7068

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

77-
void ESP_NOW_Peer_Class::_onSent(bool success) {
75+
void ESP_NOW_Broadcast_Peer::_onSent(bool success) {
7876
// As broadcast messages does not require any acknowledgment, this method will never be called.
7977
// It is still required to be implemented because it is a pure virtual method.
8078
}
@@ -83,8 +81,8 @@ void ESP_NOW_Peer_Class::_onSent(bool success) {
8381

8482
uint32_t msg_count = 0;
8583

86-
// Create a peer object using the broadcast address
87-
ESP_NOW_Peer_Class broadcast_peer(ESP_NOW.BROADCAST_ADDR, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
84+
// Create a boradcast peer object
85+
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
8886

8987
/* Main */
9088

@@ -102,17 +100,9 @@ void setup() {
102100
WiFi.mode(WIFI_STA);
103101
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
104102

105-
// Initialize the ESP-NOW protocol
106-
if (!ESP_NOW.begin()) {
107-
Serial.println("Failed to initialize ESP-NOW");
108-
Serial.println("Reebooting in 5 seconds...");
109-
delay(5000);
110-
ESP.restart();
111-
}
112-
113103
// Register the broadcast peer
114-
if (!broadcast_peer.add_peer()) {
115-
Serial.println("Failed to register the broadcast peer");
104+
if (!broadcast_peer.begin()) {
105+
Serial.println("Failed to initialize broadcast peer");
116106
Serial.println("Reebooting in 5 seconds...");
117107
delay(5000);
118108
ESP.restart();

0 commit comments

Comments
 (0)