Skip to content

Commit 0a644f2

Browse files
author
Jeroen88
committed
Updated examples to work with new API and added a https example
1 parent 7535a40 commit 0a644f2

File tree

8 files changed

+176
-48
lines changed

8 files changed

+176
-48
lines changed

libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino

+9-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <ESP8266HTTPClient.h>
1414

15+
#include <WiFiClient.h>
16+
1517
#define USE_SERIAL Serial
1618

1719
ESP8266WiFiMulti WiFiMulti;
@@ -40,22 +42,24 @@ void loop() {
4042
// wait for WiFi connection
4143
if ((WiFiMulti.run() == WL_CONNECTED)) {
4244

45+
WiFiClient client;
46+
4347
HTTPClient http;
4448

4549
USE_SERIAL.print("[HTTP] begin...\n");
4650
// configure traged server and url
4751

4852

49-
http.begin("http://user:[email protected]/test.html");
53+
http.begin((Client&)client, "http://guest:[email protected]/HTTP/Basic/");
5054

5155
/*
5256
// or
53-
http.begin("http://192.168.1.12/test.html");
54-
http.setAuthorization("user", "password");
57+
http.begin("http://jigsaw.w3.org/HTTP/Basic/");
58+
http.setAuthorization("guest", "guest");
5559
5660
// or
57-
http.begin("http://192.168.1.12/test.html");
58-
http.setAuthorization("dXNlcjpwYXN3b3Jk");
61+
http.begin("http://jigsaw.w3.org/HTTP/Basic/");
62+
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
5963
*/
6064

6165

@@ -82,4 +86,3 @@ void loop() {
8286

8387
delay(10000);
8488
}
85-

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

+26-19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <ESP8266HTTPClient.h>
1414

15+
#include <WiFiClient.h>
16+
1517
#define USE_SERIAL Serial
1618

1719
ESP8266WiFiMulti WiFiMulti;
@@ -40,34 +42,39 @@ void loop() {
4042
// wait for WiFi connection
4143
if ((WiFiMulti.run() == WL_CONNECTED)) {
4244

45+
WiFiClient client;
46+
4347
HTTPClient http;
4448

4549
USE_SERIAL.print("[HTTP] begin...\n");
4650
// configure traged server and url
4751
//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
49-
50-
USE_SERIAL.print("[HTTP] GET...\n");
51-
// start connection and send HTTP header
52-
int httpCode = http.GET();
53-
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);
58-
59-
// file found at server
60-
if (httpCode == HTTP_CODE_OK) {
61-
String payload = http.getString();
62-
USE_SERIAL.println(payload);
52+
if(http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP
53+
54+
55+
USE_SERIAL.print("[HTTP] GET...\n");
56+
// start connection and send HTTP header
57+
int httpCode = http.GET();
58+
59+
// httpCode will be negative on error
60+
if (httpCode > 0) {
61+
// HTTP header has been send and Server response header has been handled
62+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
63+
64+
// file found at server
65+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
66+
String payload = http.getString();
67+
USE_SERIAL.println(payload);
68+
}
69+
} else {
70+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
6371
}
72+
73+
http.end();
6474
} else {
65-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
75+
USE_SERIAL.printf("Unable to connect\n");
6676
}
67-
68-
http.end();
6977
}
7078

7179
delay(10000);
7280
}
73-

libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include <ESP8266HTTPClient.h>
1313

14-
const char* ssid = "........";
15-
const char* ssidPassword = "........";
14+
const char* ssid = "SSID";
15+
const char* ssidPassword = "PASSWORD";
1616

1717
const char *username = "admin";
1818
const char *password = "admin";
@@ -76,7 +76,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass
7676
}
7777

7878
void setup() {
79-
Serial.begin(9600);
79+
Serial.begin(115200);
8080

8181
WiFi.mode(WIFI_STA);
8282
WiFi.begin(ssid, ssidPassword);
@@ -95,10 +95,12 @@ void setup() {
9595
void loop() {
9696
HTTPClient http;
9797

98+
WiFiClient client;
99+
98100
Serial.print("[HTTP] begin...\n");
99101

100102
// configure traged server and url
101-
http.begin(String(server) + String(uri));
103+
http.begin((Client&) client, String(server) + String(uri));
102104

103105

104106
const char *keys[] = {"WWW-Authenticate"};

libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ void loop() {
4545
// wait for WiFi connection
4646
if ((WiFiMulti.run() == WL_CONNECTED)) {
4747

48-
http.begin("http://192.168.1.12/test.html");
49-
//http.begin("192.168.1.12", 80, "/test.html");
48+
WiFiClient client;
49+
50+
http.begin((Client&) client, "http://192.168.1.12/test.html");
51+
//http.begin((Client&) client, "192.168.1.12", 80, "/test.html");
5052

5153
int httpCode = http.GET();
5254
if (httpCode > 0) {
@@ -65,6 +67,3 @@ void loop() {
6567

6668
delay(1000);
6769
}
68-
69-
70-

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ void loop() {
4242

4343
HTTPClient http;
4444

45+
WiFiClient client;
46+
4547
USE_SERIAL.print("[HTTP] begin...\n");
4648

4749
// configure server and url
48-
http.begin("http://192.168.1.12/test.html");
49-
//http.begin("192.168.1.12", 80, "/test.html");
50+
http.begin((Client&) client, "http://jigsaw.w3.org/HTTP/connection.html");
51+
//http.begin("jigsaw.w3.org", 80, "/HTTP/connection.html");
5052

5153
USE_SERIAL.print("[HTTP] GET...\n");
5254
// start connection and send HTTP header
@@ -65,7 +67,7 @@ void loop() {
6567
uint8_t buff[128] = { 0 };
6668

6769
// get tcp stream
68-
WiFiClient * stream = http.getStreamPtr();
70+
WiFiClient * stream = &client;
6971

7072
// read all data from server
7173
while (http.connected() && (len > 0 || len == -1)) {
@@ -99,4 +101,3 @@ void loop() {
99101

100102
delay(10000);
101103
}
102-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
StreamHTTPClient.ino
3+
4+
Created on: 24.05.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
23+
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
28+
for (uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
33+
34+
WiFi.mode(WIFI_STA);
35+
WiFiMulti.addAP("SSID", "PASSWORD");
36+
37+
}
38+
39+
void loop() {
40+
// wait for WiFi connection
41+
if ((WiFiMulti.run() == WL_CONNECTED)) {
42+
43+
HTTPClient http;
44+
45+
BearSSL::WiFiClientSecure client;
46+
47+
bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024);
48+
USE_SERIAL.printf("\nConnecting to https://tls.mbed.org\n");
49+
USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
50+
if (mfln) {
51+
client.setBufferSizes(1024, 1024);
52+
}
53+
54+
USE_SERIAL.print("[HTTP] begin...\n");
55+
56+
// configure server and url
57+
const uint8_t fingerprint[20] = {0xEB, 0xD9, 0xDF, 0x37, 0xC2, 0xCC, 0x84, 0x89, 0x00,0xA0, 0x58, 0x52, 0x24, 0x04, 0xE4, 0x37, 0x3E, 0x2B, 0xF1, 0x41};
58+
client.setFingerprint(fingerprint);
59+
60+
if(http.begin((Client&) client, "https://tls.mbed.org/")) {
61+
//if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) {
62+
63+
USE_SERIAL.print("[HTTP] GET...\n");
64+
// start connection and send HTTP header
65+
int httpCode = http.GET();
66+
if (httpCode > 0) {
67+
// HTTP header has been send and Server response header has been handled
68+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
69+
70+
// file found at server
71+
if (httpCode == HTTP_CODE_OK) {
72+
73+
// get lenght of document (is -1 when Server sends no Content-Length header)
74+
int len = http.getSize();
75+
76+
// create buffer for read
77+
uint8_t buff[128] = { 0 };
78+
79+
// get tcp stream
80+
WiFiClient * stream = &client;
81+
82+
// read all data from server
83+
while (http.connected() && (len > 0 || len == -1)) {
84+
// get available data size
85+
size_t size = stream->available();
86+
87+
if (size) {
88+
// read up to 128 byte
89+
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
90+
91+
// write it to Serial
92+
USE_SERIAL.write(buff, c);
93+
94+
if (len > 0) {
95+
len -= c;
96+
}
97+
}
98+
delay(1);
99+
}
100+
101+
USE_SERIAL.println();
102+
USE_SERIAL.print("[HTTP] connection closed or file end.\n");
103+
104+
}
105+
} else {
106+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
107+
}
108+
109+
http.end();
110+
} else {
111+
USE_SERIAL.printf("Unable to connect\n");
112+
}
113+
}
114+
115+
delay(10000);
116+
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void HTTPClient::clear()
143143
*/
144144
bool HTTPClient::begin(Client &client, String url) {
145145
#ifdef KEEP_PRESENT_API
146-
_tcpDepreciated.reset(nullptr);
146+
_tcpDeprecated.reset(nullptr);
147147
_transportTraits.reset(nullptr);
148148
#endif
149149
_tcp = &client;
@@ -178,7 +178,7 @@ bool HTTPClient::begin(Client &client, String url) {
178178
bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, bool https)
179179
{
180180
#ifdef KEEP_PRESENT_API
181-
_tcpDepreciated.reset(nullptr);
181+
_tcpDeprecated.reset(nullptr);
182182
_transportTraits.reset(nullptr);
183183
#endif
184184
_tcp = &client;
@@ -710,7 +710,7 @@ int HTTPClient::getSize(void)
710710
WiFiClient& HTTPClient::getStream(void)
711711
{
712712
if(connected()) {
713-
return *_tcpDepreciated;
713+
return *_tcpDeprecated;
714714
}
715715

716716
DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n");
@@ -725,7 +725,7 @@ WiFiClient& HTTPClient::getStream(void)
725725
WiFiClient* HTTPClient::getStreamPtr(void)
726726
{
727727
if(connected()) {
728-
return (WiFiClient*)_tcpDepreciated.get();
728+
return (WiFiClient*)_tcpDeprecated.get();
729729
}
730730

731731
DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n");
@@ -988,8 +988,8 @@ bool HTTPClient::connect(void)
988988

989989
#ifdef KEEP_PRESENT_API
990990
if(!_tcp) {
991-
_tcpDepreciated = _transportTraits->create();
992-
_tcp = _tcpDepreciated.get();
991+
_tcpDeprecated = _transportTraits->create();
992+
_tcp = _tcpDeprecated.get();
993993
}
994994
#endif
995995
_tcp->setTimeout(_tcpTimeout);
@@ -1002,7 +1002,7 @@ bool HTTPClient::connect(void)
10021002
DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port);
10031003

10041004
#ifdef KEEP_PRESENT_API
1005-
if (_tcpDepreciated && _transportTraits && !_transportTraits->verify(*_tcpDepreciated, _host.c_str())) {
1005+
if (_tcpDeprecated && _transportTraits && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) {
10061006
DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n");
10071007
_tcp->stop();
10081008
return false;
@@ -1012,8 +1012,8 @@ bool HTTPClient::connect(void)
10121012

10131013
#ifdef ESP8266
10141014
#ifdef KEEP_PRESENT_API
1015-
if(_tcpDepreciated)
1016-
_tcpDepreciated->setNoDelay(true);
1015+
if(_tcpDeprecated)
1016+
_tcpDeprecated->setNoDelay(true);
10171017
#else
10181018
// Client has no setNoDelay(), is this important???????????????
10191019
#endif

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class HTTPClient
223223

224224
#ifdef KEEP_PRESENT_API
225225
TransportTraitsPtr _transportTraits;
226-
std::unique_ptr<WiFiClient> _tcpDepreciated;
226+
std::unique_ptr<WiFiClient> _tcpDeprecated;
227227
#endif
228228
Client* _tcp;
229229

0 commit comments

Comments
 (0)