Skip to content

Commit d4db72d

Browse files
author
Jeroen88
committed
add basicHttpsClient example, ensured in the backward compatible function that only one connection is made, updated version to 1.2 deprecating present begin() functions, several minor updates
1 parent 5aff343 commit d4db72d

File tree

9 files changed

+290
-167
lines changed

9 files changed

+290
-167
lines changed

libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414

1515
#include <WiFiClient.h>
1616

17-
#define USE_SERIAL Serial
18-
1917
ESP8266WiFiMulti WiFiMulti;
2018

2119
void setup() {
2220

23-
USE_SERIAL.begin(115200);
24-
// USE_SERIAL.setDebugOutput(true);
21+
Serial.begin(115200);
22+
// Serial.setDebugOutput(true);
2523

26-
USE_SERIAL.println();
27-
USE_SERIAL.println();
28-
USE_SERIAL.println();
24+
Serial.println();
25+
Serial.println();
26+
Serial.println();
2927

3028
for (uint8_t t = 4; t > 0; t--) {
31-
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
32-
USE_SERIAL.flush();
29+
Serial.printf("[SETUP] WAIT %d...\n", t);
30+
Serial.flush();
3331
delay(1000);
3432
}
3533

@@ -46,7 +44,7 @@ void loop() {
4644

4745
HTTPClient http;
4846

49-
USE_SERIAL.print("[HTTP] begin...\n");
47+
Serial.print("[HTTP] begin...\n");
5048
// configure traged server and url
5149

5250

@@ -63,22 +61,22 @@ void loop() {
6361
*/
6462

6563

66-
USE_SERIAL.print("[HTTP] GET...\n");
64+
Serial.print("[HTTP] GET...\n");
6765
// start connection and send HTTP header
6866
int httpCode = http.GET();
6967

7068
// httpCode will be negative on error
7169
if (httpCode > 0) {
7270
// HTTP header has been send and Server response header has been handled
73-
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
71+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
7472

7573
// file found at server
7674
if (httpCode == HTTP_CODE_OK) {
7775
String payload = http.getString();
78-
USE_SERIAL.println(payload);
76+
Serial.println(payload);
7977
}
8078
} else {
81-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
79+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
8280
}
8381

8482
http.end();

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414

1515
#include <WiFiClient.h>
1616

17-
#define USE_SERIAL Serial
18-
1917
ESP8266WiFiMulti WiFiMulti;
2018

2119
void setup() {
2220

23-
USE_SERIAL.begin(115200);
24-
// USE_SERIAL.setDebugOutput(true);
21+
Serial.begin(115200);
22+
// Serial.setDebugOutput(true);
2523

26-
USE_SERIAL.println();
27-
USE_SERIAL.println();
28-
USE_SERIAL.println();
24+
Serial.println();
25+
Serial.println();
26+
Serial.println();
2927

3028
for (uint8_t t = 4; t > 0; t--) {
31-
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
32-
USE_SERIAL.flush();
29+
Serial.printf("[SETUP] WAIT %d...\n", t);
30+
Serial.flush();
3331
delay(1000);
3432
}
3533

@@ -46,33 +44,31 @@ void loop() {
4644

4745
HTTPClient http;
4846

49-
USE_SERIAL.print("[HTTP] begin...\n");
50-
// configure traged server and url
51-
//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
47+
Serial.print("[HTTP] begin...\n");
5248
if (http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html")) { // HTTP
5349

5450

55-
USE_SERIAL.print("[HTTP] GET...\n");
51+
Serial.print("[HTTP] GET...\n");
5652
// start connection and send HTTP header
5753
int httpCode = http.GET();
5854

5955
// httpCode will be negative on error
6056
if (httpCode > 0) {
6157
// HTTP header has been send and Server response header has been handled
62-
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
58+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
6359

6460
// file found at server
6561
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
6662
String payload = http.getString();
67-
USE_SERIAL.println(payload);
63+
Serial.println(payload);
6864
}
6965
} else {
70-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
66+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
7167
}
7268

7369
http.end();
7470
} else {
75-
USE_SERIAL.printf("Unable to connect\n");
71+
Serial.printf("[HTTP} Unable to connect\n");
7672
}
7773
}
7874

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
BasicHTTPSClient.ino
3+
4+
Created on: 20.08.2018
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266HTTPClient.h>
14+
15+
#include <WiFiClientSecureBearSSL.h>
16+
// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
17+
const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0};
18+
19+
ESP8266WiFiMulti WiFiMulti;
20+
21+
void setup() {
22+
23+
Serial.begin(115200);
24+
// Serial.setDebugOutput(true);
25+
26+
Serial.println();
27+
Serial.println();
28+
Serial.println();
29+
30+
for (uint8_t t = 4; t > 0; t--) {
31+
Serial.printf("[SETUP] WAIT %d...\n", t);
32+
Serial.flush();
33+
delay(1000);
34+
}
35+
36+
WiFi.mode(WIFI_STA);
37+
WiFiMulti.addAP("SSID", "PASSWORD");
38+
}
39+
40+
void loop() {
41+
// wait for WiFi connection
42+
if ((WiFiMulti.run() == WL_CONNECTED)) {
43+
44+
BearSSL::WiFiClientSecure client;
45+
client.setFingerprint(fingerprint);
46+
47+
HTTPClient https;
48+
49+
Serial.print("[HTTPS] begin...\n");
50+
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
51+
52+
53+
Serial.print("[HTTPS] GET...\n");
54+
// start connection and send HTTP header
55+
int httpCode = https.GET();
56+
57+
// httpCode will be negative on error
58+
if (httpCode > 0) {
59+
// HTTP header has been send and Server response header has been handled
60+
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
61+
62+
// file found at server
63+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
64+
String payload = https.getString();
65+
Serial.println(payload);
66+
}
67+
} else {
68+
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
69+
}
70+
71+
https.end();
72+
} else {
73+
Serial.printf("[HTTPS] Unable to connect\n");
74+
}
75+
}
76+
77+
delay(10000);
78+
}

libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@
1313

1414
#include <ESP8266HTTPClient.h>
1515

16-
#define USE_SERIAL Serial
17-
1816
ESP8266WiFiMulti WiFiMulti;
1917

2018
HTTPClient http;
2119

2220
void setup() {
2321

24-
USE_SERIAL.begin(115200);
25-
// USE_SERIAL.setDebugOutput(true);
22+
Serial.begin(115200);
23+
// Serial.setDebugOutput(true);
2624

27-
USE_SERIAL.println();
28-
USE_SERIAL.println();
29-
USE_SERIAL.println();
25+
Serial.println();
26+
Serial.println();
27+
Serial.println();
3028

3129
for (uint8_t t = 4; t > 0; t--) {
32-
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
33-
USE_SERIAL.flush();
30+
Serial.printf("[SETUP] WAIT %d...\n", t);
31+
Serial.flush();
3432
delay(1000);
3533
}
3634

@@ -47,19 +45,19 @@ void loop() {
4745

4846
WiFiClient client;
4947

50-
http.begin(client, "http://192.168.1.12/test.html");
51-
//http.begin(client, "192.168.1.12", 80, "/test.html");
48+
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
49+
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
5250

5351
int httpCode = http.GET();
5452
if (httpCode > 0) {
55-
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
53+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
5654

5755
// file found at server
5856
if (httpCode == HTTP_CODE_OK) {
59-
http.writeToStream(&USE_SERIAL);
57+
http.writeToStream(&Serial);
6058
}
6159
} else {
62-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
60+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
6361
}
6462

6563
http.end();

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212

1313
#include <ESP8266HTTPClient.h>
1414

15-
#define USE_SERIAL Serial
16-
1715
ESP8266WiFiMulti WiFiMulti;
1816

1917
void setup() {
2018

21-
USE_SERIAL.begin(115200);
22-
// USE_SERIAL.setDebugOutput(true);
19+
Serial.begin(115200);
20+
// Serial.setDebugOutput(true);
2321

24-
USE_SERIAL.println();
25-
USE_SERIAL.println();
26-
USE_SERIAL.println();
22+
Serial.println();
23+
Serial.println();
24+
Serial.println();
2725

2826
for (uint8_t t = 4; t > 0; t--) {
29-
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30-
USE_SERIAL.flush();
27+
Serial.printf("[SETUP] WAIT %d...\n", t);
28+
Serial.flush();
3129
delay(1000);
3230
}
3331

@@ -44,18 +42,18 @@ void loop() {
4442

4543
WiFiClient client;
4644

47-
USE_SERIAL.print("[HTTP] begin...\n");
45+
Serial.print("[HTTP] begin...\n");
4846

4947
// configure server and url
5048
http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html");
5149
//http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
5250

53-
USE_SERIAL.print("[HTTP] GET...\n");
51+
Serial.print("[HTTP] GET...\n");
5452
// start connection and send HTTP header
5553
int httpCode = http.GET();
5654
if (httpCode > 0) {
5755
// HTTP header has been send and Server response header has been handled
58-
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
56+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
5957

6058
// file found at server
6159
if (httpCode == HTTP_CODE_OK) {
@@ -79,7 +77,7 @@ void loop() {
7977
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
8078

8179
// write it to Serial
82-
USE_SERIAL.write(buff, c);
80+
Serial.write(buff, c);
8381

8482
if (len > 0) {
8583
len -= c;
@@ -88,12 +86,12 @@ void loop() {
8886
delay(1);
8987
}
9088

91-
USE_SERIAL.println();
92-
USE_SERIAL.print("[HTTP] connection closed or file end.\n");
89+
Serial.println();
90+
Serial.print("[HTTP] connection closed or file end.\n");
9391

9492
}
9593
} else {
96-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
94+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
9795
}
9896

9997
http.end();

0 commit comments

Comments
 (0)