Full WiFi scan and apply BSSID if used (#5351)

* use extended wifi scan (fix Arduino bug for ESP32 family), apply BSSID
* if BSSID changed: force reconnect, add comment to BSSID check
This commit is contained in:
Damian Schneider
2026-02-08 09:26:36 +01:00
committed by GitHub
parent e9ced6ddf1
commit 80e75139c6
2 changed files with 26 additions and 2 deletions

View File

@@ -36,6 +36,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
if (n >= multiWiFi.size()) multiWiFi.emplace_back(); // expand vector by one
char oldSSID[33]; strcpy(oldSSID, multiWiFi[n].clientSSID);
char oldPass[65]; strcpy(oldPass, multiWiFi[n].clientPass);
uint8_t oldBSSID[6]; memcpy(oldBSSID, multiWiFi[n].bssid, 6); // save old BSSID
strlcpy(multiWiFi[n].clientSSID, request->arg(cs).c_str(), 33);
if (strlen(oldSSID) == 0 || strncmp(multiWiFi[n].clientSSID, oldSSID, 32) != 0) {
@@ -46,6 +47,9 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
forceReconnect = true;
}
fillStr2MAC(multiWiFi[n].bssid, request->arg(bs).c_str());
if (memcmp(oldBSSID, multiWiFi[n].bssid, 6) != 0) { // check if BSSID changed
forceReconnect = true;
}
for (size_t i = 0; i < 4; i++) {
ip[3] = 48+i;
gw[3] = 48+i;

View File

@@ -503,6 +503,10 @@ void WLED::setup()
if (strcmp(multiWiFi[0].clientSSID, DEFAULT_CLIENT_SSID) == 0 && !configBackupExists())
showWelcomePage = true;
#ifndef ESP8266
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
#endif
WiFi.persistent(false);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA); // enable scanning
@@ -727,7 +731,15 @@ void WLED::initConnection()
wifi_station_clear_enterprise_username();
wifi_station_clear_enterprise_password();
#endif
WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass);
uint8_t *bssid = nullptr;
// check if user BSSID is non zero for current WiFi config
for (int i = 0; i < sizeof(multiWiFi[selectedWiFi].bssid); i++) {
if (multiWiFi[selectedWiFi].bssid[i] != 0) {
bssid = multiWiFi[selectedWiFi].bssid; // BSSID set, assign pointer and continue
break;
}
}
WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass, 0, bssid); // no harm if called multiple times
} else { // WIFI_ENCRYPTION_TYPE_ENTERPRISE
DEBUG_PRINTF_P(PSTR("Using WPA2_AUTH_PEAP (Anon: %s, Ident: %s)\n"), multiWiFi[selectedWiFi].enterpriseAnonIdentity, multiWiFi[selectedWiFi].enterpriseIdentity);
#ifdef ESP8266
@@ -746,7 +758,15 @@ void WLED::initConnection()
#endif
}
#else // WLED_ENABLE_WPA_ENTERPRISE
WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass); // no harm if called multiple times
uint8_t *bssid = nullptr;
// check if user BSSID is non zero for current WiFi config
for (int i = 0; i < sizeof(multiWiFi[selectedWiFi].bssid); i++) {
if (multiWiFi[selectedWiFi].bssid[i] != 0) {
bssid = multiWiFi[selectedWiFi].bssid; // BSSID set, assign pointer and continue
break;
}
}
WiFi.begin(multiWiFi[selectedWiFi].clientSSID, multiWiFi[selectedWiFi].clientPass, 0, bssid); // no harm if called multiple times
#endif // WLED_ENABLE_WPA_ENTERPRISE
#ifdef ARDUINO_ARCH_ESP32