Difficulty Level: Intermediate
ESP-NOW is a wireless communication protocol supported by ESP8266 and ESP32 boards. It allows devices to communicate with each other without the need for Wi-Fi. In this tutorial, we’ll build a mesh network using multiple ESP8266 modules to exchange data over ESP-NOW.
Ensure that you have the ESP8266 core installed in your Arduino IDE. If you haven’t installed it yet, follow these steps:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
The **ESP-NOW** library comes pre-installed with the ESP8266 core. If it’s not installed, search for "ESP8266" in **Library Manager** and install the appropriate library.
ESP-NOW requires each device to know the MAC address of the other devices it communicates with. We will start with a basic peer-to-peer communication example between two ESP8266 modules.
#include
#include
// MAC address of the other device (use your actual MAC addresses)
uint8_t broadcastAddress[] = {0x24, 0x6F, 0x28, 0xA4, 0xB6, 0x7F};
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Delivery Status: ");
Serial.println(sendStatus == 0 ? "Success" : "Fail");
}
void setup() {
Serial.begin(115200);
// Set device as Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set callback for sending data
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
// Add peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
// Example data to send
String message = "Hello, ESP-NOW!";
// Convert String to char array
uint8_t data[250];
message.getBytes(data, 250);
// Send message
esp_now_send(broadcastAddress, data, sizeof(data));
// Delay between messages
delay(2000);
}
To build a mesh network, each ESP8266 will act as both a sender and a receiver. Here’s how you can set up multiple ESP8266s to relay messages across the network:
#include
#include
void OnDataRecv(uint8_t *mac_addr, uint8_t *incomingData, uint8_t len) {
char message[len + 1];
memcpy(message, incomingData, len);
message[len] = '\0';
Serial.print("Received: ");
Serial.println(message);
}
void setup() {
Serial.begin(115200);
// Set device as Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set callback for receiving data
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Keep listening for incoming messages
}
#include
#include
// MAC address of the receiving device
uint8_t peerAddress[] = {0x24, 0x6F, 0x28, 0xA4, 0xB6, 0x7F};
void setup() {
Serial.begin(115200);
// Set device as Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Add peer
esp_now_add_peer(peerAddress, ESP_NOW_ROLE_CONTROLLER, 1, NULL, 0);
}
void loop() {
// Example data to send
String message = "Message from sender";
uint8_t data[250];
message.getBytes(data, 250);
// Send data
esp_now_send(peerAddress, data, sizeof(data));
delay(3000);
}
To expand this mesh network, each ESP8266 needs to be configured as both a sender and a receiver. You can program each ESP8266 to forward messages from one node to another, creating a larger mesh network.
Upload the sender code to one ESP8266 and the receiver code to another. Monitor the serial output to ensure data is being exchanged. For larger networks, test communication across all nodes.
By following these steps, you can create a basic mesh network using ESP-NOW with ESP8266 modules. ESP-NOW provides a low-power, low-latency method of communication, making it ideal for IoT projects requiring communication between multiple nodes.