Skip to content

Add callback to allow Static IP per SSID in WiFiMulti #9232

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions libraries/ESP8266WiFi/examples/WiFiMulti/WiFiMulti.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- 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
- Static IP assigned depending on which SSID is connected

To enable debugging output, select in the Arduino iDE:
- Tools | Debug Port: Serial
Expand Down Expand Up @@ -35,9 +36,26 @@ void setup() {
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

wifiMulti.onSSIDSelected(connectedToSSID);

// More is possible
}

void connectedToSSID(const char *ssid) {
// On connecting to the second AP, assign static IP using config(...).
if (strcmp(ssid, "ssid_from_AP_2") == 0) {
IPAddress ip2(192, 168, 1, 123);
IPAddress gw2(192, 168, 1, 1);
IPAddress subnet2(255, 255, 255, 0);
WiFi.config(ip2, gw2, subnet2);
return;
}

// For other SSID DHCP will be used.
WiFi.config(0U, 0U, 0U);
}

void loop() {
// Maintain WiFi connection
if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
Expand Down
10 changes: 10 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ wl_status_t ESP8266WiFiMulti::connectWiFiMulti(uint32_t connectTimeoutMs)
if (ssid == entry.ssid) {
DEBUG_WIFI_MULTI("[WIFIM] Connecting %s\n", ssid.c_str());

// User-defined callback
if (_onSSIDSelected) {
_onSSIDSelected(entry.ssid);
}

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

Expand All @@ -350,6 +355,11 @@ wl_status_t ESP8266WiFiMulti::connectWiFiMulti(uint32_t connectTimeoutMs)
if (!connectSkipIndex[i]) {
DEBUG_WIFI_MULTI("[WIFIM] Try hidden connect %s\n", entry.ssid);

// User-defined callback
if (_onSSIDSelected) {
_onSSIDSelected(entry.ssid);
}

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

Expand Down
18 changes: 10 additions & 8 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,31 @@
#define WIFI_SCAN_TIMEOUT_MS 5000
#endif

struct WifiAPEntry {
char *ssid;
char *passphrase;
};

typedef std::vector<WifiAPEntry> WifiAPlist;

class ESP8266WiFiMulti
{
public:
using SSIDSelectedCallback = void (*)(const char *ssid);

ESP8266WiFiMulti();
~ESP8266WiFiMulti();

bool addAP(const char *ssid, const char *passphrase = NULL);
bool existsAP(const char *ssid, const char *passphrase = NULL);
void onSSIDSelected(SSIDSelectedCallback callback) { _onSSIDSelected = callback; }

wl_status_t run(uint32_t connectTimeoutMs=WIFI_CONNECT_TIMEOUT_MS);

void cleanAPlist();
int count() { return _APlist.size(); }
private:
WifiAPlist _APlist;
struct WifiAPEntry {
char *ssid;
char *passphrase;
};

std::vector<WifiAPEntry> _APlist;
bool _firstRun;
SSIDSelectedCallback _onSSIDSelected = NULL;

bool APlistAdd(const char *ssid, const char *passphrase = NULL);
bool APlistExists(const char *ssid, const char *passphrase = NULL);
Expand Down
Loading