Compare commits

..

No commits in common. "main" and "v1.0.1" have entirely different histories.
main ... v1.0.1

4 changed files with 147 additions and 147 deletions

View File

@ -3,24 +3,23 @@
## Tested on
1. ESP8266 RTOS_SDK v3.4
2. ESP32 ESP-IDF v5.4
2. ESP32 ESP-IDF v5.2
## Features
1. The maximum size of the transmitted data is up to 250 / 1490 bytes. Please see attention for details.
1. The maximum size of transmitted data is up to 250 bytes.
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.
6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW.
## 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.
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.
## [Function description](http://zh-espnow.zh.com.ru)
## Using
@ -55,7 +54,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] = {0xEC, 0x94, 0xCB, 0x87, 0xEC, 0xFC};
uint8_t target[6] = {0x34, 0x94, 0x54, 0x24, 0xA3, 0x41};
typedef struct
{
@ -75,8 +74,8 @@ void app_main(void)
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);
zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
zh_espnow_init(&zh_espnow_init_config);
#ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, NULL);
#else
@ -86,7 +85,6 @@ 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

@ -1,3 +1,9 @@
/**
* @file
* Header file for the zh_espnow component.
*
*/
#pragma once
#include "string.h"
@ -9,57 +15,91 @@
#include "esp_log.h"
#include "esp_heap_caps.h"
/**
* @brief Unique identifier of ESP-NOW interface events base. Used when registering the event handler.
*
*/
#define ESP_EVENT_BASE ZH_ESPNOW
/**
* @brief Default values for zh_espnow_init_config_t structure for initial initialization of ESP-NOW interface.
*
*/
#define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \
{ \
.task_priority = 10, \
.stack_size = 3072, \
.queue_size = 64, \
.task_priority = 4, \
.stack_size = 2048, \
.queue_size = 32, \
.wifi_interface = WIFI_IF_STA, \
.wifi_channel = 1, \
.attempts = 3, \
.battery_mode = false}
.wifi_channel = 1 \
}
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct // Structure for initial initialization of ESP-NOW interface.
/**
* @brief Structure for initial initialization of 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
*/
typedef struct
{
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.
uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4.
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.
} zh_espnow_init_config_t;
ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
/// \cond
ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE);
/// \endcond
typedef enum // Enumeration of possible ESP-NOW events.
/**
* @brief Enumeration of possible ESP-NOW events.
*
*/
typedef enum
{
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_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.
/**
* @brief Enumeration of possible status of sent ESP-NOW message.
*
*/
typedef enum
{
ZH_ESPNOW_SEND_SUCCESS, // If ESP-NOW message was sent success.
ZH_ESPNOW_SEND_FAIL // If ESP-NOW message was sent fail.
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.
/**
* @brief 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.
*/
typedef struct
{
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.
uint8_t mac_addr[6]; ///< MAC address of the device to which the ESP-NOW message was sent. @note
zh_espnow_on_send_event_type_t status; ///< Status of sent ESP-NOW message. @note
} 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.
/**
* @brief 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.
*/
typedef struct
{
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.
uint8_t mac_addr[6]; ///< MAC address of the sender ESP-NOW message. @note
uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note
uint8_t data_len; ///< Size of the received ESP-NOW message. @note
} zh_espnow_event_on_recv_t;
/**
@ -106,14 +146,6 @@ 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.3.1
1.0.1

166
zh_espnow.c Executable file → Normal file
View File

@ -1,8 +1,16 @@
/**
* @file
* The main code of the zh_espnow component.
*
*/
#include "zh_espnow.h"
/// \cond
#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]
/// \endcond
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
@ -19,13 +27,8 @@ 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
/// \cond
typedef struct
{
enum
@ -35,13 +38,14 @@ typedef struct
} id;
struct
{
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
uint8_t mac_addr[6];
uint8_t *payload;
uint16_t payload_len;
uint8_t payload_len;
} data;
} _queue_t;
ESP_EVENT_DEFINE_BASE(ZH_ESPNOW);
/// \endcond
esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
{
@ -54,51 +58,24 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
_init_config = *config;
if (_init_config.wifi_channel < 1 || _init_config.wifi_channel > 14)
{
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel incorrect.");
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel.");
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)
if (esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK)
{
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)
{
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;
}
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
return ESP_FAIL;
}
else
if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
{
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__);
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error.");
return ESP_FAIL;
}
_is_initialized = true;
@ -117,10 +94,7 @@ esp_err_t zh_espnow_deinit(void)
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_unregister_recv_cb();
esp_now_deinit();
vTaskDelete(_processing_task_handle);
_is_initialized = false;
@ -143,7 +117,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 > _max_message_size)
if (data == NULL || data_len == 0 || data_len > ESP_NOW_MAX_DATA_LEN)
{
ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument.");
return ESP_ERR_INVALID_ARG;
@ -153,30 +127,33 @@ 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[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t broadcast[6] = {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);
memcpy(queue.data.mac_addr, broadcast, 6);
}
else
{
memcpy(queue.data.mac_addr, target, ESP_NOW_ETH_ALEN);
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);
}
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.");
@ -185,10 +162,15 @@ 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 IRAM_ATTR _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
{
if (status == ESP_NOW_SEND_SUCCESS)
{
@ -201,9 +183,9 @@ static void IRAM_ATTR _send_cb(const uint8_t *mac_addr, esp_now_send_status_t st
}
#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)
static void _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)
static void _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
@ -219,31 +201,38 @@ static void IRAM_ATTR _recv_cb(const esp_now_recv_info_t *esp_now_info, const ui
_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);
memcpy(queue.data.mac_addr, mac_addr, 6);
#else
memcpy(queue.data.mac_addr, esp_now_info->src_addr, ESP_NOW_ETH_ALEN);
memcpy(queue.data.mac_addr, esp_now_info->src_addr, 6);
#endif
queue.data.payload = heap_caps_calloc(1, data_len, MALLOC_CAP_8BIT);
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);
}
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 IRAM_ATTR _processing(void *pvParameter)
static void _processing(void *pvParameter)
{
_queue_t queue = {0};
while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE)
@ -253,15 +242,16 @@ static void IRAM_ATTR _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_calloc(1, sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT);
esp_now_peer_info_t *peer = heap_caps_malloc(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, ESP_NOW_ETH_ALEN);
memcpy(peer->peer_addr, queue.data.mac_addr, 6);
err = esp_now_add_peer(peer);
if (err == ESP_ERR_ESPNOW_NO_MEM)
{
@ -277,7 +267,7 @@ static void IRAM_ATTR _processing(void *pvParameter)
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);
zh_espnow_event_on_send_t *on_send = heap_caps_malloc(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.");
@ -285,9 +275,8 @@ static void IRAM_ATTR _processing(void *pvParameter)
heap_caps_free(peer);
break;
}
memcpy(on_send->mac_addr, queue.data.mac_addr, ESP_NOW_ETH_ALEN);
SEND:
++_attempts;
memset(on_send, 0, sizeof(zh_espnow_event_on_send_t));
memcpy(on_send->mac_addr, queue.data.mac_addr, 6);
err = esp_now_send(queue.data.mac_addr, queue.data.payload, queue.data.payload_len);
if (err == ESP_ERR_ESPNOW_NO_MEM)
{
@ -300,7 +289,7 @@ static void IRAM_ATTR _processing(void *pvParameter)
}
else if (err != ESP_OK)
{
ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error at line %d.", __LINE__);
ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error.");
heap_caps_free(queue.data.payload);
heap_caps_free(peer);
heap_caps_free(on_send);
@ -314,28 +303,19 @@ static void IRAM_ATTR _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 after %d attempts.", MAC2STR(queue.data.mac_addr), _attempts);
ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.mac_addr));
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;
}
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);
@ -344,25 +324,15 @@ static void IRAM_ATTR _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;
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_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)
{
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;
}