Skip to content

Commit 787f27f

Browse files
committed
feat(wifi): simplifies the example by using HTTPClient
1 parent bf315f8 commit 787f27f

File tree

1 file changed

+8
-57
lines changed

1 file changed

+8
-57
lines changed

libraries/WiFi/examples/WiFiMultiAdvanced/WiFiMultiAdvanced.ino

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,18 @@
66

77
#include <WiFi.h>
88
#include <WiFiMulti.h>
9+
#include <HTTPClient.h>
910

1011
WiFiMulti wifiMulti;
1112

12-
// This is used to test the Internet connection of connected the AP
13-
// Use a non-302 status code to ensure we bypass captive portals. Can be any text in the webpage.
14-
String _testResp = "301 Moved"; // usually http:// is moves to https:// by a 301 code
15-
// You can also set this to a simple test page on your own server to ensure you can reach it,
16-
// like "http://www.mysite.com/test.html"
17-
String _testURL = "http://www.espressif.com"; // Must include "http://" if testing a HTTP host
18-
const int _testPort = 80; // HTTP port
19-
13+
// callback used to check Internet connectivity
2014
bool testConnection(){
21-
//parse url
22-
int8_t split = _testURL.indexOf('/',7);
23-
String host = _testURL.substring(7, split);
24-
String url = (split < 0) ? "/":_testURL.substring(split,_testURL.length());
25-
log_i("Testing Connection to %s. Test Respponse is \"%s\"",_testURL.c_str(), _testResp.c_str());
26-
// Use WiFiClient class to create TCP connections
27-
WiFiClient client;
28-
if (!client.connect(host.c_str(), _testPort)) {
29-
log_e("Connection failed");
30-
return false;
31-
} else {
32-
log_i("Connected to test host");
33-
}
34-
35-
// This will send the request to the server
36-
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
37-
"Host: " + host + "\r\n" +
38-
"Connection: close\r\n\r\n");
39-
unsigned long timeout = millis();
40-
while (client.available() == 0) {
41-
if (millis() - timeout > 5000) {
42-
log_e(">>>Client timeout!");
43-
client.stop();
44-
return false;
45-
}
46-
}
47-
48-
bool bSuccess = false;
49-
timeout = millis();
50-
while(client.available()) {
51-
if (millis() - timeout < 5000) {
52-
String line = client.readStringUntil('\r');
53-
Serial.println("=============HTTP RESPONSE=============");
54-
Serial.print(line);
55-
Serial.println("\n=======================================");
56-
57-
bSuccess = client.find(_testResp.c_str());
58-
if (bSuccess){
59-
log_i("Success. Found test response");
60-
} else {
61-
log_e("Failed. Can't find test response");
62-
}
63-
return bSuccess;
64-
} else {
65-
log_e("Test Response checking has timed out!");
66-
break;
67-
}
68-
}
69-
return false; // timeout
15+
HTTPClient http;
16+
http.begin("http://www.espressif.com");
17+
int httpCode = http.GET();
18+
// we expect to get a 301 because it will ask to use HTTPS intead of HTTP
19+
if (httpCode == HTTP_CODE_MOVED_PERMANENTLY) return true;
20+
return false;
7021
}
7122

7223
void setup()

0 commit comments

Comments
 (0)