From 10a5837c8fffd027d2d6a0709644f69a0bc85be1 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 9 Jun 2024 09:26:55 +0300 Subject: [PATCH] Version 1.0.3 Fixed bug with ESP-NOW channel setting when device is connected to router. --- version.txt | 2 +- zh_espnow.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/version.txt b/version.txt index e6d5cb8..e4c0d46 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.2 \ No newline at end of file +1.0.3 \ No newline at end of file diff --git a/zh_espnow.c b/zh_espnow.c index 0220c0b..cab7eba 100644 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -62,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)