Skip to content

Commit 1fd238f

Browse files
committed
feat(wifi): Add support for 2.4GHz and 5GHz band switching
1 parent b759424 commit 1fd238f

File tree

3 files changed

+121
-12
lines changed

3 files changed

+121
-12
lines changed

libraries/WiFi/examples/WiFiScan/WiFiScan.ino

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
/*
2-
* This sketch demonstrates how to scan WiFi networks.
2+
* This sketch demonstrates how to scan WiFi networks. For chips that support 5GHz band, separate scans are done for all bands.
33
* The API is based on the Arduino WiFi Shield library, but has significant changes as newer WiFi functions are supported.
44
* E.g. the return value of `encryptionType()` different because more modern encryption is supported.
55
*/
66
#include "WiFi.h"
77

88
void setup() {
99
Serial.begin(115200);
10-
11-
// Set WiFi to station mode and disconnect from an AP if it was previously connected.
12-
WiFi.mode(WIFI_STA);
13-
WiFi.disconnect();
14-
delay(100);
15-
10+
// Enable Station Interface
11+
WiFi.STA.begin();
1612
Serial.println("Setup done");
1713
}
1814

19-
void loop() {
15+
void ScanWiFi() {
2016
Serial.println("Scan start");
21-
2217
// WiFi.scanNetworks will return the number of networks found.
2318
int n = WiFi.scanNetworks();
2419
Serial.println("Scan done");
@@ -54,11 +49,33 @@ void loop() {
5449
delay(10);
5550
}
5651
}
57-
Serial.println("");
5852

5953
// Delete the scan result to free memory for code below.
6054
WiFi.scanDelete();
61-
55+
Serial.println("-------------------------------------");
56+
}
57+
void loop() {
58+
Serial.println("-------------------------------------");
59+
Serial.println("Default wifi band mode scan:");
60+
Serial.println("-------------------------------------");
61+
WiFi.setBandMode(WIFI_BAND_MODE_AUTO);
62+
ScanWiFi();
63+
#if CONFIG_SOC_WIFI_SUPPORT_5G
64+
// Wait a bit before scanning again.
65+
delay(1000);
66+
Serial.println("-------------------------------------");
67+
Serial.println("2.4 Ghz wifi band mode scan:");
68+
Serial.println("-------------------------------------");
69+
WiFi.setBandMode(WIFI_BAND_MODE_2G_ONLY);
70+
ScanWiFi();
71+
// Wait a bit before scanning again.
72+
delay(1000);
73+
Serial.println("-------------------------------------");
74+
Serial.println("5 Ghz wifi band mode scan:");
75+
Serial.println("-------------------------------------");
76+
WiFi.setBandMode(WIFI_BAND_MODE_5G_ONLY);
77+
ScanWiFi();
78+
#endif
6279
// Wait a bit before scanning again.
63-
delay(5000);
80+
delay(10000);
6481
}

libraries/WiFi/src/WiFiGeneric.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ static bool espWiFiStart() {
378378
log_e("esp_wifi_start %d", err);
379379
return _esp_wifi_started;
380380
}
381+
#if SOC_WIFI_SUPPORT_5G
382+
log_v("Setting Band Mode to AUTO");
383+
esp_wifi_set_band_mode(WIFI_BAND_MODE_AUTO);
384+
#endif
381385
return _esp_wifi_started;
382386
}
383387

@@ -701,6 +705,90 @@ wifi_ps_type_t WiFiGenericClass::getSleep() {
701705
return _sleepEnabled;
702706
}
703707

708+
/**
709+
* control wifi band mode
710+
* @param band_mode enum possible band modes
711+
* @return ok
712+
*/
713+
bool WiFiGenericClass::setBandMode(wifi_band_mode_t band_mode) {
714+
#if SOC_WIFI_SUPPORT_5G
715+
if (!WiFi.STA.started() && !WiFi.AP.started()) {
716+
log_e("You need to start WiFi first");
717+
return false;
718+
}
719+
wifi_band_mode_t bm = WIFI_BAND_MODE_AUTO;
720+
esp_err_t err = esp_wifi_get_band_mode(&bm);
721+
if (err != ESP_OK) {
722+
log_e("Failed to get Current Band Mode: 0x%x: %s", err, esp_err_to_name(err));
723+
return false;
724+
} else if (bm == band_mode) {
725+
log_d("No change in Band Mode");
726+
return true;
727+
} else {
728+
log_d("Switching Band Mode from %d to %d", bm, band_mode);
729+
}
730+
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
731+
if (WiFi.STA.connected() || WiFi.AP.connected()) {
732+
log_e("Your network will get disconnected!");
733+
}
734+
#endif
735+
err = esp_wifi_set_band_mode(band_mode);
736+
if (err != ESP_OK) {
737+
log_e("Failed to set Band Mode: 0x%x: %s", err, esp_err_to_name(err));
738+
return false;
739+
}
740+
delay(100);
741+
return true;
742+
#else
743+
if (band_mode == WIFI_BAND_MODE_5G_ONLY){
744+
log_e("This chip supports only 2.4GHz WiFi");
745+
}
746+
return band_mode != WIFI_BAND_MODE_5G_ONLY;
747+
#endif
748+
}
749+
750+
/**
751+
* get the current enabled wifi band mode
752+
* @return enum band mode
753+
*/
754+
wifi_band_mode_t WiFiGenericClass::getBandMode() {
755+
#if SOC_WIFI_SUPPORT_5G
756+
wifi_band_mode_t band_mode = WIFI_BAND_MODE_AUTO;
757+
if (!WiFi.STA.started() && !WiFi.AP.started()) {
758+
log_e("You need to start WiFi first");
759+
return band_mode;
760+
}
761+
esp_err_t err = esp_wifi_get_band_mode(&band_mode);
762+
if (err != ESP_OK) {
763+
log_e("Failed to get Band Mode: 0x%x: %s", err, esp_err_to_name(err));
764+
}
765+
return band_mode;
766+
#else
767+
return WIFI_BAND_MODE_2G_ONLY;
768+
#endif
769+
}
770+
771+
/**
772+
* get the current active wifi band
773+
* @return enum band
774+
*/
775+
wifi_band_t WiFiGenericClass::getBand() {
776+
#if SOC_WIFI_SUPPORT_5G
777+
wifi_band_t band = WIFI_BAND_2G;
778+
if (!WiFi.STA.started() && !WiFi.AP.started()) {
779+
log_e("You need to start WiFi first");
780+
return band;
781+
}
782+
esp_err_t err = esp_wifi_get_band(&band);
783+
if (err != ESP_OK) {
784+
log_e("Failed to get Band: 0x%x: %s", err, esp_err_to_name(err));
785+
}
786+
return band;
787+
#else
788+
return WIFI_BAND_2G;
789+
#endif
790+
}
791+
704792
/**
705793
* control wifi tx power
706794
* @param power enum maximum wifi tx power

libraries/WiFi/src/WiFiGeneric.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class WiFiGenericClass {
111111
bool setTxPower(wifi_power_t power);
112112
wifi_power_t getTxPower();
113113

114+
bool setBandMode(wifi_band_mode_t band_mode);
115+
wifi_band_mode_t getBandMode();
116+
wifi_band_t getBand();
117+
114118
bool initiateFTM(uint8_t frm_count = 16, uint16_t burst_period = 2, uint8_t channel = 1, const uint8_t *mac = NULL);
115119

116120
static bool setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2, wifi_rx_ant_t rx_mode, wifi_tx_ant_t tx_mode);

0 commit comments

Comments
 (0)