From 98efc65a52e23ab113e82b1c7f39d58c4210e4df Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Wed, 5 Jun 2024 17:53:34 +0300 Subject: [PATCH] Version 1.0.2 Added multiple attempts to send a message. --- include/zh_espnow.h | 4 +++- version.txt | 2 +- zh_espnow.c | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/zh_espnow.h b/include/zh_espnow.h index 4115b5f..1603e72 100644 --- a/include/zh_espnow.h +++ b/include/zh_espnow.h @@ -31,7 +31,8 @@ .stack_size = 2048, \ .queue_size = 32, \ .wifi_interface = WIFI_IF_STA, \ - .wifi_channel = 1 \ + .wifi_channel = 1, \ + .attempts = 3 \ } #ifdef __cplusplus @@ -53,6 +54,7 @@ extern "C" 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. } zh_espnow_init_config_t; /// \cond diff --git a/version.txt b/version.txt index 7f20734..e6d5cb8 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.1 \ No newline at end of file +1.0.2 \ No newline at end of file diff --git a/zh_espnow.c b/zh_espnow.c index 24d7d47..0220c0b 100644 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -27,6 +27,7 @@ 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; /// \cond typedef struct @@ -277,6 +278,8 @@ static void _processing(void *pvParameter) } memset(on_send, 0, sizeof(zh_espnow_event_on_send_t)); memcpy(on_send->mac_addr, queue.data.mac_addr, 6); + SEND: + ++_attempts; err = esp_now_send(queue.data.mac_addr, queue.data.payload, queue.data.payload_len); if (err == ESP_ERR_ESPNOW_NO_MEM) { @@ -305,11 +308,17 @@ static void _processing(void *pvParameter) { 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)