Compare commits

...

4 Commits
v1.0.4 ... main

Author SHA1 Message Date
af7fa204e1 Version 1.3.1
Fixed an error of the unsupported WiFi protocol bitmap for ESP32-C2 and ESP8266.
2025-02-15 09:47:24 +03:00
f4ecd08e45 Version 1.3.0
Added support for the node's battery operation mode.
2025-01-20 17:28:21 +03:00
7608ba0abf Version 1.2.0
Added function to get ESP-NOW version.
Minor main code refactoring.
2025-01-18 12:50:25 +03:00
83cebd5677 Version 1.1.0
Added support for ESP-NOW v2.0.
The default initialization values have been changed.
ESP-NOW operation has been fixed for B/LR protocols only.
Some functions have been moved to IRAM.
Heap memory allocation functions have been changed.
Refactoring of the main code.
2025-01-16 18:37:47 +03:00
4 changed files with 107 additions and 74 deletions

View File

@ -3,21 +3,24 @@
## Tested on
1. ESP8266 RTOS_SDK v3.4
2. ESP32 ESP-IDF v5.2
2. ESP32 ESP-IDF v5.4
## Features
1. The maximum size of transmitted data is up to 250 bytes.
1. The maximum size of the transmitted data is up to 250 / 1490 bytes. Please see attention for details.
2. Support of any data types.
3. All nodes are not visible to the network scanner.
4. Not required a pre-pairings for data transfer.
5. Broadcast or unicast data transmissions.
6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW.
6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW. Please see attention for details.
## Attention
1. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
2. All devices on the network must have the same WiFi channel.
1. For correct operation ESP-NOW interface must be the same as the WiFi interface (except in the case of APSTA mode - the ESP-NOW interface can be anything).
2. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
3. All devices on the network must have the same WiFi channel.
4. For use encrypted messages, use the application layer.
5. ESP-NOW supports two versions: v1.0 (RTOS_SDK and ESP-IDF v5.3 and below) and v2.0 (ESP-IDF v5.4 and highter). The maximum packet length supported by v2.0 devices is 1490 bytes, while the maximum packet length supported by v1.0 devices is 250 bytes. The v2.0 devices are capable of receiving packets from both v2.0 and v1.0 devices. In contrast, v1.0 devices can only receive packets from other v1.0 devices. However, v1.0 devices can receive v2.0 packets if the packet length is less than or equal to 250 bytes. For packets exceeding this length, the v1.0 devices will either truncate the data to the first 250 bytes or discard the packet entirely.
## Using
@ -52,7 +55,7 @@ Sending and receiving messages:
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
uint8_t target[6] = {0x34, 0x94, 0x54, 0x24, 0xA3, 0x41};
uint8_t target[6] = {0xEC, 0x94, 0xCB, 0x87, 0xEC, 0xFC};
typedef struct
{
@ -83,6 +86,7 @@ void app_main(void)
strcpy(send_message.char_value, "THIS IS A CHAR");
send_message.float_value = 1.234;
send_message.bool_value = false;
printf("Used ESP-NOW version %d.\n", zh_espnow_get_version());
for (;;)
{
send_message.int_value = esp_random();

View File

@ -11,13 +11,13 @@
#define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \
{ \
.task_priority = 4, \
.stack_size = 2048, \
.queue_size = 32, \
.task_priority = 10, \
.stack_size = 3072, \
.queue_size = 64, \
.wifi_interface = WIFI_IF_STA, \
.wifi_channel = 1, \
.attempts = 3 \
}
.attempts = 3, \
.battery_mode = false}
#ifdef __cplusplus
extern "C"
@ -26,12 +26,13 @@ extern "C"
typedef struct // Structure for initial initialization of ESP-NOW interface.
{
uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 5.
uint16_t stack_size; // Stack size for task for the ESP-NOW messages processing. @note The minimum size is 2048 bytes.
uint8_t queue_size; // Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 16.
wifi_interface_t wifi_interface; // WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface.
uint8_t wifi_channel; // Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14.
uint8_t attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5.
bool battery_mode; // Battery operation mode. If true, the node does not receive messages.
} zh_espnow_init_config_t;
ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
@ -50,15 +51,15 @@ extern "C"
typedef struct // Structure for sending data to the event handler when an ESP-NOW message was sent. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_SEND_EVENT event.
{
uint8_t mac_addr[6]; // MAC address of the device to which the ESP-NOW message was sent.
uint8_t mac_addr[ESP_NOW_ETH_ALEN]; // MAC address of the device to which the ESP-NOW message was sent.
zh_espnow_on_send_event_type_t status; // Status of sent ESP-NOW message.
} zh_espnow_event_on_send_t;
typedef struct // Structure for sending data to the event handler when an ESP-NOW message was received. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_RECV_EVENT event.
{
uint8_t mac_addr[6]; // MAC address of the sender ESP-NOW message.
uint8_t *data; // Pointer to the data of the received ESP-NOW message.
uint8_t data_len; // Size of the received ESP-NOW message.
uint8_t mac_addr[ESP_NOW_ETH_ALEN]; // MAC address of the sender ESP-NOW message.
uint8_t *data; // Pointer to the data of the received ESP-NOW message.
uint16_t data_len; // Size of the received ESP-NOW message.
} zh_espnow_event_on_recv_t;
/**
@ -105,6 +106,14 @@ extern "C"
*/
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
/**
* @brief Get ESP-NOW version.
*
* @return
* - ESP-NOW version
*/
uint8_t zh_espnow_get_version(void);
#ifdef __cplusplus
}
#endif

View File

@ -1 +1 @@
1.0.4
1.3.1

134
zh_espnow.c Normal file → Executable file
View File

@ -20,6 +20,11 @@ static TaskHandle_t _processing_task_handle = {0};
static zh_espnow_init_config_t _init_config = {0};
static bool _is_initialized = false;
static uint8_t _attempts = 0;
#if defined ESP_NOW_MAX_DATA_LEN_V2
static uint16_t _max_message_size = ESP_NOW_MAX_DATA_LEN_V2;
#else
static uint16_t _max_message_size = ESP_NOW_MAX_DATA_LEN;
#endif
typedef struct
{
@ -30,9 +35,9 @@ typedef struct
} id;
struct
{
uint8_t mac_addr[6];
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
uint8_t *payload;
uint8_t payload_len;
uint16_t payload_len;
} data;
} _queue_t;
@ -61,23 +66,39 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
else if (err == ESP_FAIL)
{
uint8_t prim = 0;
wifi_second_chan_t sec = 0;
wifi_second_chan_t sec = WIFI_SECOND_CHAN_NONE;
esp_wifi_get_channel(&prim, &sec);
if (prim != _init_config.wifi_channel)
{
ESP_LOGW(TAG, "ESP-NOW initialization warning. The device is connected to the router. Channel %d will be used for ESP-NOW.", prim);
}
}
#if defined CONFIG_IDF_TARGET_ESP8266 || CONFIG_IDF_TARGET_ESP32C2
esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B);
#else
esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_LR);
#endif
_event_group_handle = xEventGroupCreate();
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t));
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
if (_init_config.battery_mode == false)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
return ESP_FAIL;
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
return ESP_FAIL;
}
}
if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
else
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
return ESP_FAIL;
}
}
if (xTaskCreatePinnedToCore(&_processing, "zh_espnow_processing", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
return ESP_FAIL;
}
_is_initialized = true;
@ -96,7 +117,10 @@ esp_err_t zh_espnow_deinit(void)
vEventGroupDelete(_event_group_handle);
vQueueDelete(_queue_handle);
esp_now_unregister_send_cb();
esp_now_unregister_recv_cb();
if (_init_config.battery_mode == false)
{
esp_now_unregister_recv_cb();
}
esp_now_deinit();
vTaskDelete(_processing_task_handle);
_is_initialized = false;
@ -119,7 +143,7 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. ESP-NOW not initialized.");
return ESP_FAIL;
}
if (data == NULL || data_len == 0 || data_len > ESP_NOW_MAX_DATA_LEN)
if (data == NULL || data_len == 0 || data_len > _max_message_size)
{
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
@ -129,33 +153,30 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8
ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full.");
return ESP_ERR_INVALID_STATE;
}
uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t broadcast[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
_queue_t queue = {0};
queue.id = TO_SEND;
if (target == NULL)
{
memcpy(queue.data.mac_addr, broadcast, 6);
memcpy(queue.data.mac_addr, broadcast, ESP_NOW_ETH_ALEN);
}
else
{
memcpy(queue.data.mac_addr, target, 6);
}
if (data_len / sizeof(void *) == 0)
{
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT);
}
else
{
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT);
memcpy(queue.data.mac_addr, target, ESP_NOW_ETH_ALEN);
}
queue.data.payload = heap_caps_calloc(1, data_len, MALLOC_CAP_8BIT);
if (queue.data.payload == NULL)
{
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap.");
return ESP_ERR_NO_MEM;
}
memset(queue.data.payload, 0, data_len);
memcpy(queue.data.payload, data, data_len);
queue.data.payload_len = data_len;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
return ESP_FAIL;
}
if (target == NULL)
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
@ -164,15 +185,10 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
}
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
return ESP_FAIL;
}
return ESP_OK;
}
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
static void IRAM_ATTR _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if (status == ESP_NOW_SEND_SUCCESS)
{
@ -185,9 +201,9 @@ static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
}
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
static void IRAM_ATTR _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
#else
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len)
static void IRAM_ATTR _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len)
#endif
{
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
@ -203,38 +219,31 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat
_queue_t queue = {0};
queue.id = ON_RECV;
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
memcpy(queue.data.mac_addr, mac_addr, 6);
memcpy(queue.data.mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
#else
memcpy(queue.data.mac_addr, esp_now_info->src_addr, 6);
memcpy(queue.data.mac_addr, esp_now_info->src_addr, ESP_NOW_ETH_ALEN);
#endif
if (data_len / sizeof(void *) == 0)
{
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT);
}
else
{
queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT);
}
queue.data.payload = heap_caps_calloc(1, data_len, MALLOC_CAP_8BIT);
if (queue.data.payload == NULL)
{
ESP_LOGE(TAG, "Adding incoming ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap.");
return;
}
memset(queue.data.payload, 0, data_len);
memcpy(queue.data.payload, data, data_len);
queue.data.payload_len = data_len;
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
return;
}
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
#else
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
#endif
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
}
static void _processing(void *pvParameter)
static void IRAM_ATTR _processing(void *pvParameter)
{
_queue_t queue = {0};
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
@ -244,16 +253,15 @@ static void _processing(void *pvParameter)
{
case TO_SEND:
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr));
esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
esp_now_peer_info_t *peer = heap_caps_calloc(1, sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
if (peer == NULL)
{
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
heap_caps_free(queue.data.payload);
break;
}
memset(peer, 0, sizeof(esp_now_peer_info_t));
peer->ifidx = _init_config.wifi_interface;
memcpy(peer->peer_addr, queue.data.mac_addr, 6);
memcpy(peer->peer_addr, queue.data.mac_addr, ESP_NOW_ETH_ALEN);
err = esp_now_add_peer(peer);
if (err == ESP_ERR_ESPNOW_NO_MEM)
{
@ -269,7 +277,7 @@ static void _processing(void *pvParameter)
heap_caps_free(peer);
break;
}
zh_espnow_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_espnow_event_on_send_t), MALLOC_CAP_8BIT);
zh_espnow_event_on_send_t *on_send = heap_caps_calloc(1, sizeof(zh_espnow_event_on_send_t), MALLOC_CAP_8BIT);
if (on_send == NULL)
{
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap.");
@ -277,8 +285,7 @@ static void _processing(void *pvParameter)
heap_caps_free(peer);
break;
}
memset(on_send, 0, sizeof(zh_espnow_event_on_send_t));
memcpy(on_send->mac_addr, queue.data.mac_addr, 6);
memcpy(on_send->mac_addr, queue.data.mac_addr, ESP_NOW_ETH_ALEN);
SEND:
++_attempts;
err = esp_now_send(queue.data.mac_addr, queue.data.payload, queue.data.payload_len);
@ -293,7 +300,7 @@ static void _processing(void *pvParameter)
}
else if (err != ESP_OK)
{
ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error.");
ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error at line %d.", __LINE__);
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
heap_caps_free(on_send);
@ -307,7 +314,7 @@ static void _processing(void *pvParameter)
EventBits_t bit = xEventGroupWaitBits(_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS);
if ((bit & DATA_SEND_SUCCESS) != 0)
{
ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.mac_addr));
ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success after %d attempts.", MAC2STR(queue.data.mac_addr), _attempts);
on_send->status = ZH_ESPNOW_SEND_SUCCESS;
_attempts = 0;
}
@ -321,11 +328,14 @@ static void _processing(void *pvParameter)
on_send->status = ZH_ESPNOW_SEND_FAIL;
_attempts = 0;
}
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_SEND_EVENT, on_send, sizeof(zh_espnow_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
else
{
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
}
heap_caps_free(queue.data.payload);
esp_now_del_peer(peer->peer_addr);
heap_caps_free(peer);
@ -334,15 +344,25 @@ static void _processing(void *pvParameter)
case ON_RECV:
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr));
zh_espnow_event_on_recv_t *recv_data = (zh_espnow_event_on_recv_t *)&queue.data;
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, recv_data->data_len + 7, portTICK_PERIOD_MS) != ESP_OK)
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, recv_data->data_len + sizeof(recv_data->mac_addr) + sizeof(uint8_t), portTICK_PERIOD_MS) != ESP_OK)
{
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
}
else
{
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
}
break;
default:
break;
}
}
vTaskDelete(NULL);
}
uint8_t zh_espnow_get_version(void)
{
uint32_t version = 0;
esp_now_get_version(&version);
return version;
}