Added WiFi channel selection

This commit is contained in:
2024-06-01 07:46:22 +03:00
parent 906f1e2725
commit b9c2158b73
3 changed files with 15 additions and 7 deletions

View File

@ -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

View File

@ -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.

View File

@ -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);