diff --git a/README.md b/README.md index 9a4b4ac..8c9a617 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ void app_main(void) send_message.float_value = 1.234; send_message.bool_value = false; printf("Used ESP-NOW version %d.\n", zh_espnow_get_version()); + printf("ESP-NOW channel %d. \n", zh_espnow_get_channel()); uint8_t counter = 0; for (;;) { diff --git a/include/zh_espnow.h b/include/zh_espnow.h index affbee9..58699de 100644 --- a/include/zh_espnow.h +++ b/include/zh_espnow.h @@ -144,6 +144,22 @@ extern "C" */ esp_err_t zh_espnow_set_attempts(uint8_t attempts); + /** + * @brief Get ESP-NOW channel. + * + * @return ESP-NOW channel if success or 0 otherwise. + */ + uint8_t zh_espnow_get_channel(void); + + /** + * @brief Set ESP-NOW channel. + * + * @param[in] channel ESP-NOW channel (1-14). + * + * @return ESP_OK if success or an error code otherwise. + */ + esp_err_t zh_espnow_set_channel(uint8_t channel); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/version.txt b/version.txt index e21e727..3e1ad72 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.4.0 \ No newline at end of file +1.5.0 \ No newline at end of file diff --git a/zh_espnow.c b/zh_espnow.c index 4d7fb1c..5e5fc16 100755 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -535,4 +535,39 @@ esp_err_t zh_espnow_set_attempts(uint8_t attempts) _init_config.attempts = attempts; ZH_ESPNOW_LOGI("Number of attempts set successfully."); return ESP_OK; +} + +uint8_t zh_espnow_get_channel(void) +{ + if (_is_initialized == false) + { + ZH_ESPNOW_LOGE("ESP-NOW channel receiption failed. ESP-NOW is not initialized."); + return 0; + } + uint8_t prim_channel = 0; + wifi_second_chan_t sec_channel = 0; + esp_err_t err = esp_wifi_get_channel(&prim_channel, &sec_channel); + if (err != ESP_OK) + { + ZH_ESPNOW_LOGE_ERR("ESP-NOW channel receiption failed.", err); + return 0; + } + _init_config.wifi_channel = prim_channel; + ZH_ESPNOW_LOGI("ESP-NOW channel receiption successfully."); + return prim_channel; +} + +esp_err_t zh_espnow_set_channel(uint8_t channel) +{ + ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "ESP-NOW channel set failed. ESP-NOW is not initialized."); + ZH_ESPNOW_CHECK(channel > 0 && channel < 15, ESP_ERR_INVALID_ARG, "ESP-NOW channel set failed. Invalid channel."); + esp_err_t err = esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); + if (err != ESP_OK) + { + ZH_ESPNOW_LOGE_ERR("ESP-NOW channel set failed.", err); + return err; + } + _init_config.wifi_channel = channel; + ZH_ESPNOW_LOGI("ESP-NOW channel set successfully."); + return err; } \ No newline at end of file