From 10ae26da4679e15fd295598153a7b19472e187dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 28 Aug 2016 11:17:26 +0200 Subject: [PATCH] Wait X milliseconds for autoConnection and skip non-defined SSIDs --- code/lib/JustWifi/JustWifi.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/code/lib/JustWifi/JustWifi.cpp b/code/lib/JustWifi/JustWifi.cpp index 8833fb2b..19b8a551 100644 --- a/code/lib/JustWifi/JustWifi.cpp +++ b/code/lib/JustWifi/JustWifi.cpp @@ -30,19 +30,30 @@ bool JustWifi::connected() { bool JustWifi::autoConnect() { + unsigned long timeout; + // Return if already connected if (connected()) return true; // Try to connect to last successful network - WiFi.mode(WIFI_STA); - if (WiFi.SSID()) { + if (WiFi.SSID().length() > 0) { - _doCallback(MESSAGE_AUTO_CONNECTING, (char *) WiFi.SSID().c_str()); ETS_UART_INTR_DISABLE(); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); + + _doCallback(MESSAGE_AUTO_CONNECTING, (char *) WiFi.SSID().c_str()); + WiFi.mode(WIFI_STA); WiFi.begin(); + // Check connection with timeout + timeout = millis(); + while (millis() - timeout < _connect_timeout) { + _doCallback(MESSAGE_CONNECT_WAITING); + if (WiFi.status() == WL_CONNECTED) break; + delay(100); + } + if (WiFi.status() == WL_CONNECTED) { _mode = MODE_STATION; _doCallback(MESSAGE_CONNECTED); @@ -72,6 +83,9 @@ bool JustWifi::connect() { // Loop through configured networks for (unsigned char i=0; i<_network_count; i++) { + // Skip if no SSID defined + if (_network[i].ssid.length() == 0) continue; + // TODO: Static IP options here // Connect