forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request esp8266#1081 from Links2004/httpClient
Http client class
- Loading branch information
Showing
10 changed files
with
980 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
libraries/ESP8266httpClient/examples/BasicHttpClient/BasicHttpClient.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
98 changes: 98 additions & 0 deletions
98
libraries/ESP8266httpClient/examples/StreamHttpClient/StreamHttpClient.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
65 changes: 65 additions & 0 deletions
65
libraries/ESP8266httpClient/examples/reuseConnection/reuseConnection.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.