Skip to content

Commit 13f3746

Browse files
Jeroen88earlephilhower
Jeroen88
authored andcommitted
Add WiFiClient parameter to HTTPClient (#4980)
Make HTTPClient take a WiFiClient parameter, allowing you to pass in a simple HTTP WiFiClient or a BearSSL or axTLS WiFiClientSecure with any desired verification options. Deprecate the older, TLSTraits methods. Add basic HttpsClient example. Add optional LED feedback to the Update class
1 parent 9bc8ea1 commit 13f3746

File tree

18 files changed

+761
-164
lines changed

18 files changed

+761
-164
lines changed

cores/esp8266/Updater.cpp

+27-3
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,26 @@ void UpdaterClass::_reset() {
3535
_currentAddress = 0;
3636
_size = 0;
3737
_command = U_FLASH;
38+
39+
if(_ledPin != -1) {
40+
digitalWrite(_ledPin, _ledStateRestore);
41+
}
3842
}
3943

40-
bool UpdaterClass::begin(size_t size, int command) {
44+
bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
4145
if(_size > 0){
4246
#ifdef DEBUG_UPDATER
4347
DEBUG_UPDATER.println(F("[begin] already running"));
4448
#endif
4549
return false;
4650
}
4751

52+
_ledPin = ledPin;
53+
_ledOn = ledOn;
54+
if(_ledPin != -1) {
55+
_ledStateRestore = digitalRead(_ledPin);
56+
}
57+
4858
/* Check boot mode; if boot mode is 1 (UART download mode),
4959
we will not be able to reset into normal mode once update is done.
5060
Fail early to avoid frustration.
@@ -360,18 +370,32 @@ size_t UpdaterClass::writeStream(Stream &data) {
360370
return 0;
361371
}
362372

373+
if(_ledPin != -1) {
374+
pinMode(_ledPin, OUTPUT);
375+
}
376+
363377
while(remaining()) {
364-
toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
378+
if(_ledPin != -1) {
379+
digitalWrite(LED_BUILTIN, _ledOn); // Switch LED on
380+
}
381+
size_t bytesToRead = _bufferSize - _bufferLen;
382+
if(bytesToRead > remaining()) {
383+
bytesToRead = remaining();
384+
}
385+
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
365386
if(toRead == 0) { //Timeout
366387
delay(100);
367-
toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
388+
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
368389
if(toRead == 0) { //Timeout
369390
_currentAddress = (_startAddress + _size);
370391
_setError(UPDATE_ERROR_STREAM);
371392
_reset();
372393
return written;
373394
}
374395
}
396+
if(_ledPin != -1) {
397+
digitalWrite(LED_BUILTIN, _ledOn == HIGH ? LOW : HIGH); // Switch LED off
398+
}
375399
_bufferLen += toRead;
376400
if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer())
377401
return written;

cores/esp8266/Updater.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class UpdaterClass {
3535
Call this to check the space needed for the update
3636
Will return false if there is not enough space
3737
*/
38-
bool begin(size_t size, int command = U_FLASH);
38+
bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
3939

4040
/*
4141
Run Updater from asynchronous callbacs
@@ -162,6 +162,10 @@ class UpdaterClass {
162162

163163
String _target_md5;
164164
MD5Builder _md5;
165+
166+
int _ledPin;
167+
uint8_t _ledOn;
168+
int _ledStateRestore;
165169
};
166170

167171
extern UpdaterClass Update;

libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino

+20-19
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
#include <ESP8266HTTPClient.h>
1414

15-
#define USE_SERIAL Serial
15+
#include <WiFiClient.h>
1616

1717
ESP8266WiFiMulti WiFiMulti;
1818

1919
void setup() {
2020

21-
USE_SERIAL.begin(115200);
22-
// USE_SERIAL.setDebugOutput(true);
21+
Serial.begin(115200);
22+
// Serial.setDebugOutput(true);
2323

24-
USE_SERIAL.println();
25-
USE_SERIAL.println();
26-
USE_SERIAL.println();
24+
Serial.println();
25+
Serial.println();
26+
Serial.println();
2727

2828
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();
3131
delay(1000);
3232
}
3333

@@ -40,46 +40,47 @@ void loop() {
4040
// wait for WiFi connection
4141
if ((WiFiMulti.run() == WL_CONNECTED)) {
4242

43+
WiFiClient client;
44+
4345
HTTPClient http;
4446

45-
USE_SERIAL.print("[HTTP] begin...\n");
47+
Serial.print("[HTTP] begin...\n");
4648
// configure traged server and url
4749

4850

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

5153
/*
5254
// or
53-
http.begin("http://192.168.1.12/test.html");
54-
http.setAuthorization("user", "password");
55+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
56+
http.setAuthorization("guest", "guest");
5557
5658
// or
57-
http.begin("http://192.168.1.12/test.html");
58-
http.setAuthorization("dXNlcjpwYXN3b3Jk");
59+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
60+
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
5961
*/
6062

6163

62-
USE_SERIAL.print("[HTTP] GET...\n");
64+
Serial.print("[HTTP] GET...\n");
6365
// start connection and send HTTP header
6466
int httpCode = http.GET();
6567

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

7173
// file found at server
7274
if (httpCode == HTTP_CODE_OK) {
7375
String payload = http.getString();
74-
USE_SERIAL.println(payload);
76+
Serial.println(payload);
7577
}
7678
} else {
77-
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());
7880
}
7981

8082
http.end();
8183
}
8284

8385
delay(10000);
8486
}
85-

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

+30-27
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
#include <ESP8266HTTPClient.h>
1414

15-
#define USE_SERIAL Serial
15+
#include <WiFiClient.h>
1616

1717
ESP8266WiFiMulti WiFiMulti;
1818

1919
void setup() {
2020

21-
USE_SERIAL.begin(115200);
22-
// USE_SERIAL.setDebugOutput(true);
21+
Serial.begin(115200);
22+
// Serial.setDebugOutput(true);
2323

24-
USE_SERIAL.println();
25-
USE_SERIAL.println();
26-
USE_SERIAL.println();
24+
Serial.println();
25+
Serial.println();
26+
Serial.println();
2727

2828
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();
3131
delay(1000);
3232
}
3333

@@ -40,34 +40,37 @@ void loop() {
4040
// wait for WiFi connection
4141
if ((WiFiMulti.run() == WL_CONNECTED)) {
4242

43+
WiFiClient client;
44+
4345
HTTPClient http;
4446

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+
4950

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();
5354

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);
5859

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());
6367
}
68+
69+
http.end();
6470
} else {
65-
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
71+
Serial.printf("[HTTP} Unable to connect\n");
6672
}
67-
68-
http.end();
6973
}
7074

7175
delay(10000);
7276
}
73-
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/DigestAuthorization/DigestAuthorization.ino

+7-5
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, String(server) + String(uri));
102104

103105

104106
const char *keys[] = {"WWW-Authenticate"};
@@ -115,7 +117,7 @@ void loop() {
115117
String authorization = getDigestAuth(authReq, String(username), String(password), String(uri), 1);
116118

117119
http.end();
118-
http.begin(String(server) + String(uri));
120+
http.begin(client, String(server) + String(uri));
119121

120122
http.addHeader("Authorization", authorization);
121123

0 commit comments

Comments
 (0)