ESP-NOW is a connectionless communication protocol developed by Espressif. It functions similarly to Wi-Fi (802.11) but offers lower power consumption and minimal latency, making it ideal for point-to-point communication in IoT applications.
Key Features:
#include <esp_now.h> // ESP-NOW library
#include <WiFi.h> // Wi-Fi functions
// Structure to hold an incoming text message
struct MessagePacket {
char text[32];
};
// Callback when data arrives
void onMessageReceived(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
MessagePacket receivedPacket;
memcpy(&receivedPacket, data, sizeof(receivedPacket)); // Copy payload
// Print sender's MAC address
char senderMac[18];
snprintf(senderMac, sizeof(senderMac), "%02X:%02X:%02X:%02X:%02X:%02X",
info->src_addr[0], info->src_addr[1], info->src_addr[2],
info->src_addr[3], info->src_addr[4], info->src_addr[5]);
Serial.print("Received from MAC: "); Serial.println(senderMac);
// Print message text
Serial.print("Received message: "); Serial.println(receivedPacket.text);
}
void setup() {
Serial.begin(115200); // Start serial logging
WiFi.mode(WIFI_STA); // Set to station mode
delay(100); // Short delay to ensure MAC address is ready
// Show this board's MAC for peer registration
Serial.print("This Receiver's MAC: ");
Serial.println(WiFi.macAddress());
esp_now_init(); // Initialize ESP-NOW
esp_now_register_recv_cb(onMessageReceived); // Register receive callback
}
void loop() {
// Nothing needed here; callback handles incoming messages
}
<aside> <img src="/icons/info-alternate_blue.svg" alt="/icons/info-alternate_blue.svg" width="40px" />
</aside>
Upload Receiver sketch first, and note down the MAC address from serial monitor:
This Receiver's MAC: AA:BB:CC:DD:EE:FF
#include <esp_now.h>
#include <WiFi.h>
// Structure for the outgoing text message
struct MessagePacket {
char text[32];
};
MessagePacket sendPacket; // Buffer for outgoing message
int sendMessageCount = 0; // Counter for sent messages
// Replace with the Receiver's MAC address shown in its Serial Monitor
uint8_t receiverAddress[6] = {0x24,0x6F,0x28,0xAB,0xCD,0xEF};
void setup() {
Serial.begin(115200); // Start serial logging
WiFi.mode(WIFI_STA); // Station mode
delay(100); // Ensure MAC ready
esp_now_init(); // Initialize ESP-NOW
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, receiverAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo); // Register the Receiver as a peer
}
void loop() {
sendMessageCount++; // Increment send counter
// Prepare message with embedded counter
snprintf(sendPacket.text, sizeof(sendPacket.text), "Msg #%d from ESP32", sendMessageCount);
// Send the packet
bool ok = (esp_now_send(receiverAddress, (uint8_t *)&sendPacket, sizeof(sendPacket)) == ESP_OK);
Serial.print(ok ? "Sent: " : "Failed: ");
Serial.println(sendPacket.text);
delay(2000); // Send every 2 seconds
}
<aside> <img src="/icons/info-alternate_blue.svg" alt="/icons/info-alternate_blue.svg" width="40px" />
</aside>