Skip to content

Commit 2214640

Browse files
Add HTTPClient, ported from the ESP8266
Remove the need to have a separate WiFiClient that's destroyed after the HTTPClient. Let the object handle its own client, and pass through any SSL requests.
1 parent a2465f5 commit 2214640

File tree

25 files changed

+2937
-5
lines changed

25 files changed

+2937
-5
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
Authorization.ino
3+
4+
Created on: 09.12.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
12+
#ifndef STASSID
13+
#define STASSID "your-ssid"
14+
#define STAPSK "your-password"
15+
#endif
16+
17+
const char *ssid = STASSID;
18+
const char *pass = STAPSK;
19+
20+
WiFiMulti WiFiMulti;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println();
30+
31+
for (uint8_t t = 4; t > 0; t--) {
32+
Serial.printf("[SETUP] WAIT %d...\n", t);
33+
Serial.flush();
34+
delay(1000);
35+
}
36+
37+
WiFi.mode(WIFI_STA);
38+
WiFiMulti.addAP(ssid, pass);
39+
}
40+
41+
void loop() {
42+
// wait for WiFi connection
43+
if ((WiFiMulti.run() == WL_CONNECTED)) {
44+
45+
HTTPClient http;
46+
http.setInsecure();
47+
48+
Serial.print("[HTTP] begin...\n");
49+
// configure traged server and url
50+
51+
52+
http.begin("https://guest:[email protected]/HTTP/Basic/");
53+
54+
/*
55+
// or
56+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
57+
http.setAuthorization("guest", "guest");
58+
59+
// or
60+
http.begin(client, "http://jigsaw.w3.org/HTTP/Basic/");
61+
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
62+
*/
63+
64+
65+
Serial.print("[HTTP] GET...\n");
66+
// start connection and send HTTP header
67+
int httpCode = http.GET();
68+
69+
// httpCode will be negative on error
70+
if (httpCode > 0) {
71+
// HTTP header has been send and Server response header has been handled
72+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
73+
74+
// file found at server
75+
if (httpCode == HTTP_CODE_OK) {
76+
String payload = http.getString();
77+
Serial.println(payload);
78+
}
79+
} else {
80+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
81+
}
82+
83+
http.end();
84+
}
85+
86+
delay(10000);
87+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
BasicHTTPClient.ino
3+
4+
Created on: 24.05.2015
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
12+
#ifndef STASSID
13+
#define STASSID "your-ssid"
14+
#define STAPSK "your-password"
15+
#endif
16+
17+
const char *ssid = STASSID;
18+
const char *pass = STAPSK;
19+
20+
WiFiMulti WiFiMulti;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println();
30+
31+
for (uint8_t t = 4; t > 0; t--) {
32+
Serial.printf("[SETUP] WAIT %d...\n", t);
33+
Serial.flush();
34+
delay(1000);
35+
}
36+
37+
WiFiMulti.addAP(ssid, pass);
38+
}
39+
40+
void loop() {
41+
// wait for WiFi connection
42+
if ((WiFiMulti.run() == WL_CONNECTED)) {
43+
44+
HTTPClient http;
45+
46+
Serial.print("[HTTP] begin...\n");
47+
if (http.begin("http://httpbin.org")) { // HTTP
48+
49+
50+
Serial.print("[HTTP] GET...\n");
51+
// start connection and send HTTP header
52+
int httpCode = http.GET();
53+
54+
// httpCode will be negative on error
55+
if (httpCode > 0) {
56+
// HTTP header has been send and Server response header has been handled
57+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
58+
59+
// file found at server
60+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
61+
String payload = http.getString();
62+
Serial.println(payload);
63+
}
64+
} else {
65+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
66+
}
67+
68+
http.end();
69+
} else {
70+
Serial.printf("[HTTP} Unable to connect\n");
71+
}
72+
}
73+
74+
delay(10000);
75+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
BasicHTTPSClient.ino
3+
4+
Created on: 20.08.2018
5+
6+
*/
7+
8+
#include <Arduino.h>
9+
#include <WiFi.h>
10+
#include <HTTPClient.h>
11+
12+
#ifndef STASSID
13+
#define STASSID "your-ssid"
14+
#define STAPSK "your-password"
15+
#endif
16+
17+
const char *ssid = STASSID;
18+
const char *pass = STAPSK;
19+
20+
WiFiMulti WiFiMulti;
21+
22+
void setup() {
23+
24+
Serial.begin(115200);
25+
// Serial.setDebugOutput(true);
26+
27+
Serial.println();
28+
Serial.println();
29+
Serial.println();
30+
31+
for (uint8_t t = 4; t > 0; t--) {
32+
Serial.printf("[SETUP] WAIT %d...\n", t);
33+
Serial.flush();
34+
delay(1000);
35+
}
36+
37+
WiFi.mode(WIFI_STA);
38+
WiFiMulti.addAP(ssid, pass);
39+
}
40+
41+
const char *jigsaw_cert = R"EOF(
42+
-----BEGIN CERTIFICATE-----
43+
MIIFKTCCBM+gAwIBAgIQAbTKhAICxb7iDJbE6qU/NzAKBggqhkjOPQQDAjBKMQsw
44+
CQYDVQQGEwJVUzEZMBcGA1UEChMQQ2xvdWRmbGFyZSwgSW5jLjEgMB4GA1UEAxMX
45+
Q2xvdWRmbGFyZSBJbmMgRUNDIENBLTMwHhcNMjIwMzE3MDAwMDAwWhcNMjMwMzE2
46+
MjM1OTU5WjB1MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQG
47+
A1UEBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQQ2xvdWRmbGFyZSwgSW5jLjEe
48+
MBwGA1UEAxMVc25pLmNsb3Vk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/sWxX
64+
vw+tTG1Cy7u2JyAmUeo/4SrvqAPDO9ZMAAABf5oU+GEAAAQDAEgwRgIhANKeTNMy
65+
GqUsCo7ph7YMWzrhMuDeyP8xPSiCtFzKcn/eAiEAyv5lgCUQ6K14V13zYfL99wZD
66+
LFcIP/KZ1y7nuPAksTAAdwCzc3cH4YRQ+GOG1gWp3BEJSnktsWcMC4fc8AMOeTal
67+
mgAAAX+aFPiWAAAEAwBIMEYCIQD6535jWw776D4vjyupP2fBw26CBMpVT5++k4rR
68+
xqeOXwIhAIbEaEKkEq6JtpWWfVpTyDkMpMfTuiqYVe6REy2XsmEhMAoGCCqGSM49
69+
BAMCA0gAMEUCIH3r/puXZcX1bfUoBq2njuHe0bxWtvzDaz5k6WLYrazTAiEA+ePL
70+
N6K5xrmaof185pVCxACPLc/BoKyUwMeC8iXCm00=
71+
-----END CERTIFICATE-----
72+
)EOF";
73+
74+
static int cnt = 0;
75+
76+
void loop() {
77+
// wait for WiFi connection
78+
if ((WiFiMulti.run() == WL_CONNECTED)) {
79+
HTTPClient https;
80+
switch (cnt) {
81+
case 0:
82+
Serial.println("[HTTPS] using insecure SSL, not validating certificate");
83+
https.setInsecure(); // Note this is unsafe against MITM attacks
84+
cnt++;
85+
break;
86+
case 1:
87+
Serial.println("[HTTPS] using secure SSL, validating certificate");
88+
https.setCACert(jigsaw_cert);
89+
cnt++;
90+
break;
91+
default:
92+
Serial.println("[HTTPS] not setting any SSL verification settings, will fail");
93+
cnt = 0;
94+
}
95+
96+
Serial.print("[HTTPS] begin...\n");
97+
if (https.begin("https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
98+
99+
Serial.print("[HTTPS] GET...\n");
100+
// start connection and send HTTP header
101+
int httpCode = https.GET();
102+
103+
// httpCode will be negative on error
104+
if (httpCode > 0) {
105+
// HTTP header has been send and Server response header has been handled
106+
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
107+
108+
// file found at server
109+
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
110+
String payload = https.getString();
111+
Serial.println(payload);
112+
}
113+
} else {
114+
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
115+
}
116+
117+
https.end();
118+
} else {
119+
Serial.printf("[HTTPS] Unable to connect\n");
120+
}
121+
}
122+
123+
Serial.println("Wait 10s before next round...");
124+
delay(10000);
125+
}

0 commit comments

Comments
 (0)