You have an ESP32 with a button in one room and another ESP32 with a relay in another room, and you want the button to switch the relay. You could connect both to Wi-Fi and use MQTT, but that needs a working router and a server. There's a much simpler and faster way: ESP-NOW. The ESP32 sends the message directly to the other ESP32, without a router or internet.
What is ESP-NOW?
ESP-NOW is a protocol from Espressif (the company behind the ESP32 and ESP8266). It uses the chip's Wi-Fi radio, but without joining a network.
- Each device is identified by its MAC address (such as 24:6F:28:AA:BB:CC).
- A message can be up to 250 bytes: plenty for sensor readings or commands.
- Very fast: the message arrives in milliseconds, with no time spent connecting to a router.
- Range is roughly like Wi-Fi: tens of metres indoors, and much more in the open.
Step 1: Find the receiver's MAC address
Upload this to the receiving ESP32 and open the serial monitor:
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
delay(100);
Serial.println(WiFi.macAddress()); // note this address down
}
void loop() {}
Step 2: The sender code
We send a struct holding the device number, temperature and humidity:
#include <WiFi.h>
#include <esp_now.h>
uint8_t receiver[] = {0x24, 0x6F, 0x28, 0xAA, 0xBB, 0xCC}; // receiver's address
typedef struct {
int id;
float temp;
float hum;
} Msg;
Msg msg;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW not working");
return;
}
esp_now_peer_info_t peer = {};
memcpy(peer.peer_addr, receiver, 6);
peer.channel = 0; // same channel as the receiver
peer.encrypt = false;
esp_now_add_peer(&peer);
}
void loop() {
msg.id = 1;
msg.temp = 24.5; // your sensor reading here
msg.hum = 60;
esp_now_send(receiver, (uint8_t *)&msg, sizeof(msg));
delay(5000);
}
Step 3: The receiver code
#include <WiFi.h>
#include <esp_now.h>
typedef struct {
int id;
float temp;
float hum;
} Msg;
void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
Msg msg;
memcpy(&msg, data, sizeof(msg));
Serial.printf("Device %d: %.1f°C %.0f%%\n", msg.id, msg.temp, msg.hum);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_recv_cb(onReceive);
}
void loop() {}
Important: the struct must be exactly the same on the sender and receiver. The receive callback's signature also changed between versions of the ESP32 Arduino core. If the code won't compile, try the older form: void onReceive(const uint8_t *mac, const uint8_t *data, int len).
More than one device
- Several senders to one receiver: each sensor sends a different
id, so the receiver knows who sent it. This is the most common use. - One sender to several receivers: add each receiver as a peer, or send to
FF:FF:FF:FF:FF:FF(broadcast) to reach them all. - Up to 20 devices can be registered as peers at a time.
ESP-NOW and Wi-Fi at the same time
If the receiver is also connected to a router (to send readings to the internet), the senders must transmit on the same Wi-Fi channel as the router. This is the most common cause of trouble: the receiver changed channel when it joined the router, while the senders are still on the old one.
The fix: find the router's channel and set it in the senders, or have the sender try each channel until the message gets through.
Battery life: the biggest advantage
Because ESP-NOW doesn't need to connect to a router (which takes a second or more at high current), a sensor can wake from deep sleep, send in milliseconds, and go back to sleep. That makes a battery sensor last far longer than an ordinary Wi-Fi one. Read deep sleep on the ESP32.
Quick comparison
| Wi-Fi + MQTT | ESP-NOW | LoRa | |
|---|---|---|---|
| Needs a router | Yes | No | No |
| Range | Depends on the router | Tens of metres | Kilometres |
| Response time | A second or more to connect | Instant | Slow |
| Battery | Average | Excellent | Excellent |
| Reaches the internet | Directly | Through a connected receiver | Through a connected receiver |
For long distances read LoRa, and for internet and phone control read ESP32 and MQTT.
Project ideas
- A remote control made from an ESP32 and buttons, switching relays around the house.
- Temperature sensors in every room sending to one display.
- A wireless doorbell.
- A car or robot with a remote, no router needed.
If you want to learn the ESP32 and wireless communication step by step, see our electronics courses.
FAQ
Does ESP-NOW work between an ESP32 and an ESP8266? Yes, the protocol is the same, though the code and library are a little different on the ESP8266.
Can a message get lost? It can, especially with distance or walls. The send result tells you whether the message arrived, so you can resend.
Can I encrypt the messages? Yes, ESP-NOW supports encryption with a key you set on both devices, with a lower limit on the number of encrypted peers.