|
| 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 | +} |
0 commit comments