feat: initial

This commit is contained in:
Alexey Zholtikov 2025-05-03 07:42:48 +03:00
parent 818da31b65
commit 1496d59aee
6 changed files with 623 additions and 3 deletions

View File

@ -1 +1,6 @@
idf_component_register(SRCS "main.c" INCLUDE_DIRS "include") if(${IDF_TARGET} STREQUAL esp8266)
set(requires)
else()
set(requires esp_wifi)
endif()
idf_component_register(SRCS "zh_espnow.c" INCLUDE_DIRS "include" REQUIRES ${requires})

131
README.md
View File

@ -1,3 +1,130 @@
# esp_component_template # ESP32 ESP-IDF and ESP8266 RTOS SDK component for ESP-NOW interface
esp_component_template ## Tested on
1. ESP8266 RTOS_SDK v3.4
2. ESP32 ESP-IDF v5.4
## Features
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. Please see attention for details.
## Attention
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
In an existing project, run the following command to install the component:
```text
cd ../your_project/components
git clone http://http://git.zh.com.ru/alexey.zholtikov/zh_espnow
```
In the application, add the component:
```c
#include "zh_espnow.h"
```
## Example
Sending and receiving messages:
```c
#include "nvs_flash.h"
#include "esp_netif.h"
#include "zh_espnow.h"
#ifdef CONFIG_IDF_TARGET_ESP8266
#include "esp_system.h"
#else
#include "esp_random.h"
#endif
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
uint8_t target[6] = {0xEC, 0x94, 0xCB, 0x87, 0xEC, 0xFC};
typedef struct
{
char char_value[30];
int int_value;
float float_value;
bool bool_value;
} example_message_t;
void app_main(void)
{
esp_log_level_set("zh_espnow", ESP_LOG_NONE);
nvs_flash_init();
esp_netif_init();
esp_event_loop_create_default();
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_config);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
zh_espnow_init(&espnow_init_config);
#ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, NULL);
#else
esp_event_handler_instance_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, NULL, NULL);
#endif
example_message_t send_message = {0};
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();
zh_espnow_send(NULL, (uint8_t *)&send_message, sizeof(send_message));
vTaskDelay(5000 / portTICK_PERIOD_MS);
zh_espnow_send(target, (uint8_t *)&send_message, sizeof(send_message));
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id)
{
case ZH_ESPNOW_ON_RECV_EVENT:;
zh_espnow_event_on_recv_t *recv_data = event_data;
printf("Message from MAC %02X:%02X:%02X:%02X:%02X:%02X is received. Data lenght %d bytes.\n", MAC2STR(recv_data->mac_addr), recv_data->data_len);
example_message_t *recv_message = (example_message_t *)recv_data->data;
printf("Char %s\n", recv_message->char_value);
printf("Int %d\n", recv_message->int_value);
printf("Float %f\n", recv_message->float_value);
printf("Bool %d\n", recv_message->bool_value);
heap_caps_free(recv_data->data); // Do not delete to avoid memory leaks!
break;
case ZH_ESPNOW_ON_SEND_EVENT:;
zh_espnow_event_on_send_t *send_data = event_data;
if (send_data->status == ZH_ESPNOW_SEND_SUCCESS)
{
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.\n", MAC2STR(send_data->mac_addr));
}
else
{
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.\n", MAC2STR(send_data->mac_addr));
}
default:
break;
}
}
```
Any [feedback](mailto:github@azholtikov.ru) will be gladly accepted.

View File

119
include/zh_espnow.h Normal file
View File

@ -0,0 +1,119 @@
#pragma once
#include "string.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_now.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \
{ \
.task_priority = 10, \
.stack_size = 3072, \
.queue_size = 64, \
.wifi_interface = WIFI_IF_STA, \
.wifi_channel = 1, \
.attempts = 3, \
.battery_mode = false}
#ifdef __cplusplus
extern "C"
{
#endif
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 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);
typedef enum // Enumeration of possible ESP-NOW events.
{
ZH_ESPNOW_ON_RECV_EVENT, // The event when the ESP-NOW message was received.
ZH_ESPNOW_ON_SEND_EVENT // The event when the ESP-NOW message was sent.
} zh_espnow_event_type_t;
typedef enum // Enumeration of possible status of sent ESP-NOW message.
{
ZH_ESPNOW_SEND_SUCCESS, // If ESP-NOW message was sent success.
ZH_ESPNOW_SEND_FAIL // If ESP-NOW message was sent fail.
} zh_espnow_on_send_event_type_t;
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[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[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;
/**
* @brief Initialize ESP-NOW interface.
*
* @note Before initialize ESP-NOW interface recommend initialize zh_espnow_init_config_t structure with default values.
*
* @code zh_espnow_init_config_t config = ZH_ESPNOW_INIT_CONFIG_DEFAULT() @endcode
*
* @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable.
*
* @return
* - ESP_OK if initialization was success
* - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
* - ESP_FAIL if any internal error
*/
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config);
/**
* @brief Deinitialize ESP-NOW interface.
*
* @return
* - ESP_OK if deinitialization was success
* - ESP_FAIL if ESP-NOW is not initialized
*/
esp_err_t zh_espnow_deinit(void);
/**
* @brief Send ESP-NOW data.
*
* @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast.
* @param[in] data Pointer to a buffer containing the data for send.
* @param[in] data_len Sending data length.
*
* @note The function will return an ESP_ERR_INVALID_STATE error if less than 10% of the size set at initialization remains in the message queue.
*
* @return
* - ESP_OK if sent was success
* - ESP_ERR_INVALID_ARG if parameter error
* - ESP_ERR_NO_MEM if memory allocation fail or no free memory in the heap
* - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full
* - ESP_FAIL if ESP-NOW is not initialized or any internal error
*/
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

@ -0,0 +1 @@
1.0.0

368
zh_espnow.c Executable file
View File

@ -0,0 +1,368 @@
#include "zh_espnow.h"
#define DATA_SEND_SUCCESS BIT0
#define DATA_SEND_FAIL BIT1
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
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);
#else
static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
#endif
static void _processing(void *pvParameter);
static const char *TAG = "zh_espnow";
static EventGroupHandle_t _event_group_handle = {0};
static QueueHandle_t _queue_handle = {0};
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
{
enum
{
ON_RECV,
TO_SEND,
} id;
struct
{
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
uint8_t *payload;
uint16_t payload_len;
} data;
} _queue_t;
ESP_EVENT_DEFINE_BASE(ZH_ESPNOW);
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
{
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
if (config == NULL)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
}
_init_config = *config;
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel incorrect.");
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE);
if (err == ESP_ERR_WIFI_NOT_INIT || err == ESP_ERR_WIFI_NOT_STARTED)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized.");
return ESP_ERR_WIFI_NOT_INIT;
}
else if (err == ESP_FAIL)
{
uint8_t prim = 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 (_init_config.battery_mode == false)
{
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;
}
}
else
{
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;
ESP_LOGI(TAG, "ESP-NOW initialization success.");
return ESP_OK;
}
esp_err_t zh_espnow_deinit(void)
{
ESP_LOGI(TAG, "ESP-NOW deinitialization begin.");
if (_is_initialized == false)
{
ESP_LOGE(TAG, "ESP-NOW deinitialization fail. ESP-NOW not initialized.");
return ESP_FAIL;
}
vEventGroupDelete(_event_group_handle);
vQueueDelete(_queue_handle);
esp_now_unregister_send_cb();
if (_init_config.battery_mode == false)
{
esp_now_unregister_recv_cb();
}
esp_now_deinit();
vTaskDelete(_processing_task_handle);
_is_initialized = false;
ESP_LOGI(TAG, "ESP-NOW deinitialization success.");
return ESP_OK;
}
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len)
{
if (target == NULL)
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue begin.");
}
else
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(target));
}
if (_is_initialized == false)
{
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 > _max_message_size)
{
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
}
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 10)
{
ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full.");
return ESP_ERR_INVALID_STATE;
}
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, ESP_NOW_ETH_ALEN);
}
else
{
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;
}
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.");
}
else
{
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
}
return ESP_OK;
}
static void IRAM_ATTR _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if (status == ESP_NOW_SEND_SUCCESS)
{
xEventGroupSetBits(_event_group_handle, DATA_SEND_SUCCESS);
}
else
{
xEventGroupSetBits(_event_group_handle, DATA_SEND_FAIL);
}
}
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
static void IRAM_ATTR _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
#else
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
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(mac_addr));
#else
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue begin.", MAC2STR(esp_now_info->src_addr));
#endif
if (uxQueueSpacesAvailable(_queue_handle) < _init_config.queue_size / 10)
{
ESP_LOGW(TAG, "Adding incoming ESP-NOW data to queue fail. Queue is almost full.");
return;
}
_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, ESP_NOW_ETH_ALEN);
#else
memcpy(queue.data.mac_addr, esp_now_info->src_addr, ESP_NOW_ETH_ALEN);
#endif
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;
}
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
}
static void IRAM_ATTR _processing(void *pvParameter)
{
_queue_t queue = {0};
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
{
esp_err_t err = ESP_OK;
switch (queue.id)
{
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_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;
}
peer->ifidx = _init_config.wifi_interface;
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)
{
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. No free memory in the heap for adding peer.");
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
break;
}
else if (err != ESP_OK)
{
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Internal error with adding peer.");
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
break;
}
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.");
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
break;
}
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);
if (err == ESP_ERR_ESPNOW_NO_MEM)
{
ESP_LOGE(TAG, "Sending ESP-NOW data fail. No free memory in the heap.");
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
heap_caps_free(on_send);
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail.");
break;
}
else if (err != ESP_OK)
{
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);
ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail.");
break;
}
else
{
ESP_LOGI(TAG, "Sending ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X success.", MAC2STR(queue.data.mac_addr));
}
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 after %d attempts.", MAC2STR(queue.data.mac_addr), _attempts);
on_send->status = ZH_ESPNOW_SEND_SUCCESS;
_attempts = 0;
}
else
{
if (_attempts < _init_config.attempts)
{
goto SEND;
}
ESP_LOGE(TAG, "Confirmation message not received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.", MAC2STR(queue.data.mac_addr));
on_send->status = ZH_ESPNOW_SEND_FAIL;
_attempts = 0;
}
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);
heap_caps_free(on_send);
break;
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;
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;
}