-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add a PostHttpsClient example in the ESP8266HTTPClient library. #8187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b15cff1
f21bd45
d215f57
5ff0e20
2376fa6
a1f407c
8a7435d
6df128b
ef2533f
654f2e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
PostHttpsClient.ino | ||
Created on: 28.06.2021 | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include <ESP8266WiFi.h> | ||
#include <ESP8266WiFiMulti.h> | ||
#include <WiFiClientSecureBearSSL.h> | ||
#include <ESP8266HTTPClient.h> | ||
|
||
// 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; | ||
|
||
|
||
|
||
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_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure); | ||
client->setInsecure(); // Ignore SSL certificate | ||
//client->setFingerprint(fingerprint); //Use SSL | ||
HTTPClient https; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For coherency,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stack (cons: too small or precious), Heap (cons: fragmentation), or global (cons: cannot be released), the debate is never closed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You really want to allocate and leave allocated a WiFiClientSecure either globally or very near the beginning of setup(). This is because the client will allocate memory for itself (~17kb IIRC) but also allocate ~7kb for the SSL stack. Later in the app the heap may have enough space, but be fragmented such that you might have 30kb free but no single block of 17kb or 7kb for the stack or the SSL buffers. |
||
|
||
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"); | ||
} | ||
} | ||
delay(10000); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is prefered to use secure connections in examples.
You may leave the
setInsecure()
commented though.