forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiMultiAdvanced.ino
64 lines (55 loc) · 1.95 KB
/
WiFiMultiAdvanced.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* This sketch tries to connect to the best AP available
* and tests for captive portals on open networks
*
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti wifiMulti;
// callback used to check Internet connectivity
bool testConnection(){
HTTPClient http;
http.begin("http://www.espressif.com");
int httpCode = http.GET();
// we expect to get a 301 because it will ask to use HTTPS instead of HTTP
if (httpCode == HTTP_CODE_MOVED_PERMANENTLY) return true;
return false;
}
void setup()
{
Serial.begin(115200);
delay(10);
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");
// These options can help when you need ANY kind of wifi connection to get a config file, report errors, etc.
wifiMulti.setStrictMode(false); // Default is true. Library will disconnect and forget currently connected AP if it's not in the AP list.
wifiMulti.setAllowOpenAP(true); // Default is false. True adds open APs to the AP list.
wifiMulti.setConnectionTestCallbackFunc(testConnection); // Attempts to connect to a remote webserver in case of captive portals.
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
void loop()
{
static bool isConnected = false;
uint8_t WiFiStatus = wifiMulti.run();
if (WiFiStatus == WL_CONNECTED) {
if (!isConnected) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
isConnected = true;
} else {
Serial.println("WiFi not connected!");
isConnected = false;
delay(5000);
}
}