-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Feature/http client #1973
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
Merged
Merged
Feature/http client #1973
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
36a9a5a
Pass client parameter into two new begin() functions. Set other begin…
03c4da8
Added working HTTPS example on a public url with a certificate
baeb171
Remove two unnecessary tests in ::disconnect()
d7293a2
Add a scoping block to BasicHttpsClient.ino to assure HTTPClient is d…
367706e
Added check to handle mixup of old and present api properly
8275243
Correct HTTPClient::setTimeout() to convert milliseconds to seconds. …
79724d6
Added small comment because it looked like the Travis build did not f…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
libraries/HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
BasicHTTPSClient.ino | ||
|
||
Created on: 14.10.2018 | ||
|
||
*/ | ||
|
||
#include <Arduino.h> | ||
|
||
#include <WiFi.h> | ||
#include <WiFiMulti.h> | ||
|
||
#include <HTTPClient.h> | ||
|
||
#include <WiFiClientSecure.h> | ||
|
||
// This is GandiStandardSSLCA2.pem, the root Certificate Authority that signed | ||
// the server certifcate for the demo server https://jigsaw.w3.org in this | ||
// example. This certificate is valid until Sep 11 23:59:59 2024 GMT | ||
const char* rootCACertificate = \ | ||
"-----BEGIN CERTIFICATE-----\n" \ | ||
"MIIF6TCCA9GgAwIBAgIQBeTcO5Q4qzuFl8umoZhQ4zANBgkqhkiG9w0BAQwFADCB\n" \ |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u5/3EFZucJ48y7RetWIExKREa\n" \ | ||
"m9T8bJUox04FB6b9HbwZ4ui3uRGKLXASUoWNjDNKD/yZkuBjcNqllEdjB+dYxzFf\n" \ | ||
"BT02Vf6Dsuimrdfp5gJ0iHRc2jTbkNJtUQoj1iM=\n" \ | ||
"-----END CERTIFICATE-----\n"; | ||
|
||
// Not sure if WiFiClientSecure checks the validity date of the certificate. | ||
// Setting clock just to be sure... | ||
void setClock() { | ||
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); | ||
|
||
Serial.print(F("Waiting for NTP time sync: ")); | ||
time_t nowSecs = time(nullptr); | ||
while (nowSecs < 8 * 3600 * 2) { | ||
delay(500); | ||
Serial.print(F(".")); | ||
yield(); | ||
nowSecs = time(nullptr); | ||
} | ||
|
||
Serial.println(); | ||
struct tm timeinfo; | ||
gmtime_r(&nowSecs, &timeinfo); | ||
Serial.print(F("Current time: ")); | ||
Serial.print(asctime(&timeinfo)); | ||
} | ||
|
||
|
||
WiFiMulti WiFiMulti; | ||
|
||
void setup() { | ||
|
||
Serial.begin(115200); | ||
// Serial.setDebugOutput(true); | ||
|
||
Serial.println(); | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
WiFi.mode(WIFI_STA); | ||
WiFiMulti.addAP("SSID", "PASSWORD"); | ||
|
||
// wait for WiFi connection | ||
Serial.print("Waiting for WiFi to connect..."); | ||
while ((WiFiMulti.run() != WL_CONNECTED)) { | ||
Serial.print("."); | ||
} | ||
Serial.println(" connected"); | ||
|
||
setClock(); | ||
} | ||
|
||
void loop() { | ||
WiFiClientSecure *client = new WiFiClientSecure; | ||
if(client) { | ||
client -> setCACert(rootCACertificate); | ||
|
||
{ | ||
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is | ||
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"); | ||
} | ||
|
||
// End extra scoping block | ||
} | ||
|
||
delete client; | ||
} else { | ||
Serial.println("Unable to create client"); | ||
} | ||
|
||
Serial.println(); | ||
Serial.println("Waiting 10s before the next round..."); | ||
delay(10000); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Two questions:
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.
@marcelstoer do you mean 8 * 3600 * 2 with 'this static value'? If so, you shouldn't bother, the compiler will replace it with a static value.
Basically the value is quite arbitrary, any big enough value will do. time(nullptr) will return the number of seconds since the last reset, until it is overwritten by the NTP time sync. After that time(nullptr) is far bigger than the static test value and the while loop exits.
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.
time_t starts counting at the unix Epoch which you can read up about here.
If the time sync takes more than 16 hours there is a serious issue with network connectivity and time sync!
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.
Errhm, yes. Sorry. I should have said "constant" when I mean "constant".
True, the comment was more a style nitpick (and in that quit subjective I know).
@Jeroen88 @atanisoft for my second comment I was drawing from ESP8266 (i.e. NONOS SDK) experience, maybe it doesn't apply here anymore. For devices that spent most of their life in deep sleep we had to ensure that the constant value was a somewhat recent timestamp - at least from the current century. Otherwise, we might get
nowSecs
values that are way off.while
loops a handful of times. Hence, it has nothing to do with "time sync takes more than 16 hours" or so.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.
@marcelstoer I do not have any experience with deep sleep, but if time(nullptr) keeps running in deep sleep, the while() will break inmediately. I can imagine that in that case the ESP time and the real (network / NTP time) have drifted, and that you do want to have a time sync. The only thing I can think of here is to force a NTP time sync.
@atanisoft I didn't test it, but a freshly booted ESP32 could only initiate it's time after it has connected to the network, I guess? So some kind of waiting should be introduced to ensure synchronization has finished?