-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Because of git problems, start from a new fork and create a new PR. This was PR #6457 * Style update to pass Travis * Update ReuseConnectionV2.ino * fix + enforce testing http code per @earlephilhower review * Close connection before ::connecting on HTTP/1.0 HTTPClient never actually closes the TCP connection on its own. It will leave the TCP connection open unless you explicitly do a getString which makes a StreamString and stuffs it with the HTTP server response, at which point the HTTP server itself will close the connection. If you check the HTTP error code and find failure, unless you do a getString and throw it away, it won't disconnect. Even in HTTP/1.0 or in cases when you haven't enabled _reuse. Change the logic in ::connect to only reuse the connection when it is specifically allowed. Otherwise, fall back to re-connection. * Adjust example per request Do single URL get in each loop, avoid infinite for loop at end. * Fix astyle * Clean up final pass notice * Fix example syntax error Editing code in a web textbox without running it is a painful process. Co-authored-by: Earle F. Philhower, III <earlephilhower@yahoo.com>
- Loading branch information
1 parent
ed8add5
commit 157ce57
Showing
2 changed files
with
104 additions
and
19 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
libraries/ESP8266HTTPClient/examples/ReuseConnectionV2/ReuseConnectionV2.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters