forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReuseConnectionV2.ino
82 lines (63 loc) · 1.87 KB
/
ReuseConnectionV2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
reuseConnectionV2.ino
Created on: 22.11.2015
This example reuses the http connection and also restores the connection if the connection is lost
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
ESP8266WiFiMulti WiFiMulti;
HTTPClient http;
WiFiClient client;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println("Connecting to WiFi...");
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(STASSID, STAPSK);
// wait for WiFi connection
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.write('.');
delay(500);
}
Serial.println(" connected to WiFi");
// allow reuse (if server supports it)
http.setReuse(true);
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
}
int pass = 0;
void loop() {
// First 10 loop()s, retrieve the URL
if (pass < 10) {
pass++;
Serial.printf("Reuse connection example, GET url for the %d time\n", pass);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
http.writeToStream(&Serial);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
// Something went wrong with the connection, try to reconnect
http.end();
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
}
if (pass == 10) {
http.end();
Serial.println("Done testing");
} else {
Serial.println("\n\n\nWait 5 second...\n");
delay(5000);
}
}
}