Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to set the WiFi radio channel #9405

Merged
merged 8 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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