Skip to content

feat(wifi): Add support for 2.4GHz and 5GHz band switching #11045

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

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 29 additions & 12 deletions libraries/WiFi/examples/WiFiScan/WiFiScan.ino
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
/*
* This sketch demonstrates how to scan WiFi networks.
* This sketch demonstrates how to scan WiFi networks. For chips that support 5GHz band, separate scans are done for all bands.
* The API is based on the Arduino WiFi Shield library, but has significant changes as newer WiFi functions are supported.
* E.g. the return value of `encryptionType()` different because more modern encryption is supported.
*/
#include "WiFi.h"

void setup() {
Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected.
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

// Enable Station Interface
WiFi.STA.begin();
Serial.println("Setup done");
}

void loop() {
void ScanWiFi() {
Serial.println("Scan start");

// WiFi.scanNetworks will return the number of networks found.
int n = WiFi.scanNetworks();
Serial.println("Scan done");
Expand Down Expand Up @@ -54,11 +49,33 @@ void loop() {
delay(10);
}
}
Serial.println("");

// Delete the scan result to free memory for code below.
WiFi.scanDelete();

Serial.println("-------------------------------------");
}
void loop() {
Serial.println("-------------------------------------");
Serial.println("Default wifi band mode scan:");
Serial.println("-------------------------------------");
WiFi.setBandMode(WIFI_BAND_MODE_AUTO);
ScanWiFi();
#if CONFIG_SOC_WIFI_SUPPORT_5G
// Wait a bit before scanning again.
delay(1000);
Serial.println("-------------------------------------");
Serial.println("2.4 Ghz wifi band mode scan:");
Serial.println("-------------------------------------");
WiFi.setBandMode(WIFI_BAND_MODE_2G_ONLY);
ScanWiFi();
// Wait a bit before scanning again.
delay(1000);
Serial.println("-------------------------------------");
Serial.println("5 Ghz wifi band mode scan:");
Serial.println("-------------------------------------");
WiFi.setBandMode(WIFI_BAND_MODE_5G_ONLY);
ScanWiFi();
#endif
// Wait a bit before scanning again.
delay(5000);
delay(10000);
}
88 changes: 88 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ static bool espWiFiStart() {
log_e("esp_wifi_start %d", err);
return _esp_wifi_started;
}
#if SOC_WIFI_SUPPORT_5G
log_v("Setting Band Mode to AUTO");
esp_wifi_set_band_mode(WIFI_BAND_MODE_AUTO);
#endif
return _esp_wifi_started;
}

Expand Down Expand Up @@ -701,6 +705,90 @@ wifi_ps_type_t WiFiGenericClass::getSleep() {
return _sleepEnabled;
}

/**
* control wifi band mode
* @param band_mode enum possible band modes
* @return ok
*/
bool WiFiGenericClass::setBandMode(wifi_band_mode_t band_mode) {
#if SOC_WIFI_SUPPORT_5G
if (!WiFi.STA.started() && !WiFi.AP.started()) {
log_e("You need to start WiFi first");
return false;
}
wifi_band_mode_t bm = WIFI_BAND_MODE_AUTO;
esp_err_t err = esp_wifi_get_band_mode(&bm);
if (err != ESP_OK) {
log_e("Failed to get Current Band Mode: 0x%x: %s", err, esp_err_to_name(err));
return false;
} else if (bm == band_mode) {
log_d("No change in Band Mode");
return true;
} else {
log_d("Switching Band Mode from %d to %d", bm, band_mode);
}
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
if (WiFi.STA.connected() || WiFi.AP.connected()) {
log_e("Your network will get disconnected!");
}
#endif
err = esp_wifi_set_band_mode(band_mode);
if (err != ESP_OK) {
log_e("Failed to set Band Mode: 0x%x: %s", err, esp_err_to_name(err));
return false;
}
delay(100);
return true;
#else
if (band_mode == WIFI_BAND_MODE_5G_ONLY){
log_e("This chip supports only 2.4GHz WiFi");
}
return band_mode != WIFI_BAND_MODE_5G_ONLY;
#endif
}

/**
* get the current enabled wifi band mode
* @return enum band mode
*/
wifi_band_mode_t WiFiGenericClass::getBandMode() {
#if SOC_WIFI_SUPPORT_5G
wifi_band_mode_t band_mode = WIFI_BAND_MODE_AUTO;
if (!WiFi.STA.started() && !WiFi.AP.started()) {
log_e("You need to start WiFi first");
return band_mode;
}
esp_err_t err = esp_wifi_get_band_mode(&band_mode);
if (err != ESP_OK) {
log_e("Failed to get Band Mode: 0x%x: %s", err, esp_err_to_name(err));
}
return band_mode;
#else
return WIFI_BAND_MODE_2G_ONLY;
#endif
}

/**
* get the current active wifi band
* @return enum band
*/
wifi_band_t WiFiGenericClass::getBand() {
#if SOC_WIFI_SUPPORT_5G
wifi_band_t band = WIFI_BAND_2G;
if (!WiFi.STA.started() && !WiFi.AP.started()) {
log_e("You need to start WiFi first");
return band;
}
esp_err_t err = esp_wifi_get_band(&band);
if (err != ESP_OK) {
log_e("Failed to get Band: 0x%x: %s", err, esp_err_to_name(err));
}
return band;
#else
return WIFI_BAND_2G;
#endif
}

/**
* control wifi tx power
* @param power enum maximum wifi tx power
Expand Down
4 changes: 4 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class WiFiGenericClass {
bool setTxPower(wifi_power_t power);
wifi_power_t getTxPower();

bool setBandMode(wifi_band_mode_t band_mode);
wifi_band_mode_t getBandMode();
wifi_band_t getBand();

bool initiateFTM(uint8_t frm_count = 16, uint16_t burst_period = 2, uint8_t channel = 1, const uint8_t *mac = NULL);

static bool setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode);
Expand Down
Loading