12
12
13
13
#include < ESP8266HTTPClient.h>
14
14
15
- #define USE_SERIAL Serial
15
+ #include < WiFiClient.h >
16
16
17
17
ESP8266WiFiMulti WiFiMulti;
18
18
19
19
void setup () {
20
20
21
- USE_SERIAL .begin (115200 );
22
- // USE_SERIAL .setDebugOutput(true);
21
+ Serial .begin (115200 );
22
+ // Serial .setDebugOutput(true);
23
23
24
- USE_SERIAL .println ();
25
- USE_SERIAL .println ();
26
- USE_SERIAL .println ();
24
+ Serial .println ();
25
+ Serial .println ();
26
+ Serial .println ();
27
27
28
28
for (uint8_t t = 4 ; t > 0 ; t--) {
29
- USE_SERIAL .printf (" [SETUP] WAIT %d...\n " , t);
30
- USE_SERIAL .flush ();
29
+ Serial .printf (" [SETUP] WAIT %d...\n " , t);
30
+ Serial .flush ();
31
31
delay (1000 );
32
32
}
33
33
@@ -40,34 +40,37 @@ void loop() {
40
40
// wait for WiFi connection
41
41
if ((WiFiMulti.run () == WL_CONNECTED)) {
42
42
43
+ WiFiClient client;
44
+
43
45
HTTPClient http;
44
46
45
- USE_SERIAL.print (" [HTTP] begin...\n " );
46
- // configure traged server and url
47
- // http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
48
- http.begin (" http://192.168.1.12/test.html" ); // HTTP
47
+ Serial.print (" [HTTP] begin...\n " );
48
+ if (http.begin (client, " http://jigsaw.w3.org/HTTP/connection.html" )) { // HTTP
49
+
49
50
50
- USE_SERIAL .print (" [HTTP] GET...\n " );
51
- // start connection and send HTTP header
52
- int httpCode = http.GET ();
51
+ Serial .print (" [HTTP] GET...\n " );
52
+ // start connection and send HTTP header
53
+ int httpCode = http.GET ();
53
54
54
- // httpCode will be negative on error
55
- if (httpCode > 0 ) {
56
- // HTTP header has been send and Server response header has been handled
57
- USE_SERIAL .printf (" [HTTP] GET... code: %d\n " , httpCode);
55
+ // httpCode will be negative on error
56
+ if (httpCode > 0 ) {
57
+ // HTTP header has been send and Server response header has been handled
58
+ Serial .printf (" [HTTP] GET... code: %d\n " , httpCode);
58
59
59
- // file found at server
60
- if (httpCode == HTTP_CODE_OK) {
61
- String payload = http.getString ();
62
- USE_SERIAL.println (payload);
60
+ // file found at server
61
+ if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
62
+ String payload = http.getString ();
63
+ Serial.println (payload);
64
+ }
65
+ } else {
66
+ Serial.printf (" [HTTP] GET... failed, error: %s\n " , http.errorToString (httpCode).c_str ());
63
67
}
68
+
69
+ http.end ();
64
70
} else {
65
- USE_SERIAL .printf (" [HTTP] GET... failed, error: %s \n " , http. errorToString (httpCode). c_str () );
71
+ Serial .printf (" [HTTP} Unable to connect \n " );
66
72
}
67
-
68
- http.end ();
69
73
}
70
74
71
75
delay (10000 );
72
76
}
73
-
0 commit comments