Skip to content

Commit 01d22c8

Browse files
Jeroen88me-no-dev
Jeroen88
authored andcommitted
Feature/http client (#1973)
* Pass client parameter into two new begin() functions. Set other begin() functions deprecated. Updated library version to 1.2 * Added working HTTPS example on a public url with a certificate * Remove two unnecessary tests in ::disconnect() * Add a scoping block to BasicHttpsClient.ino to assure HTTPClient is destroyed before WiFiClientSecure * Added check to handle mixup of old and present api properly * Correct HTTPClient::setTimeout() to convert milliseconds to seconds. Correct WiFiClient::setTimeout() to call Stream::setTimeout() with seconds converted back to milliseconds. Remove inproper checks for _insecure. * Added small comment because it looked like the Travis build did not finish
1 parent b70737d commit 01d22c8

File tree

5 files changed

+351
-51
lines changed

5 files changed

+351
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
BasicHTTPSClient.ino
3+
4+
Created on: 14.10.2018
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
15+
#include <WiFiClientSecure.h>
16+
17+
// This is GandiStandardSSLCA2.pem, the root Certificate Authority that signed
18+
// the server certifcate for the demo server https://jigsaw.w3.org in this
19+
// example. This certificate is valid until Sep 11 23:59:59 2024 GMT
20+
const char* rootCACertificate = \
21+
"-----BEGIN CERTIFICATE-----\n" \
22+
"MIIF6TCCA9GgAwIBAgIQBeTcO5Q4qzuFl8umoZhQ4zANBgkqhkiG9w0BAQwFADCB\n" \
23+
"iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
24+
"cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
25+
"BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTQw\n" \
26+
"OTEyMDAwMDAwWhcNMjQwOTExMjM1OTU5WjBfMQswCQYDVQQGEwJGU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mNvbS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZ\n" \
42+
"aHR0cDovL29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAWGf9\n" \
43+
"crJq13xhlhl+2UNG0SZ9yFP6ZrBrLafTqlb3OojQO3LJUP33WbKqaPWMcwO7lWUX\n" \
44+
"zi8c3ZgTopHJ7qFAbjyY1lzzsiI8Le4bpOHeICQW8owRc5E69vrOJAKHypPstLbI\n" \
45+
"FhfFcvwnQPYT/pOmnVHvPCvYd1ebjGU6NSU2t7WKY28HJ5OxYI2A25bUeo8tqxyI\n" \
46+
"yW5+1mUfr13KFj8oRtygNeX56eXVlogMT8a3d2dIhCe2H7Bo26y/d7CQuKLJHDJd\n" \
47+
"ArolQ4FCR7vY4Y8MDEZf7kYzawMUgtN+zY+vkNaOJH1AQrRqahfGlZfh8jjNp+20\n" \
48+
"J0CT33KpuMZmYzc4ZCIwojvxuch7yPspOqsactIGEk72gtQjbz7Dk+XYtsDe3CMW\n" \
49+
"1hMwt6CaDixVBgBwAc/qOR2A24j3pSC4W/0xJmmPLQphgzpHphNULB7j7UTKvGof\n" \
50+
"KA5R2d4On3XNDgOVyvnFqSot/kGkoUeuDcL5OWYzSlvhhChZbH2UF3bkRYKtcCD9\n" \
51+
"0m9jqNf6oDP6N8v3smWe2lBvP+Sn845dWDKXcCMu5/3EFZucJ48y7RetWIExKREa\n" \
52+
"m9T8bJUox04FB6b9HbwZ4ui3uRGKLXASUoWNjDNKD/yZkuBjcNqllEdjB+dYxzFf\n" \
53+
"BT02Vf6Dsuimrdfp5gJ0iHRc2jTbkNJtUQoj1iM=\n" \
54+
"-----END CERTIFICATE-----\n";
55+
56+
// Not sure if WiFiClientSecure checks the validity date of the certificate.
57+
// Setting clock just to be sure...
58+
void setClock() {
59+
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
60+
61+
Serial.print(F("Waiting for NTP time sync: "));
62+
time_t nowSecs = time(nullptr);
63+
while (nowSecs < 8 * 3600 * 2) {
64+
delay(500);
65+
Serial.print(F("."));
66+
yield();
67+
nowSecs = time(nullptr);
68+
}
69+
70+
Serial.println();
71+
struct tm timeinfo;
72+
gmtime_r(&nowSecs, &timeinfo);
73+
Serial.print(F("Current time: "));
74+
Serial.print(asctime(&timeinfo));
75+
}
76+
77+
78+
WiFiMulti WiFiMulti;
79+
80+
void setup() {
81+
82+
Serial.begin(115200);
83+
// Serial.setDebugOutput(true);
84+
85+
Serial.println();
86+
Serial.println();
87+
Serial.println();
88+
89+
WiFi.mode(WIFI_STA);
90+
WiFiMulti.addAP("SSID", "PASSWORD");
91+
92+
// wait for WiFi connection
93+
Serial.print("Waiting for WiFi to connect...");
94+
while ((WiFiMulti.run() != WL_CONNECTED)) {
95+
Serial.print(".");
96+
}
97+
Serial.println(" connected");
98+
99+
setClock();
100+
}
101+
102+
void loop() {
103+
WiFiClientSecure *client = new WiFiClientSecure;
104+
if(client) {
105+
client -> setCACert(rootCACertificate);
106+
107+
{
108+
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
109+
HTTPClient https;
110+
111+
Serial.print("[HTTPS] begin...\n");
112+
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
113+
Serial.print("[HTTPS] GET...\n");
114+
// start connection and send HTTP header
115+
int httpCode = https.GET();
116+
117+
// httpCode will be negative on error
118+
if (httpCode > 0) {
119+
// HTTP header has been send and Server response header has been handled
120+
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
121+
122+
// file found at server
123+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
124+
String payload = https.getString();
125+
Serial.println(payload);
126+
}
127+
} else {
128+
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
129+
}
130+
131+
https.end();
132+
} else {
133+
Serial.printf("[HTTPS] Unable to connect\n");
134+
}
135+
136+
// End extra scoping block
137+
}
138+
139+
delete client;
140+
} else {
141+
Serial.println("Unable to create client");
142+
}
143+
144+
Serial.println();
145+
Serial.println("Waiting 10s before the next round...");
146+
delay(10000);
147+
}

Diff for: libraries/HTTPClient/library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=HTTPClient
2-
version=1.1
2+
version=1.2
33
author=Markus Sattler
44
maintainer=Markus Sattler
55
sentence=http Client for ESP32

0 commit comments

Comments
 (0)