|
20 | 20 | #ifdef ARDUINO_ARCH_ESP8266
|
21 | 21 | #include "Adafruit_MQTT.h"
|
22 | 22 | #include "Adafruit_MQTT_Client.h"
|
23 |
| -#include "ESP8266WiFiMulti.h" |
24 | 23 | #include "ESP8266WiFi.h"
|
| 24 | +#include "ESP8266WiFiMulti.h" |
25 | 25 | #include "Wippersnapper.h"
|
26 | 26 |
|
27 | 27 | /* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new
|
@@ -136,7 +136,14 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
|
136 | 136 |
|
137 | 137 | // Was the network within secrets.json found?
|
138 | 138 | for (int i = 0; i < n; ++i) {
|
139 |
| - if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) |
| 139 | + if (strlen(WS._multiNetworks[0].ssid) > 0) { |
| 140 | + // multi network mode |
| 141 | + for (int j = 0; j < 5; j++) { |
| 142 | + if (strcmp(WS._multiNetworks[j].ssid, WiFi.SSID(i).c_str()) == 0) |
| 143 | + return true; |
| 144 | + } |
| 145 | + } // else single network mode |
| 146 | + else if (strcmp(_ssid, WiFi.SSID(i).c_str()) == 0) |
140 | 147 | return true;
|
141 | 148 | }
|
142 | 149 |
|
@@ -227,21 +234,57 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
|
227 | 234 | if (WiFi.status() == WL_CONNECTED)
|
228 | 235 | return;
|
229 | 236 |
|
230 |
| - // Attempt connection |
231 |
| - _disconnect(); |
232 |
| - delay(100); |
233 |
| - // ESP8266 MUST be in STA mode to avoid device acting as client/server |
234 |
| - WiFi.mode(WIFI_STA); |
235 |
| - WiFi.begin(_ssid, _pass); |
236 |
| - _status = WS_NET_DISCONNECTED; |
237 |
| - delay(100); |
| 237 | + if (strlen(_ssid) == 0) { |
| 238 | + _status = WS_SSID_INVALID; |
| 239 | + } else { |
| 240 | + WiFi.setAutoReconnect(false); |
| 241 | + // Attempt connection |
| 242 | + _disconnect(); |
| 243 | + delay(100); |
| 244 | + // ESP8266 MUST be in STA mode to avoid device acting as client/server |
| 245 | + WiFi.mode(WIFI_STA); |
| 246 | + WiFi.begin(_ssid, _pass); |
| 247 | + _status = WS_NET_DISCONNECTED; |
| 248 | + delay(100); |
| 249 | + |
| 250 | + if (strlen(WS._multiNetworks[0].ssid) > 0) { |
| 251 | + // multi network mode |
| 252 | + for (int i = 0; i < 5; i++) { |
| 253 | + if (strlen(WS._multiNetworks[i].ssid) > 0 && |
| 254 | + (_wifiMulti.existsAP(WS._multiNetworks[i].ssid) == false)) { |
| 255 | + // doesn't exist, add it |
| 256 | + _wifiMulti.addAP(WS._multiNetworks[i].ssid, |
| 257 | + WS._multiNetworks[i].pass); |
| 258 | + } |
| 259 | + } |
| 260 | + long startRetry = millis(); |
| 261 | + WS_DEBUG_PRINTLN("CONNECTING"); |
| 262 | + while (_wifiMulti.run(5000) != WL_CONNECTED && millis() - startRetry < 10000) { |
| 263 | + // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite |
| 264 | + yield(); |
| 265 | + } |
| 266 | + if (_wifiMulti.run(5000) == WL_CONNECTED) { |
| 267 | + _status = WS_NET_CONNECTED; |
| 268 | + } else { |
| 269 | + _status = WS_NET_DISCONNECTED; |
| 270 | + } |
| 271 | + } else { |
| 272 | + // single network mode |
238 | 273 |
|
239 |
| - // wait for a connection to be established |
240 |
| - long startRetry = millis(); |
241 |
| - WS_DEBUG_PRINTLN("CONNECTING"); |
242 |
| - while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) { |
243 |
| - // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite |
244 |
| - yield(); |
| 274 | + // wait for a connection to be established |
| 275 | + long startRetry = millis(); |
| 276 | + WS_DEBUG_PRINTLN("CONNECTING"); |
| 277 | + while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) { |
| 278 | + // ESP8266 WDT requires yield() during a busy-loop so it doesn't bite |
| 279 | + yield(); |
| 280 | + } |
| 281 | + if (WiFi.status() == WL_CONNECTED) { |
| 282 | + _status = WS_NET_CONNECTED; |
| 283 | + } else { |
| 284 | + _status = WS_NET_DISCONNECTED; |
| 285 | + } |
| 286 | + } |
| 287 | + WS.feedWDT(); |
245 | 288 | }
|
246 | 289 | }
|
247 | 290 |
|
|
0 commit comments