Add method to set the WiFi radio channel (#9405)

* Add method to set the WiFi radio channel

* Fix Tab

* Add check

* Change name

* Fix description

* Add check

* Add error return

* Improve error message
This commit is contained in:
Lucas Saavedra Vaz 2024-03-26 07:21:00 -03:00 committed by GitHub
parent 75f7b3303b
commit 9c0d59f9c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View file

@ -1269,6 +1269,40 @@ int32_t WiFiGenericClass::channel(void)
return primaryChan; return primaryChan;
} }
/**
* Set the WiFi channel configuration
* @param primary primary channel. Depending on the region, not all channels may be available.
* @param secondary secondary channel (WIFI_SECOND_CHAN_NONE, WIFI_SECOND_CHAN_ABOVE, WIFI_SECOND_CHAN_BELOW)
* @return 0 on success, otherwise error
*/
int WiFiGenericClass::setChannel(uint8_t primary, wifi_second_chan_t secondary)
{
wifi_country_t country;
esp_err_t ret;
ret = esp_wifi_get_country(&country);
if (ret != ESP_OK) {
log_e("Failed to get country info");
return ret;
}
uint8_t min_chan = country.schan;
uint8_t max_chan = min_chan + country.nchan - 1;
if(primary < min_chan || primary > max_chan){
log_e("Invalid primary channel: %d. Valid range is %d-%d for country %s", primary, min_chan, max_chan, country.cc);
return ESP_ERR_INVALID_ARG;
}
ret = esp_wifi_set_channel(primary, secondary);
if (ret != ESP_OK) {
log_e("Failed to set channel");
return ret;
}
return ESP_OK;
}
/** /**
* store WiFi config in SDK flash area * store WiFi config in SDK flash area
* @param persistent * @param persistent

View file

@ -183,6 +183,7 @@ class WiFiGenericClass
static int waitStatusBits(int bits, uint32_t timeout_ms); static int waitStatusBits(int bits, uint32_t timeout_ms);
int32_t channel(void); int32_t channel(void);
int setChannel(uint8_t primary, wifi_second_chan_t secondary=WIFI_SECOND_CHAN_NONE);
void persistent(bool persistent); void persistent(bool persistent);
void enableLongRange(bool enable); void enableLongRange(bool enable);