Skip to content

Commit

Permalink
Add WiFiClient parameter to HTTPClient (esp8266#4980)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jeroen88 authored and earlephilhower committed Oct 6, 2018
1 parent 9bc8ea1 commit 13f3746
Show file tree
Hide file tree
Showing 18 changed files with 761 additions and 164 deletions.
30 changes: 27 additions & 3 deletions cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ void UpdaterClass::_reset() {
_currentAddress = 0;
_size = 0;
_command = U_FLASH;

if(_ledPin != -1) {
digitalWrite(_ledPin, _ledStateRestore);
}
}

bool UpdaterClass::begin(size_t size, int command) {
bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
if(_size > 0){
#ifdef DEBUG_UPDATER
DEBUG_UPDATER.println(F("[begin] already running"));
#endif
return false;
}

_ledPin = ledPin;
_ledOn = ledOn;
if(_ledPin != -1) {
_ledStateRestore = digitalRead(_ledPin);
}

/* Check boot mode; if boot mode is 1 (UART download mode),
we will not be able to reset into normal mode once update is done.
Fail early to avoid frustration.
Expand Down Expand Up @@ -360,18 +370,32 @@ size_t UpdaterClass::writeStream(Stream &data) {
return 0;
}

if(_ledPin != -1) {
pinMode(_ledPin, OUTPUT);
}

while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
if(_ledPin != -1) {
digitalWrite(LED_BUILTIN, _ledOn); // Switch LED on
}
size_t bytesToRead = _bufferSize - _bufferLen;
if(bytesToRead > remaining()) {
bytesToRead = remaining();
}
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
delay(100);
toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
_currentAddress = (_startAddress + _size);
_setError(UPDATE_ERROR_STREAM);
_reset();
return written;
}
}
if(_ledPin != -1) {
digitalWrite(LED_BUILTIN, _ledOn == HIGH ? LOW : HIGH); // Switch LED off
}
_bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer())
return written;
Expand Down
6 changes: 5 additions & 1 deletion cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class UpdaterClass {
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size, int command = U_FLASH);
bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);

/*
Run Updater from asynchronous callbacs
Expand Down Expand Up @@ -162,6 +162,10 @@ class UpdaterClass {

String _target_md5;
MD5Builder _md5;

int _ledPin;
uint8_t _ledOn;
int _ledStateRestore;
};

extern UpdaterClass Update;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial
#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
Serial.begin(115200);
// Serial.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
Serial.println();
Serial.println();
Serial.println();

for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}

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

WiFiClient client;

HTTPClient http;

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


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

/*
// or
http.begin("http://192.168.1.12/test.html");
http.setAuthorization("user", "password");
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
http.setAuthorization("guest", "guest");
// or
http.begin("http://192.168.1.12/test.html");
http.setAuthorization("dXNlcjpwYXN3b3Jk");
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
*/


USE_SERIAL.print("[HTTP] GET...\n");
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();

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

// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
Serial.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}

http.end();
}

delay(10000);
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial
#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
Serial.begin(115200);
// Serial.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
Serial.println();
Serial.println();
Serial.println();

for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}

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

WiFiClient client;

HTTPClient http;

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


USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();

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

// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}

http.end();
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
Serial.printf("[HTTP} Unable to connect\n");
}

http.end();
}

delay(10000);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
BasicHTTPSClient.ino
Created on: 20.08.2018
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
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};

ESP8266WiFiMulti WiFiMulti;

void setup() {

Serial.begin(115200);
// Serial.setDebugOutput(true);

Serial.println();
Serial.println();
Serial.println();

for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}

WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
}

void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

BearSSL::WiFiClientSecure client;
client.setFingerprint(fingerprint);

HTTPClient https;

Serial.print("[HTTPS] begin...\n");
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS


Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();

// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}

https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}

delay(10000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <ESP8266HTTPClient.h>

const char* ssid = "........";
const char* ssidPassword = "........";
const char* ssid = "SSID";
const char* ssidPassword = "PASSWORD";

const char *username = "admin";
const char *password = "admin";
Expand Down Expand Up @@ -76,7 +76,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass
}

void setup() {
Serial.begin(9600);
Serial.begin(115200);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, ssidPassword);
Expand All @@ -95,10 +95,12 @@ void setup() {
void loop() {
HTTPClient http;

WiFiClient client;

Serial.print("[HTTP] begin...\n");

// configure traged server and url
http.begin(String(server) + String(uri));
http.begin(client, String(server) + String(uri));


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

http.end();
http.begin(String(server) + String(uri));
http.begin(client, String(server) + String(uri));

http.addHeader("Authorization", authorization);

Expand Down
Loading

0 comments on commit 13f3746

Please sign in to comment.