Skip to content

Commit 51a4432

Browse files
copercinime-no-dev
authored andcommitted
HTTPClient Port (#347)
* Fix possible infinite loop in the example * Remove workaround of sockets always return -76 Remove workaround of sockets always return -76 (because it's fixed on IDF now) Remove delay during handshake (improving stability) * Remove unusable mbedtls_net of context creation * Fix bad destructor * Compatibility with WiFiClient for HTTPClient * Initial port from ESP8266 Changed SHA1 fingerprint by Root CA verification Changed log system * Remove deprecated function
1 parent 05a3de2 commit 51a4432

File tree

12 files changed

+1745
-54
lines changed

12 files changed

+1745
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Authorization.ino
3+
*
4+
* Created on: 09.12.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
WiFiMulti wifiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
23+
USE_SERIAL.println();
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
27+
for(uint8_t t = 4; t > 0; t--) {
28+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
29+
USE_SERIAL.flush();
30+
delay(1000);
31+
}
32+
33+
wifiMulti.addAP("SSID", "PASSWORD");
34+
35+
}
36+
37+
void loop() {
38+
// wait for WiFi connection
39+
if((wifiMulti.run() == WL_CONNECTED)) {
40+
41+
HTTPClient http;
42+
43+
USE_SERIAL.print("[HTTP] begin...\n");
44+
// configure traged server and url
45+
46+
47+
http.begin("http://user:[email protected]/test.html");
48+
49+
/*
50+
// or
51+
http.begin("http://192.168.1.12/test.html");
52+
http.setAuthorization("user", "password");
53+
54+
// or
55+
http.begin("http://192.168.1.12/test.html");
56+
http.setAuthorization("dXNlcjpwYXN3b3Jk");
57+
*/
58+
59+
60+
USE_SERIAL.print("[HTTP] GET...\n");
61+
// start connection and send HTTP header
62+
int httpCode = http.GET();
63+
64+
// httpCode will be negative on error
65+
if(httpCode > 0) {
66+
// HTTP header has been send and Server response header has been handled
67+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
68+
69+
// file found at server
70+
if(httpCode == HTTP_CODE_OK) {
71+
String payload = http.getString();
72+
USE_SERIAL.println(payload);
73+
}
74+
} else {
75+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
76+
}
77+
78+
http.end();
79+
}
80+
81+
delay(10000);
82+
}
83+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* BasicHTTPClient.ino
3+
*
4+
* Created on: 24.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
WiFiMulti wifiMulti;
18+
19+
/*
20+
const char* ca = \
21+
"-----BEGIN CERTIFICATE-----\n" \
22+
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
23+
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
24+
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
25+
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
26+
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
27+
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
28+
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
29+
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s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qMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
45+
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
46+
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
47+
"-----END CERTIFICATE-----\n";
48+
*/
49+
50+
void setup() {
51+
52+
USE_SERIAL.begin(115200);
53+
54+
USE_SERIAL.println();
55+
USE_SERIAL.println();
56+
USE_SERIAL.println();
57+
58+
for(uint8_t t = 4; t > 0; t--) {
59+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
60+
USE_SERIAL.flush();
61+
delay(1000);
62+
}
63+
64+
wifiMulti.addAP("SSID", "PASSWORD");
65+
66+
}
67+
68+
void loop() {
69+
// wait for WiFi connection
70+
if((wifiMulti.run() == WL_CONNECTED)) {
71+
72+
HTTPClient http;
73+
74+
USE_SERIAL.print("[HTTP] begin...\n");
75+
// configure traged server and url
76+
//http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
77+
http.begin("http://example.com/index.html"); //HTTP
78+
79+
USE_SERIAL.print("[HTTP] GET...\n");
80+
// start connection and send HTTP header
81+
int httpCode = http.GET();
82+
83+
// httpCode will be negative on error
84+
if(httpCode > 0) {
85+
// HTTP header has been send and Server response header has been handled
86+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
87+
88+
// file found at server
89+
if(httpCode == HTTP_CODE_OK) {
90+
String payload = http.getString();
91+
USE_SERIAL.println(payload);
92+
}
93+
} else {
94+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
95+
}
96+
97+
http.end();
98+
}
99+
100+
delay(5000);
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* reuseConnection.ino
3+
*
4+
* Created on: 22.11.2015
5+
*
6+
*/
7+
8+
9+
#include <Arduino.h>
10+
11+
#include <WiFi.h>
12+
#include <WiFiMulti.h>
13+
14+
#include <HTTPClient.h>
15+
16+
#define USE_SERIAL Serial
17+
18+
WiFiMulti wifiMulti;
19+
20+
HTTPClient http;
21+
22+
void setup() {
23+
24+
USE_SERIAL.begin(115200);
25+
26+
USE_SERIAL.println();
27+
USE_SERIAL.println();
28+
USE_SERIAL.println();
29+
30+
for(uint8_t t = 4; t > 0; t--) {
31+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
32+
USE_SERIAL.flush();
33+
delay(1000);
34+
}
35+
36+
wifiMulti.addAP("SSID", "PASSWORD");
37+
38+
// allow reuse (if server supports it)
39+
http.setReuse(true);
40+
}
41+
42+
void loop() {
43+
// wait for WiFi connection
44+
if((wifiMulti.run() == WL_CONNECTED)) {
45+
46+
http.begin("http://192.168.1.12/test.html");
47+
//http.begin("192.168.1.12", 80, "/test.html");
48+
49+
int httpCode = http.GET();
50+
if(httpCode > 0) {
51+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
52+
53+
// file found at server
54+
if(httpCode == HTTP_CODE_OK) {
55+
http.writeToStream(&USE_SERIAL);
56+
}
57+
} else {
58+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
59+
}
60+
61+
http.end();
62+
}
63+
64+
delay(1000);
65+
}
66+
67+
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* StreamHTTPClient.ino
3+
*
4+
* Created on: 24.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <WiFi.h>
11+
#include <WiFiMulti.h>
12+
13+
#include <HTTPClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
WiFiMulti wifiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
23+
USE_SERIAL.println();
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
27+
for(uint8_t t = 4; t > 0; t--) {
28+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
29+
USE_SERIAL.flush();
30+
delay(1000);
31+
}
32+
33+
wifiMulti.addAP("SSID", "PASSWORD");
34+
35+
}
36+
37+
void loop() {
38+
// wait for WiFi connection
39+
if((wifiMulti.run() == WL_CONNECTED)) {
40+
41+
HTTPClient http;
42+
43+
USE_SERIAL.print("[HTTP] begin...\n");
44+
45+
// configure server and url
46+
http.begin("http://192.168.1.12/test.html");
47+
//http.begin("192.168.1.12", 80, "/test.html");
48+
49+
USE_SERIAL.print("[HTTP] GET...\n");
50+
// start connection and send HTTP header
51+
int httpCode = http.GET();
52+
if(httpCode > 0) {
53+
// HTTP header has been send and Server response header has been handled
54+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
55+
56+
// file found at server
57+
if(httpCode == HTTP_CODE_OK) {
58+
59+
// get lenght of document (is -1 when Server sends no Content-Length header)
60+
int len = http.getSize();
61+
62+
// create buffer for read
63+
uint8_t buff[128] = { 0 };
64+
65+
// get tcp stream
66+
WiFiClient * stream = http.getStreamPtr();
67+
68+
// read all data from server
69+
while(http.connected() && (len > 0 || len == -1)) {
70+
// get available data size
71+
size_t size = stream->available();
72+
73+
if(size) {
74+
// read up to 128 byte
75+
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
76+
77+
// write it to Serial
78+
USE_SERIAL.write(buff, c);
79+
80+
if(len > 0) {
81+
len -= c;
82+
}
83+
}
84+
delay(1);
85+
}
86+
87+
USE_SERIAL.println();
88+
USE_SERIAL.print("[HTTP] connection closed or file end.\n");
89+
90+
}
91+
} else {
92+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
93+
}
94+
95+
http.end();
96+
}
97+
98+
delay(10000);
99+
}
100+

Diff for: libraries/HTTPClient/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=HTTPClient
2+
version=1.1
3+
author=Markus Sattler
4+
maintainer=Markus Sattler
5+
sentence=http Client for ESP32
6+
paragraph=
7+
category=Communication
8+
url=https://github.com/Links2004/Arduino/tree/libraries/ESP8266HTTPClient
9+
architectures=esp32

0 commit comments

Comments
 (0)