From b9c2158b73d678dc037c06dfb6878784090f8579 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sat, 1 Jun 2024 07:46:22 +0300 Subject: [PATCH] Added WiFi channel selection --- README.md | 5 +++-- include/zh_network.h | 6 ++++-- zh_network.c | 11 ++++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ef5ebee..2636d2f 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,9 @@ ## Attention 1. The definition of ZH_NETWORK_MAX_MESSAGE_SIZE in the zh_network.h can be changed between 1 and 218. Smaller size - higher transmission speed. All devices on the network must have the same ZH_NETWORK_MAX_MESSAGE_SIZE. -2. For correct work at ESP-NOW + STA mode your WiFi router must be set on channel 1. -3. The ZHNetwork and the zh_network are incompatible. +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. The ZHNetwork and the zh_network are incompatible. ## Testing diff --git a/include/zh_network.h b/include/zh_network.h index 55c07ee..f85c86c 100644 --- a/include/zh_network.h +++ b/include/zh_network.h @@ -51,7 +51,8 @@ .max_waiting_time = 1000, \ .id_vector_size = 100, \ .route_vector_size = 100, \ - .wifi_interface = WIFI_IF_STA \ + .wifi_interface = WIFI_IF_STA, \ + .wifi_channel = 1 \ } #ifdef __cplusplus @@ -76,6 +77,7 @@ extern "C" uint16_t id_vector_size; ///< Maximum size of unique ID of received messages. @note If the size is exceeded, the first value will be deleted. Minimum recommended value: number of planned nodes in the network + 10%. uint16_t route_vector_size; ///< The maximum size of the routing table. @note If the size is exceeded, the first route will be deleted. Minimum recommended value: number of planned nodes in the network + 10%. 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 ESPNOW data. @note Values from 1 to 14. } zh_network_init_config_t; /// \cond @@ -140,7 +142,7 @@ extern "C" * - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized * - ESP_FAIL if any internal error */ - esp_err_t zh_network_init(zh_network_init_config_t *config); + esp_err_t zh_network_init(const zh_network_init_config_t *config); /** * @brief Deinitialize ESP-NOW interface. diff --git a/zh_network.c b/zh_network.c index 3d16451..5d7a3fb 100644 --- a/zh_network.c +++ b/zh_network.c @@ -80,7 +80,7 @@ typedef struct ESP_EVENT_DEFINE_BASE(ZH_NETWORK); /// \endcond -esp_err_t zh_network_init(zh_network_init_config_t *config) +esp_err_t zh_network_init(const zh_network_init_config_t *config) { ESP_LOGI(TAG, "ESP-NOW initialization begin."); if (config == NULL) @@ -88,7 +88,13 @@ esp_err_t zh_network_init(zh_network_init_config_t *config) ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument."); return ESP_ERR_INVALID_ARG; } - if (esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE) == ESP_ERR_WIFI_NOT_INIT) + _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; + } + if (esp_wifi_set_channel(_init_config.wifi_channel, WIFI_SECOND_CHAN_NONE) == ESP_ERR_WIFI_NOT_INIT) { ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized."); return ESP_ERR_WIFI_NOT_INIT; @@ -106,7 +112,6 @@ esp_err_t zh_network_init(zh_network_init_config_t *config) { esp_read_mac(_self_mac, ESP_MAC_WIFI_SOFTAP); } - _init_config = *config; _send_cb_status_event_group_handle = xEventGroupCreate(); _queue_handle = xQueueCreate(_init_config.queue_size, sizeof(queue_t)); zh_vector_init(&_id_vector, sizeof(uint32_t), false);