Skip to content

Commit 03e837c

Browse files
committed
Add broadcast example
1 parent 98a6ae0 commit 03e837c

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/.skip.esp32h2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
ESP-NOW Broadcast Master
3+
Lucas Saavedra Vaz - 2024
4+
5+
This sketch demonstrates how to broadcast messages to all devices within the ESP-NOW network.
6+
This example is intended to be used with the ESP-NOW Broadcast Slave example.
7+
8+
The master device will broadcast a message every 5 seconds to all devices within the network.
9+
This will be done using by registering a peer object with the broadcast address.
10+
11+
The slave devices will receive the broadcasted messages and print them to the Serial Monitor.
12+
*/
13+
14+
#include "ESP32_NOW.h"
15+
#include "WiFi.h"
16+
17+
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
18+
19+
/* Definitions */
20+
21+
#define ESPNOW_WIFI_CHANNEL 6
22+
23+
/* Classes */
24+
25+
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
26+
27+
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
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();
31+
32+
bool add_peer();
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
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+
remove();
51+
}
52+
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");
57+
return false;
58+
}
59+
return true;
60+
}
61+
62+
// 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) {
64+
if (!send(data, len)) {
65+
log_e("Failed to broadcast message");
66+
return false;
67+
}
68+
return true;
69+
}
70+
71+
void ESP_NOW_Peer_Class::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
72+
// The broadcast peer will never receive any data. Rather, it will only send data.
73+
// Data broadcasted will be received by the actual object of the peer that made the broadcast.
74+
// It is still required to be implemented because it is a pure virtual method.
75+
}
76+
77+
void ESP_NOW_Peer_Class::_onSent(bool success) {
78+
// As broadcast messages does not require any acknowledgment, this method will never be called.
79+
// It is still required to be implemented because it is a pure virtual method.
80+
}
81+
82+
/* Global Variables */
83+
84+
uint32_t msg_count = 0;
85+
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);
88+
89+
/* Main */
90+
91+
void setup() {
92+
Serial.begin(115200);
93+
while (!Serial) delay(10);
94+
95+
Serial.println("ESP-NOW Example - Broadcast Master");
96+
Serial.println("Wi-Fi parameters:");
97+
Serial.println(" Mode: STA");
98+
Serial.println(" MAC Address: " + WiFi.macAddress());
99+
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
100+
101+
// Initialize the Wi-Fi module
102+
WiFi.mode(WIFI_STA);
103+
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
104+
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+
// Register the broadcast peer
114+
if (!broadcast_peer.add_peer()) {
115+
Serial.println("Failed to register the broadcast peer");
116+
Serial.println("Reebooting in 5 seconds...");
117+
delay(5000);
118+
ESP.restart();
119+
}
120+
121+
Serial.println("Setup complete. Broadcasting messages every 5 seconds.");
122+
}
123+
124+
void loop() {
125+
// Broadcast a message to all devices within the network
126+
char data[32];
127+
snprintf(data, sizeof(data), "Hello, World! #%lu", msg_count++);
128+
129+
Serial.printf("Broadcasting message: %s\n", data);
130+
131+
if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) {
132+
Serial.println("Failed to broadcast message");
133+
}
134+
135+
delay(5000);
136+
}

libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/.skip.esp32h2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
ESP-NOW Broadcast Slave
3+
Lucas Saavedra Vaz - 2024
4+
5+
This sketch demonstrates how to receive broadcast messages from a master device using the ESP-NOW protocol.
6+
7+
The master device will broadcast a message every 5 seconds to all devices within the network.
8+
9+
The slave devices will receive the broadcasted messages. If they are not from a known master, they will be registered as a new master
10+
using a callback function.
11+
*/
12+
13+
#include "ESP32_NOW.h"
14+
#include "WiFi.h"
15+
16+
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
17+
18+
#include <vector>
19+
20+
/* Definitions */
21+
22+
#define ESPNOW_WIFI_CHANNEL 6
23+
24+
/* Classes */
25+
26+
// Create a new class that inherits from the ESP_NOW_Peer class is required to implement the _onReceive and _onSent methods.
27+
28+
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
29+
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+
remove();
51+
}
52+
53+
// Function to register the master peer
54+
bool ESP_NOW_Peer_Class::add_peer() {
55+
if (!add()) {
56+
log_e("Failed to register the broadcast peer");
57+
return false;
58+
}
59+
return true;
60+
}
61+
62+
// Function to print the received messages from the master
63+
void ESP_NOW_Peer_Class::_onReceive(const uint8_t *data, size_t len, bool broadcast) {
64+
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
65+
Serial.printf(" Message: %s\n", (char *)data);
66+
}
67+
68+
void ESP_NOW_Peer_Class::_onSent(bool success) {
69+
// In this example the slave will never send any data, so this method will never be called.
70+
// It is still required to be implemented because it is a pure virtual method.
71+
}
72+
73+
/* Global Variables */
74+
75+
// List of all the masters. It will be populated when a new master is registered
76+
std::vector<ESP_NOW_Peer_Class> masters;
77+
78+
/* Callbacks */
79+
80+
// Callback called when an unknown peer sends a message
81+
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
82+
if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
83+
Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
84+
Serial.println("Registering the peer as a master");
85+
86+
ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
87+
88+
masters.push_back(new_master);
89+
if (!masters.back().add_peer()) {
90+
Serial.println("Failed to register the new master");
91+
return;
92+
}
93+
} else {
94+
// The slave will only receive broadcast messages
95+
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
96+
log_v("Igorning the message");
97+
}
98+
}
99+
100+
/* Main */
101+
102+
void setup() {
103+
Serial.begin(115200);
104+
while (!Serial) delay(10);
105+
106+
Serial.println("ESP-NOW Example - Broadcast Slave");
107+
Serial.println("Wi-Fi parameters:");
108+
Serial.println(" Mode: STA");
109+
Serial.println(" MAC Address: " + WiFi.macAddress());
110+
Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL);
111+
112+
// Initialize the Wi-Fi module
113+
WiFi.mode(WIFI_STA);
114+
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
115+
116+
// Initialize the ESP-NOW protocol
117+
if (!ESP_NOW.begin()) {
118+
Serial.println("Failed to initialize ESP-NOW");
119+
Serial.println("Reeboting in 5 seconds...");
120+
delay(5000);
121+
ESP.restart();
122+
}
123+
124+
// Register the new peer callback
125+
ESP_NOW.onNewPeer(register_new_master, NULL);
126+
127+
Serial.println("Setup complete. Waiting for a master to broadcast a message...");
128+
}
129+
130+
void loop() {
131+
delay(1000);
132+
}

0 commit comments

Comments
 (0)