24
24
25
25
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
26
26
27
- class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
27
+ class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
28
28
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 ();
31
31
32
- bool add_peer ();
32
+ bool begin ();
33
33
bool send_message (const uint8_t *data, size_t len);
34
34
35
35
// ESP_NOW_Peer interfaces
@@ -39,42 +39,40 @@ public:
39
39
40
40
/* Methods */
41
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) {}
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) {}
47
45
48
46
// Destructor of the class
49
- ESP_NOW_Peer_Class ::~ESP_NOW_Peer_Class () {
47
+ ESP_NOW_Broadcast_Peer ::~ESP_NOW_Broadcast_Peer () {
50
48
remove ();
51
49
}
52
50
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" );
57
55
return false ;
58
56
}
59
57
return true ;
60
58
}
61
59
62
60
// 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) {
64
62
if (!send (data, len)) {
65
63
log_e (" Failed to broadcast message" );
66
64
return false ;
67
65
}
68
66
return true ;
69
67
}
70
68
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) {
72
70
// The broadcast peer will never receive any data. Rather, it will only send data.
73
71
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
74
72
// It is still required to be implemented because it is a pure virtual method.
75
73
}
76
74
77
- void ESP_NOW_Peer_Class ::_onSent (bool success) {
75
+ void ESP_NOW_Broadcast_Peer ::_onSent (bool success) {
78
76
// As broadcast messages does not require any acknowledgment, this method will never be called.
79
77
// It is still required to be implemented because it is a pure virtual method.
80
78
}
@@ -83,8 +81,8 @@ void ESP_NOW_Peer_Class::_onSent(bool success) {
83
81
84
82
uint32_t msg_count = 0 ;
85
83
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 );
88
86
89
87
/* Main */
90
88
@@ -102,17 +100,9 @@ void setup() {
102
100
WiFi.mode (WIFI_STA);
103
101
WiFi.setChannel (ESPNOW_WIFI_CHANNEL);
104
102
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
-
113
103
// 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" );
116
106
Serial.println (" Reebooting in 5 seconds..." );
117
107
delay (5000 );
118
108
ESP.restart ();
0 commit comments