This component provides an easy-to-use interface for wireless communication between ESP32 devices using the ESP-NOW protocol. It supports both Master and Slave modes, allowing you to send and receive data with minimal overhead.
In addition, the module offers connection handling, a ping mechanism to check if the slave is still online, and automatic reconnection for devices that disconnect.
- Master-Slave Communication: Supports both master and slave roles.
- Low overhead and low power: ESP-NOW minimizes power consumption.
- Wi-Fi Mode Configuration: Station or SoftAP mode options.
- Long-range mode: Optionally enable extended communication range.
- Automatic Reconnection: Auto-reconnect for devices that go offline.
- Ping Mechanism: Check the availability of connected devices.
- Callback Support: User-defined callbacks for handling received data.
Navigate to the components directory of your project, then clone the ESPNOW component:
cd <your esp-idf project folder>
git clone https://github.com/blackDogLz4/espnow/tree/mainThis will copy the component into your project's component folder.
Afterwards you have to add it to your top-level CMakeLists.txt e.g:
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS "components/espnowCom")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp-master)
Open the ESP-IDF configuration menu to configure your ESPNOW communication settings:
idf.py menuconfigNavigate to the Component Config and ESPNOW Communication Component to configure settings like Wi-Fi mode, long-range mode, and whether the device will function as a Master or Slave.
Note: If the watchdog gets triggered either set disable it for CPU1 or increase the blocking time of xQueueReceive() functions in the _espnowCom_com_handler() function
Once the component is set up and configured, include it in your code by calling the appropriate functions:
In master mode, you can send data to slaves, ping slaves to check if they are online, and register callbacks for receiving data.
#include "nvs_flash.h"
#include "espnowCom.h"
#define TAG "Main-Master"
// function will be called when data with type 0 is received
void recv_handler(int type, int slave, void *data, int len){
char *str;
str = (char*) data;
ESP_LOGI(TAG, "from %d, %s", slave, str);
}
void app_main(void)
{
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
// Initialize espnowCom
espnowCom_init();
vTaskDelay(100 / portTICK_PERIOD_MS);
// register callback to type 0
espnowCom_addRecv_cb(1, &recv_handler);
while(1){
// send "Hello world!" with type 0 to slave 0
espnowCom_send(0, 0, (void *)"Hello world!\n", 15);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}In slave mode, the device listens for data from the master and responds accordingly.
#include "nvs_flash.h"
#include "espnowCom.h"
#define TAG "Main-Slave"
// function will be called when data with type 0 is received
void handlerfunc(int type, void *data, int len){
char* str;
str = (char* )data;
ESP_LOGI(TAG, "received %s", str);
}
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
espnowCom_init();
vTaskDelay(100 / portTICK_PERIOD_MS);
// register callback to type 0
espnowCom_addRecv_cb(0, &handlerfunc);
while(1){
// send Hey with type 0 to the Master
espnowCom_send(0, "Hey", 4);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}After setting up the component and adding it to your project, use the following commands to build and flash your project:
idf.py build
idf.py flashMake sure both Master and Slave devices are set up and communicating using the ESP-NOW protocol.
The max. package Rate i was able to achieve was 1 pkg / 10ms --> 100 pkg /s
Propagation Delay < 2ms
Under the useful_scripts is a file called 62-esp32.rules containing example udev rules to link the connected esp32 devices to /dev/ttyESP-Now_Master or /dev/ttyESP-Now_Slave based on the USB-Port they are connected to. To make use of this rule simply copy or link it to /etc/udev/rules.d/. and reload the rules with
sudo udevadm control --reload-rules && sudo udevadm triggerNote: you will propably need to adjust ENV{ID_PATH}. You can find out your port with udevadm info /dev/ttyUSBX
To make live even easier link esp-nowIDF.sh to the root directory of your project. This small script checks if you are developing on Master / Slave and acts like idf.py but setting the correct port.
Examples:
./esp-nowIDF.sh flash # flash software to master/slave based on menuconfig configuration
./esp-nowIDF.sh monitor # monitor master/slave based on menuconfig configurationThis component is licensed under GPL2.