Master The ESP32 WiFi: ESPNow
Written by Harry Fairhead and Mike James   
Monday, 08 December 2025
Article Index
Master The ESP32 WiFi: ESPNow
Transmit
Listing

 ESPNow is a flexible peer-to-peer networking mode that is available for use with all ESP32 devices. The problem is that it can be difficult to get started, but not if you follow our simple introduction. This is an extract from our latest book on the ESP in C.

MasterThe ESP32In C:
WiFi with the ESP-IDF, FreeRTOS, LwIP & MbedTLS

By Harry Fairhead & Mike James

ESPMaster360

Buy from Amazon.

Contents

  • Chapter 1 FreeRTOS Basics 
  • Chapter 2 Locks and Synchronization
  • Chapter 3 Practical Tasks 
  • Chapter 4 Data Structures
  • Chapter 5 Basic WiFi
  • Chapter 6 Using LwIP
  • Chapter 7 Sockets and HTTP Clients 
  • Chapter 8 Socket Server
  • Chapter 9 Details of Cryptography
  • Chapter 10 SSL/TLS and HTTPS
  • Chapter 11 SSL Server
  • Chapter 12 UDP For Speed
  • Chapter 13 SNTP For Timekeeping
  • Chapter 14 SMTP For Email
  • Chapter 15 MQTT For The IoT
  • Chapter 16 Advanced WiFi and AP Mode
      Extract: 
     ESPNow  NEW!!!
  • Appendix I Advanced WiFi Configuration

<ASIN:B0G5M76KLB> 

 

Most of the time the ESP32 is used as a client to an AP and it engages in simply transferring data. However, the WiFi hardware can be used in other ways. The most direct is the software implementation of an Access Point, AP. In this chapter we look not only at how to use this, but what it can be used for.

Beyond the simple AP, the ESP32 can be used in a long range mode and it can implement a direct point-to-point network in the form of ESP Now. Going beyond this it can implement a complete mesh network using ESP WiFi Mesh.

In chapter but not in this extract

  • AP Mode
  • DHCP Server
  • An Initialization Server
  • An ESP32 Internet Access Point
  • Promiscuous Mode
  • Long Range Mode
  • ESP WiFi Mesh

ESP Now

The ESP Now module implements a very simple packet network between ESP32 devices. It makes use of the standard WiFi connection and the ESP32 devices can either be in AP or STA mode. It is a two-way protocol and a given ESP32 can send packets to other ESP32 devices and receive them from those devices.

It is a connection-less protocol which makes it easy to use, but it has no error correction or delivery guarantees and is restricted to 250 bytes of data per packet. In this respect it is more like UDP than TCP and if you need secure communication you need to implement error detection/correction and acknowledgment and if you need to send more than 250 bytes it you will need to package it and serialize it. This said there are many applications that don’t need these extras.

It implements optional encryption based on AES with a 128-bit key. Another key difference is that MAC addresses are used to specify the device that the packet is intended for. This is much simpler than using an IP address, but as there is no DNS for MAC addresses you have to invent a way to automatically find the MAC address of the target.

To use ESP Now all you have to do is turn WiFi on. You can do this by connecting to an AP or setting up an AP, but all you really have to do is start the WiFi:
void app_main(void)
{
  esp_event_loop_create_default();
  nvs_flash_init();
  esp_netif_init();
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  esp_wifi_init(&cfg);
  esp_wifi_set_mode(WIFI_MODE_STA);
  esp_wifi_set_channel(3,WIFI_SECOND_CHAN_NONE);
  esp_wifi_start();

Notice that we haven’t connected the WiFi to an access point and this means we have to set the channel that it is working on. For devices to communicate using ESP Now they have to be on the same channel.

As this program, which is in folder ESPNowReceive, is implementing a receiver, the transmitter needs to know its MAC address so the first thing to do is print it so that it can be transferred to the transmitter program:

  uint8_t mac[6];
    esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
for (int i = 0; i < 6; i++){
printf("%2X ", mac[i]);
}
printf("\n");

Next we can start ESP Now and simply listen for incoming packets:

    esp_now_init();
esp_now_register_recv_cb(example_espnow_recv_cb);
while (true)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
};
}

The recv callback simply prints the data that has been received:

static void example_espnow_recv_cb(
              const esp_now_recv_info_t *recv_info,
              const uint8_t *data, int len)
{
for (int i = 0; i < len; i++)
{
printf("%2X", data[i]);
}
printf("\n");
fflush(stdout);
}

If you run ESPNowReceive nothing happens until another ESP32 sends some data.



Last Updated ( Monday, 08 December 2025 )