diff --git a/components/zh_espnow/CMakeLists.txt b/components/zh_espnow/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/components/zh_espnow/LICENSE b/components/zh_espnow/LICENSE old mode 100755 new mode 100644 diff --git a/components/zh_espnow/README.md b/components/zh_espnow/README.md old mode 100755 new mode 100644 diff --git a/components/zh_espnow/component.mk b/components/zh_espnow/component.mk old mode 100755 new mode 100644 diff --git a/components/zh_espnow/include/zh_espnow.h b/components/zh_espnow/include/zh_espnow.h old mode 100755 new mode 100644 index 4115b5f..1603e72 --- a/components/zh_espnow/include/zh_espnow.h +++ b/components/zh_espnow/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/components/zh_espnow/version.txt b/components/zh_espnow/version.txt old mode 100755 new mode 100644 index 7f20734..e4c0d46 --- a/components/zh_espnow/version.txt +++ b/components/zh_espnow/version.txt @@ -1 +1 @@ -1.0.1 \ No newline at end of file +1.0.3 \ No newline at end of file diff --git a/components/zh_espnow/zh_espnow.c b/components/zh_espnow/zh_espnow.c old mode 100755 new mode 100644 index 24d7d47..cab7eba --- a/components/zh_espnow/zh_espnow.c +++ b/components/zh_espnow/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 @@ -61,11 +62,22 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config) ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi channel."); return ESP_ERR_INVALID_ARG; } - if (esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) + 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 = 0; + 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); + } + } _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) @@ -277,6 +289,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 +319,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)