Arduino library · Communication

ESP-NOW on the ESP32: board to board with no router

ESP-NOW is Espressif’s own radio protocol: one ESP32 sends a short frame straight to another one’s MAC address, with no access point, no IP address and no connection to set up. A message of up to 250 bytes arrives within a millisecond or two, and the receiving radio answers with an acknowledgment, which suits remote buttons, sensor nodes on batteries and robot controllers.

Built in on Mokxi: #include <esp_now.h> and it builds, with nothing to install. Free, and no account needed.

What ESP-NOW (ESP32 core) does

A sketch puts WiFi in station mode, calls esp_now_init(), and then either adds the other board as a peer and calls esp_now_send(), or registers a receive callback and waits. The usual pattern is a struct defined the same way on both boards: the sender passes its bytes, the receiver copies them back into the struct. The send callback reports whether the other radio acknowledged.

The runnable project here is two ESP32s on one canvas: a button on GPIO 4 of one toggles an LED on GPIO 4 of the other. Mokxi simulates the air between every ESP board on the canvas, with each board’s MAC, channels, ACKs and timing, so a wrong MAC really gives "Delivery fail". There is no range or fading, and encryption is accepted but not applied. For the ESP8266’s different API, see the espnow.h page.

Library Manager nameESP-NOW (ESP32 core)
AuthorEspressif
Include#include <esp_now.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32
In the simulatorSimulated: it runs against the part the way it does on the bench.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32.

In Mokxi it is built in: #include <esp_now.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board it comes with the board package (Espressif), so the Arduino IDE has it as soon as the board is installed: there is nothing to add from the Library Manager.

The calls you will use

In the order a sketch meets them.

esp_now_init()

Starts ESP-NOW after WiFi.mode(WIFI_STA). Returns ESP_OK on success; nothing else in the API works before it.

esp_now_register_send_cb(onSent)

Registers the function told about each send: ESP_NOW_SEND_SUCCESS when the receiver’s radio acknowledged, a failure otherwise.

void onSent(const wifi_tx_info_t *info, esp_now_send_status_t status) {
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "ok" : "fail");
}

esp_now_peer_info_t peer = {}

Describes the board you will send to: copy its MAC into peer.peer_addr, set peer.channel to 0 for the current channel and peer.encrypt to false.

esp_now_add_peer(&peer)

Adds the receiver to the peer list. esp_now_send() to an address that is not a peer fails.

esp_now_send(receiverMac, (uint8_t *)&msg, sizeof(msg))

Sends up to 250 bytes to one peer. ESP_OK means the frame was queued; the send callback says later whether it arrived.

esp_now_register_recv_cb(onReceive)

Registers the function that runs for each frame received, with the sender’s address, the bytes and the length.

void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
  memcpy(&msg, data, sizeof(msg));
}

Example: ESP-NOW button and LED

On the ESP32 DevKit V1. It runs in the browser exactly as printed.

sketch.ino · ESP32 DevKit V1
// ESP-NOW sender: the button on GPIO 4 switches the LED on the other board.
//
// No wire and no router between the two ESP32s: ESP-NOW sends a frame straight
// to the receiver's MAC, and its radio answers with an ACK, which is what the
// send callback reports. In Mokxi every ESP board on the canvas shares the air.
//
// The receiver's MAC is written below. In this project the receiver's wifiMac
// setting is that address; on real boards, copy the line the receiver prints
// when it starts. Change one byte here and every send fails: nobody has that
// MAC, so no ACK comes back.

#include <WiFi.h>
#include <esp_now.h>

uint8_t receiverMac[] = {0x02, 0x4D, 0x4F, 0x00, 0x00, 0x02};

// The same struct on both boards: the receiver copies the bytes back into it.
typedef struct {
    bool on;
    uint32_t presses;
} Message;

const int kButton = 4; // to GND, with the pad's own pull-up
Message msg = {false, 0};
bool wasPressed = false;

// Arduino-ESP32 core 3.3's send callback. On cores 2.x to 3.2 the first
// argument was `const uint8_t *mac_addr`; Mokxi builds that one too.
void onSent(const wifi_tx_info_t *info, esp_now_send_status_t status)
{
    (void)info;
    Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery success" : "Delivery fail");
}

void setup()
{
    Serial.begin(115200);
    pinMode(kButton, INPUT_PULLUP);
    WiFi.mode(WIFI_STA);
    Serial.print("Sender MAC: ");
    Serial.println(WiFi.macAddress());

    if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
    }
    esp_now_register_send_cb(onSent);

    esp_now_peer_info_t peer = {};
    memcpy(peer.peer_addr, receiverMac, 6);
    peer.channel = 0; // the radio's own channel
    peer.encrypt = false;
    if (esp_now_add_peer(&peer) != ESP_OK) {
        Serial.println("Failed to add peer");
        return;
    }
    Serial.println("Press the button");
}

void loop()
{
    // Read every 20 ms: longer than the button bounces, so one press is one.
    bool pressed = digitalRead(kButton) == LOW;
    if (pressed && !wasPressed) {
        msg.on = !msg.on;
        msg.presses++;
        esp_err_t result = esp_now_send(receiverMac, (uint8_t *)&msg, sizeof(msg));
        Serial.print("Press ");
        Serial.print(msg.presses);
        Serial.print(": LED ");
        Serial.print(msg.on ? "on" : "off");
        Serial.println(result == ESP_OK ? ", sent" : ", not sent");
    }
    wasPressed = pressed;
    delay(20);
}

Open it in the editor, wired and ready to run · ESP-NOW button and LED: the project page

Common errors and how to fix them

invalid conversion from 'void (*)(const uint8_t*, esp_now_send_status_t)'

The callback signatures changed with Arduino-ESP32 core 3.x. The send callback now takes const wifi_tx_info_t *info (core 3.3) and the receive callback const esp_now_recv_info_t *info, with the sender’s MAC in info->src_addr. Match the core you build with; Mokxi accepts both forms.

Every send prints "Delivery fail"

No radio acknowledged that MAC. Print WiFi.macAddress() on the receiver and copy it byte for byte into the sender, and keep both boards on the same channel. In the Mokxi project the receiver’s wifiMac setting is that address; clear it and every send fails.

ESP-NOW stops working once the board joins my router

Joining an access point moves the radio to the router’s channel, and frames on another channel are not heard. Put the receiver on the same channel, or set the peer channel to 0 so it follows the current one.

The received numbers are garbage

The struct differs between the two sketches, so the bytes are read with the wrong layout. Copy the exact same typedef into both, check len equals sizeof(msg) before memcpy(), and avoid String fields, which hold a pointer rather than the text.

Questions and answers

Can an ESP32 and an ESP8266 talk ESP-NOW?

Yes, the frames are the same on the air. The ESP8266 uses espnow.h with roles and a different callback signature, but a struct of fixed-size fields reads the same on both. Mokxi carries frames between both kinds of board on one canvas.

How far does ESP-NOW reach?

It depends on the antenna, walls and interference, so test on real boards where they will be mounted. The simulator has no range, so every board on the canvas hears every other.

Try ESP-NOW (ESP32 core) without installing anything

Open the example, change the sketch, and keep your version in a free account.