From b15cff1c9769d93dc35e41915acd33109d24043a Mon Sep 17 00:00:00 2001 From: Alba0404 Date: Mon, 28 Jun 2021 22:22:39 +0200 Subject: [PATCH 1/4] Add a PostHttpsClient example in the ESP8266HTTPClient library. --- .../PostHttpsClient/PostHttpsClient.ino | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino new file mode 100644 index 0000000000..40eeb7f0da --- /dev/null +++ b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino @@ -0,0 +1,87 @@ +/** + PostHttpsClient.ino + Created on: 28.06.2021 +*/ + +#include +#include +#include +#include +#include + +// Fingerprint for demo URL, expires on September 5, 2021, needs to be updated well before this date +const uint8_t fingerprint[20] = {0xbb, 0x88, 0x7f, 0x7c, 0x77, 0xc2, 0x59, 0x97, 0xb7, 0x00, 0x35, 0x74, 0x50, 0x47, 0x7e, 0x67, 0x42, 0x02, 0x2f, 0xf0}; +const char* URL = "https://www.reseau-astuce.fr/fr/horaires-a-larret/28/StopTimeTable/NextDeparture"; +const char* REQUEST = "destinations=%7B%221%22%3A%22Technop%C3%B4le+SAINT-ETIENNE-DU-ROUVRAY%22%7D&stopId=102154&lineId=175&sens=1"; + +const char* WIFI_SSID = "myWifiSSID"; +const char* WIFI_PWD = "myWifiPassword"; + +ESP8266WiFiMulti WiFiMulti; +HTTPClient https; + + + +void setup() { + + Serial.begin(115200); + Serial.println(); + Serial.println(); + Serial.println(); + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP(WIFI_SSID, WIFI_PWD); + + Serial.println("[WIFI] Connecting to WiFi ..."); + while (WiFiMulti.run() != WL_CONNECTED) { + Serial.print('.'); + delay(1000); + } + + Serial.println(); + Serial.print("[WIFI] Connected with IP : "); + Serial.println(WiFi.localIP()); + WiFi.setAutoReconnect(true); + WiFi.persistent(true); + +} + + + +void loop() { + + if ((WiFiMulti.run() == WL_CONNECTED)) { + Serial.println("[HTTPS] begin..."); + + std::unique_ptrclient(new BearSSL::WiFiClientSecure); + client->setInsecure(); // Ignore SSL certificate + //client->setFingerprint(fingerprint); //Use SSL + HTTPClient https; + + if (https.begin(*client, URL)) { + https.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"); + https.addHeader("Accept-Language", "fr-FR,fr;q=0.9,en;q=0.8"); + https.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + int httpCode = https.POST(REQUEST); + + // If response code is positive => no error + if (httpCode > 0) { + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + String payload = https.getString(); + Serial.println("[HTTPS] POST... SUCCESS!"); + Serial.println(payload); + } + + // Error (response code is negative) + } else { + Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); + } + https.end(); + + // Unable to reach the server + } else { + Serial.println("[HTTPS] Unable to connect"); + } + } + +} From f21bd45fdb46ec40691bee834e74518b501ba548 Mon Sep 17 00:00:00 2001 From: Alba0404 Date: Tue, 29 Jun 2021 12:51:38 +0200 Subject: [PATCH 2/4] Correct to pass style check --- .../PostHttpsClient/PostHttpsClient.ino | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino index 40eeb7f0da..284094d522 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino @@ -23,7 +23,7 @@ HTTPClient https; void setup() { - + Serial.begin(115200); Serial.println(); Serial.println(); @@ -31,13 +31,13 @@ void setup() { WiFi.mode(WIFI_STA); WiFiMulti.addAP(WIFI_SSID, WIFI_PWD); - + Serial.println("[WIFI] Connecting to WiFi ..."); while (WiFiMulti.run() != WL_CONNECTED) { Serial.print('.'); delay(1000); } - + Serial.println(); Serial.print("[WIFI] Connected with IP : "); Serial.println(WiFi.localIP()); @@ -49,10 +49,10 @@ void setup() { void loop() { - + if ((WiFiMulti.run() == WL_CONNECTED)) { Serial.println("[HTTPS] begin..."); - + std::unique_ptrclient(new BearSSL::WiFiClientSecure); client->setInsecure(); // Ignore SSL certificate //client->setFingerprint(fingerprint); //Use SSL @@ -71,17 +71,16 @@ void loop() { Serial.println("[HTTPS] POST... SUCCESS!"); Serial.println(payload); } - - // Error (response code is negative) + + // Error (response code is negative) } else { Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); } https.end(); - // Unable to reach the server + // Unable to reach the server } else { Serial.println("[HTTPS] Unable to connect"); } } - } From 5ff0e2095fc3926086119a1f18414c0372e6f5ea Mon Sep 17 00:00:00 2001 From: Alba0404 Date: Sun, 11 Jul 2021 17:36:11 +0200 Subject: [PATCH 3/4] Correct mistakes on https and client var. --- .../examples/PostHttpsClient/PostHttpsClient.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino index 284094d522..d6c7e9865c 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino @@ -14,11 +14,10 @@ const uint8_t fingerprint[20] = {0xbb, 0x88, 0x7f, 0x7c, 0x77, 0xc2, 0x59, 0x97, const char* URL = "https://www.reseau-astuce.fr/fr/horaires-a-larret/28/StopTimeTable/NextDeparture"; const char* REQUEST = "destinations=%7B%221%22%3A%22Technop%C3%B4le+SAINT-ETIENNE-DU-ROUVRAY%22%7D&stopId=102154&lineId=175&sens=1"; -const char* WIFI_SSID = "myWifiSSID"; -const char* WIFI_PWD = "myWifiPassword"; +const char* WIFI_SSID = "AirPort Extreme"; +const char* WIFI_PWD = "3saih3x8"; ESP8266WiFiMulti WiFiMulti; -HTTPClient https; @@ -83,4 +82,5 @@ void loop() { Serial.println("[HTTPS] Unable to connect"); } } + delay(10000); } From a1f407c0c00485efbb192a8257367d328fd1955d Mon Sep 17 00:00:00 2001 From: Alba0404 Date: Sun, 11 Jul 2021 17:52:53 +0200 Subject: [PATCH 4/4] Remove wifi password and SSID --- .../examples/PostHttpsClient/PostHttpsClient.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino index d6c7e9865c..ce8b3cc002 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpsClient/PostHttpsClient.ino @@ -14,8 +14,8 @@ const uint8_t fingerprint[20] = {0xbb, 0x88, 0x7f, 0x7c, 0x77, 0xc2, 0x59, 0x97, const char* URL = "https://www.reseau-astuce.fr/fr/horaires-a-larret/28/StopTimeTable/NextDeparture"; const char* REQUEST = "destinations=%7B%221%22%3A%22Technop%C3%B4le+SAINT-ETIENNE-DU-ROUVRAY%22%7D&stopId=102154&lineId=175&sens=1"; -const char* WIFI_SSID = "AirPort Extreme"; -const char* WIFI_PWD = "3saih3x8"; +const char* WIFI_SSID = "myWifiSSID"; +const char* WIFI_PWD = "myWifiPassword"; ESP8266WiFiMulti WiFiMulti;