Skip to content

Commit b204c36

Browse files
committed
Add method to set the WiFi radio channel
1 parent f9bb62a commit b204c36

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

libraries/WiFi/src/WiFiGeneric.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,27 @@ int32_t WiFiGenericClass::channel(void)
12691269
return primaryChan;
12701270
}
12711271

1272+
/**
1273+
* Set the WiFi channel configuration
1274+
* @param primaryChan primary channel. Depending on the region, not all channels may be available.
1275+
* @param secondChan secondary channel (WIFI_SECOND_CHAN_NONE, WIFI_SECOND_CHAN_ABOVE, WIFI_SECOND_CHAN_BELOW)
1276+
*/
1277+
void WiFiGenericClass::channel(uint8_t primary, wifi_second_chan_t secondary)
1278+
{
1279+
wifi_country_t country;
1280+
esp_wifi_get_country(&country);
1281+
1282+
uint8_t min_chan = country.schan;
1283+
uint8_t max_chan = min_chan + country.nchan - 1;
1284+
1285+
if(primary < min_chan || primary > max_chan){
1286+
log_e("Invalid primary channel: %d", primary);
1287+
return;
1288+
}
1289+
1290+
esp_wifi_set_channel(primary, secondary);
1291+
}
1292+
12721293
/**
12731294
* store WiFi config in SDK flash area
12741295
* @param persistent

libraries/WiFi/src/WiFiGeneric.h

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class WiFiGenericClass
183183
static int waitStatusBits(int bits, uint32_t timeout_ms);
184184

185185
int32_t channel(void);
186+
void channel(uint8_t primary, wifi_second_chan_t secondary=WIFI_SECOND_CHAN_NONE);
186187

187188
void persistent(bool persistent);
188189
void enableLongRange(bool enable);

0 commit comments

Comments
 (0)