Skip to content

Http client class #1081

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 24 commits into from
Nov 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
95dada1
first httpClient
Links2004 Nov 16, 2015
70ca494
add Response header handling
Links2004 Nov 17, 2015
a359992
Merge remote-tracking branch 'remotes/esp8266/master' into httpClient
Links2004 Nov 17, 2015
37015f3
Merge branch 'master' into httpClient
Links2004 Nov 17, 2015
5e1a656
Merge remote-tracking branch 'remotes/esp8266/master' into httpClient
Links2004 Nov 17, 2015
1e7b968
add examples/BasicHttpClient/BasicHttpClient.ino
Links2004 Nov 22, 2015
0a8f5be
add more documentation and cleanup the example
Links2004 Nov 22, 2015
3b24638
Merge remote-tracking branch 'remotes/esp8266/master' into httpClient
Links2004 Nov 22, 2015
464b9f2
improve error handling
Links2004 Nov 22, 2015
ca092f4
fix warnings
Links2004 Nov 22, 2015
e6c661e
allow reuse of tcp connection to send multiple request to one server
Links2004 Nov 22, 2015
be91d96
improve debug out and error handling
Links2004 Nov 22, 2015
6ed7dfe
improve handling of non http servers
Links2004 Nov 22, 2015
497ab25
fix some memory leek
Links2004 Nov 22, 2015
a5aa33f
disable DEBUGV
Links2004 Nov 22, 2015
c8aac83
add :del message to unref
Links2004 Nov 22, 2015
fd19d90
Merge remote-tracking branch 'remotes/esp8266/master' into httpClient
Links2004 Nov 22, 2015
8b67051
add StreamString class (implement the Stream interface for String)
Links2004 Nov 25, 2015
e4a5250
add getString function
Links2004 Nov 25, 2015
59b4c82
add new Basic example based of getString
Links2004 Nov 25, 2015
761b73c
correct StreamHttpClient.ino example
Links2004 Nov 25, 2015
4b0e8ee
Merge remote-tracking branch 'remotes/esp8266/master' into httpClient
Links2004 Nov 25, 2015
75fb6e2
disable DEBUG_HTTPCLIENT
Links2004 Nov 25, 2015
9089448
fixed typo
Links2004 Nov 25, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions cores/esp8266/StreamString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
StreamString.cpp

Copyright (c) 2015 Markus Sattler. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

*/

#include <Arduino.h>
#include "StreamString.h"

size_t StreamString::write(const uint8_t *buffer, size_t size) {
if(reserve(length() + size + 1)) {
for(size_t i = 0; i < size; i++) {
if(write(*buffer)) {
buffer++;
} else {
return i;
}
}

}
return 0;
}

size_t StreamString::write(uint8_t data) {
return concat((char) data);
}

int StreamString::available() {
return length();
}

int StreamString::read() {
if(length()) {
char c = charAt(0);
remove(0, 1);
return c;

}
return -1;
}

int StreamString::peek() {
if(length()) {
char c = charAt(0);
return c;
}
return -1;
}

void StreamString::flush() {
}

40 changes: 40 additions & 0 deletions cores/esp8266/StreamString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
StreamString.h

Copyright (c) 2015 Markus Sattler. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

*/

#ifndef STREAMSTRING_H_
#define STREAMSTRING_H_


class StreamString: public Stream, public String {

size_t write(const uint8_t *buffer, size_t size);
size_t write(uint8_t data);

int available();
int read();
int peek();
void flush();

};


#endif /* STREAMSTRING_H_ */
2 changes: 1 addition & 1 deletion cores/esp8266/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stddef.h>
#include <stdint.h>

#define DEBUGV(...) ets_printf(__VA_ARGS__)
//#define DEBUGV(...) ets_printf(__VA_ARGS__)

#ifndef DEBUGV
#define DEBUGV(...)
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ClientContext {
close();
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
DEBUGV(":del\r\n");
delete this;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* BasicHttpClient.ino
*
* Created on: 24.05.2015
*
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266httpClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}

WiFiMulti.addAP("SSID", "PASSWORD");

}

void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {

httpClient http;

USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//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
http.begin("192.168.1.12", 80, "/test.html"); //HTTP

USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if(httpCode) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if(httpCode == 200) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}

delay(10000);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* StreamHttpClient.ino
*
* Created on: 24.05.2015
*
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266httpClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}

WiFiMulti.addAP("SSID", "PASSWORD");

}

void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {

httpClient http;

USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("192.168.1.12", 80, "/test.html");

USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if(httpCode) {
// HTTP header has been send and Server response header has been handled

USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if(httpCode == 200) {

// get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize();

// create buffer for read
uint8_t buff[128] = { 0 };

// get tcp stream
WiFiClient * stream = http.getStreamPtr();

// read all data from server
while(http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();

if(size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

// write it to Serial
USE_SERIAL.write(buff, c);

if(len > 0) {
len -= c;
}
}
delay(1);
}

USE_SERIAL.println();
USE_SERIAL.print("[HTTP] connection closed or file end.\n");

}
} else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}

delay(10000);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* reuseConnection.ino
*
* Created on: 22.11.2015
*
*/


#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266httpClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

httpClient http;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}

WiFiMulti.addAP("SSID", "PASSWORD");


}

void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {

http.begin("192.168.1.12", 80, "/test.html");

int httpCode = http.GET();
if(httpCode) {
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if(httpCode == 200) {
http.writeToStream(&USE_SERIAL);
}
} else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}

delay(1000);
}



9 changes: 9 additions & 0 deletions libraries/ESP8266httpClient/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=ESP8266httpClient
version=1.0
author=Markus Sattler
maintainer=Markus Sattler
sentence=http Client for ESP8266
paragraph=
category=Communication
url=https://github.com/Links2004/Arduino/tree/libraries/ESP8266httpClient
architectures=esp8266
Loading