Skip to content

Commit 56a3733

Browse files
committed
Merge pull request #1081 from Links2004/httpClient
Http client class
2 parents f1a8287 + 9089448 commit 56a3733

File tree

10 files changed

+980
-1
lines changed

10 files changed

+980
-1
lines changed

cores/esp8266/StreamString.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
StreamString.cpp
3+
4+
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#include <Arduino.h>
24+
#include "StreamString.h"
25+
26+
size_t StreamString::write(const uint8_t *buffer, size_t size) {
27+
if(reserve(length() + size + 1)) {
28+
for(size_t i = 0; i < size; i++) {
29+
if(write(*buffer)) {
30+
buffer++;
31+
} else {
32+
return i;
33+
}
34+
}
35+
36+
}
37+
return 0;
38+
}
39+
40+
size_t StreamString::write(uint8_t data) {
41+
return concat((char) data);
42+
}
43+
44+
int StreamString::available() {
45+
return length();
46+
}
47+
48+
int StreamString::read() {
49+
if(length()) {
50+
char c = charAt(0);
51+
remove(0, 1);
52+
return c;
53+
54+
}
55+
return -1;
56+
}
57+
58+
int StreamString::peek() {
59+
if(length()) {
60+
char c = charAt(0);
61+
return c;
62+
}
63+
return -1;
64+
}
65+
66+
void StreamString::flush() {
67+
}
68+

cores/esp8266/StreamString.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
StreamString.h
3+
4+
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#ifndef STREAMSTRING_H_
24+
#define STREAMSTRING_H_
25+
26+
27+
class StreamString: public Stream, public String {
28+
29+
size_t write(const uint8_t *buffer, size_t size);
30+
size_t write(uint8_t data);
31+
32+
int available();
33+
int read();
34+
int peek();
35+
void flush();
36+
37+
};
38+
39+
40+
#endif /* STREAMSTRING_H_ */

cores/esp8266/debug.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stddef.h>
55
#include <stdint.h>
66

7-
#define DEBUGV(...) ets_printf(__VA_ARGS__)
7+
//#define DEBUGV(...) ets_printf(__VA_ARGS__)
88

99
#ifndef DEBUGV
1010
#define DEBUGV(...)

libraries/ESP8266WiFi/src/include/ClientContext.h

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class ClientContext {
9797
close();
9898
if(_discard_cb)
9999
_discard_cb(_discard_cb_arg, this);
100+
DEBUGV(":del\r\n");
100101
delete this;
101102
}
102103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* BasicHttpClient.ino
3+
*
4+
* Created on: 24.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266httpClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
23+
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
28+
for(uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
33+
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
}
37+
38+
void loop() {
39+
// wait for WiFi connection
40+
if((WiFiMulti.run() == WL_CONNECTED)) {
41+
42+
httpClient http;
43+
44+
USE_SERIAL.print("[HTTP] begin...\n");
45+
// configure traged server and url
46+
//http.begin("192.168.1.12", 443, "/test.html", true, "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
47+
http.begin("192.168.1.12", 80, "/test.html"); //HTTP
48+
49+
USE_SERIAL.print("[HTTP] GET...\n");
50+
// start connection and send HTTP header
51+
int httpCode = http.GET();
52+
if(httpCode) {
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 == 200) {
58+
String payload = http.getString();
59+
USE_SERIAL.println(payload);
60+
}
61+
} else {
62+
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
63+
}
64+
}
65+
66+
delay(10000);
67+
}
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* StreamHttpClient.ino
3+
*
4+
* Created on: 24.05.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266httpClient.h>
14+
15+
#define USE_SERIAL Serial
16+
17+
ESP8266WiFiMulti WiFiMulti;
18+
19+
void setup() {
20+
21+
USE_SERIAL.begin(115200);
22+
// USE_SERIAL.setDebugOutput(true);
23+
24+
USE_SERIAL.println();
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
28+
for(uint8_t t = 4; t > 0; t--) {
29+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30+
USE_SERIAL.flush();
31+
delay(1000);
32+
}
33+
34+
WiFiMulti.addAP("SSID", "PASSWORD");
35+
36+
}
37+
38+
void loop() {
39+
// wait for WiFi connection
40+
if((WiFiMulti.run() == WL_CONNECTED)) {
41+
42+
httpClient http;
43+
44+
USE_SERIAL.print("[HTTP] begin...\n");
45+
// configure traged server and url
46+
http.begin("192.168.1.12", 80, "/test.html");
47+
48+
USE_SERIAL.print("[HTTP] GET...\n");
49+
// start connection and send HTTP header
50+
int httpCode = http.GET();
51+
if(httpCode) {
52+
// HTTP header has been send and Server response header has been handled
53+
54+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
55+
56+
// file found at server
57+
if(httpCode == 200) {
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.print("[HTTP] GET... failed, no connection or no HTTP server\n");
93+
}
94+
}
95+
96+
delay(10000);
97+
}
98+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* reuseConnection.ino
3+
*
4+
* Created on: 22.11.2015
5+
*
6+
*/
7+
8+
9+
#include <Arduino.h>
10+
11+
#include <ESP8266WiFi.h>
12+
#include <ESP8266WiFiMulti.h>
13+
14+
#include <ESP8266httpClient.h>
15+
16+
#define USE_SERIAL Serial
17+
18+
ESP8266WiFiMulti WiFiMulti;
19+
20+
httpClient http;
21+
22+
void setup() {
23+
24+
USE_SERIAL.begin(115200);
25+
// USE_SERIAL.setDebugOutput(true);
26+
27+
USE_SERIAL.println();
28+
USE_SERIAL.println();
29+
USE_SERIAL.println();
30+
31+
for(uint8_t t = 4; t > 0; t--) {
32+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
33+
USE_SERIAL.flush();
34+
delay(1000);
35+
}
36+
37+
WiFiMulti.addAP("SSID", "PASSWORD");
38+
39+
40+
}
41+
42+
void loop() {
43+
// wait for WiFi connection
44+
if((WiFiMulti.run() == WL_CONNECTED)) {
45+
46+
http.begin("192.168.1.12", 80, "/test.html");
47+
48+
int httpCode = http.GET();
49+
if(httpCode) {
50+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
51+
52+
// file found at server
53+
if(httpCode == 200) {
54+
http.writeToStream(&USE_SERIAL);
55+
}
56+
} else {
57+
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
58+
}
59+
}
60+
61+
delay(1000);
62+
}
63+
64+
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ESP8266httpClient
2+
version=1.0
3+
author=Markus Sattler
4+
maintainer=Markus Sattler
5+
sentence=http Client for ESP8266
6+
paragraph=
7+
category=Communication
8+
url=https://github.com/Links2004/Arduino/tree/libraries/ESP8266httpClient
9+
architectures=esp8266

0 commit comments

Comments
 (0)