Skip to content

Commit

Permalink
Merge pull request esp8266#7629 from Erriez/feature/issue-2246-multi-…
Browse files Browse the repository at this point in the history
…wifi-hidden

Add support for hidden SSID's Multi WiFi
  • Loading branch information
devyte authored Jan 21, 2021
2 parents 85e2fff + d90015e commit 2402958
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions libraries/ESP8266WiFi/examples/WiFiMulti/WiFiMulti.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Registering multiple networks (at least 1)
- Connect to WiFi with strongest signal (RSSI)
- Fall back to connect to next WiFi when a connection failed or lost
- Fall back to connect to hidden SSID's which are not reported by WiFi scan
To enable debugging output, select in the Arduino iDE:
- Tools | Debug Port: Serial
Expand All @@ -21,6 +22,9 @@ ESP8266WiFiMulti wifiMulti;
const uint32_t connectTimeoutMs = 5000;

void setup() {
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);

Serial.begin(115200);
Serial.println("\nESP8266 Multi WiFi example");

Expand Down
30 changes: 28 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,18 @@ wl_status_t ESP8266WiFiMulti::connectWiFiMulti(uint32_t connectTimeoutMs)
}
DEBUG_WIFI_MULTI("\n");

// Create indices for AP connection failures
uint8_t connectSkipIndex[_APlist.size()];
memset(connectSkipIndex, 0, sizeof(connectSkipIndex));

// Connect to known WiFi AP's sorted by RSSI
for (int8_t i = 0; i < numNetworks; i++) {
// Get network information
WiFi.getNetworkInfo(known[i], ssid, encType, rssi, bssid, channel, hidden);

for (auto entry : _APlist) {
for (uint8_t j = 0; j < _APlist.size(); j++) {
auto &entry = _APlist[j];

// Check SSID
if (ssid == entry.ssid) {
DEBUG_WIFI_MULTI("[WIFIM] Connecting %s\n", ssid);
Expand All @@ -343,13 +349,33 @@ wl_status_t ESP8266WiFiMulti::connectWiFiMulti(uint32_t connectTimeoutMs)
if (waitWiFiConnect(connectTimeoutMs) == WL_CONNECTED) {
return WL_CONNECTED;
}

// Failed to connect, skip for hidden SSID connects
connectSkipIndex[j] = true;
}
}
}

// Try to connect to hidden AP's which are not reported by WiFi scan
for (uint8_t i = 0; i < _APlist.size(); i++) {
auto &entry = _APlist[i];

if (!connectSkipIndex[i]) {
DEBUG_WIFI_MULTI("[WIFIM] Try hidden connect %s\n", entry.ssid);

// Connect to WiFi
WiFi.begin(entry.ssid, entry.passphrase);

// Wait for status change
if (waitWiFiConnect(connectTimeoutMs) == WL_CONNECTED) {
return WL_CONNECTED;
}
}
}

DEBUG_WIFI_MULTI("[WIFIM] Could not connect\n", ssid);

// Coult not connect to any WiFi network
// Could not connect to any WiFi network
return WL_CONNECT_FAILED;
}

Expand Down

0 comments on commit 2402958

Please sign in to comment.