Skip to content

Commit

Permalink
Add method to set the WiFi radio channel (#9405)
Browse files Browse the repository at this point in the history
* Add method to set the WiFi radio channel

* Fix Tab

* Add check

* Change name

* Fix description

* Add check

* Add error return

* Improve error message
  • Loading branch information
lucasssvaz authored Mar 26, 2024
1 parent 75f7b33 commit 9c0d59f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,40 @@ int32_t WiFiGenericClass::channel(void)
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
* @param persistent
Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class WiFiGenericClass
static int waitStatusBits(int bits, uint32_t timeout_ms);

int32_t channel(void);
int setChannel(uint8_t primary, wifi_second_chan_t secondary=WIFI_SECOND_CHAN_NONE);

void persistent(bool persistent);
void enableLongRange(bool enable);
Expand Down

0 comments on commit 9c0d59f

Please sign in to comment.