Added WiFi channel selection
This commit is contained in:
@ -21,8 +21,9 @@
|
|||||||
## Attention
|
## 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.
|
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.
|
2. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW.
|
||||||
3. The ZHNetwork and the zh_network are incompatible.
|
3. All devices on the network must have the same WiFi channel.
|
||||||
|
4. The ZHNetwork and the zh_network are incompatible.
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
|
@ -51,7 +51,8 @@
|
|||||||
.max_waiting_time = 1000, \
|
.max_waiting_time = 1000, \
|
||||||
.id_vector_size = 100, \
|
.id_vector_size = 100, \
|
||||||
.route_vector_size = 100, \
|
.route_vector_size = 100, \
|
||||||
.wifi_interface = WIFI_IF_STA \
|
.wifi_interface = WIFI_IF_STA, \
|
||||||
|
.wifi_channel = 1 \
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#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 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%.
|
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.
|
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;
|
} zh_network_init_config_t;
|
||||||
|
|
||||||
/// \cond
|
/// \cond
|
||||||
@ -140,7 +142,7 @@ extern "C"
|
|||||||
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
|
* - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized
|
||||||
* - ESP_FAIL if any internal error
|
* - 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.
|
* @brief Deinitialize ESP-NOW interface.
|
||||||
|
11
zh_network.c
11
zh_network.c
@ -80,7 +80,7 @@ typedef struct
|
|||||||
ESP_EVENT_DEFINE_BASE(ZH_NETWORK);
|
ESP_EVENT_DEFINE_BASE(ZH_NETWORK);
|
||||||
/// \endcond
|
/// \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.");
|
ESP_LOGI(TAG, "ESP-NOW initialization begin.");
|
||||||
if (config == NULL)
|
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.");
|
ESP_LOGE(TAG, "ESP-NOW initialization fail. Invalid argument.");
|
||||||
return ESP_ERR_INVALID_ARG;
|
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.");
|
ESP_LOGE(TAG, "ESP-NOW initialization fail. WiFi not initialized.");
|
||||||
return ESP_ERR_WIFI_NOT_INIT;
|
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);
|
esp_read_mac(_self_mac, ESP_MAC_WIFI_SOFTAP);
|
||||||
}
|
}
|
||||||
_init_config = *config;
|
|
||||||
_send_cb_status_event_group_handle = xEventGroupCreate();
|
_send_cb_status_event_group_handle = xEventGroupCreate();
|
||||||
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(queue_t));
|
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(queue_t));
|
||||||
zh_vector_init(&_id_vector, sizeof(uint32_t), false);
|
zh_vector_init(&_id_vector, sizeof(uint32_t), false);
|
||||||
|
Reference in New Issue
Block a user