1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Rename ESP8266httpClient to ESP8266HTTPClient (part 1)

This commit is contained in:
Ivan Grokhotkov 2015-11-30 09:48:07 +03:00
parent abd98f1ff8
commit 14f67c4ebc
9 changed files with 1035 additions and 1035 deletions

View File

@ -1,68 +1,68 @@
/** /**
* BasicHttpClient.ino * BasicHTTPClient.ino
* *
* Created on: 24.05.2015 * Created on: 24.05.2015
* *
*/ */
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266httpClient.h> #include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial #define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti; ESP8266WiFiMulti WiFiMulti;
void setup() { void setup() {
USE_SERIAL.begin(115200); USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true); // USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) { for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush(); USE_SERIAL.flush();
delay(1000); delay(1000);
} }
WiFiMulti.addAP("SSID", "PASSWORD"); WiFiMulti.addAP("SSID", "PASSWORD");
} }
void loop() { void loop() {
// wait for WiFi connection // wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) { if((WiFiMulti.run() == WL_CONNECTED)) {
httpClient http; HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n"); USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url // 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", 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 http.begin("192.168.1.12", 80, "/test.html"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n"); USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header // start connection and send HTTP header
int httpCode = http.GET(); int httpCode = http.GET();
if(httpCode) { if(httpCode) {
// HTTP header has been send and Server response header has been handled // HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == 200) {
String payload = http.getString(); String payload = http.getString();
USE_SERIAL.println(payload); USE_SERIAL.println(payload);
} }
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
} }
delay(10000); delay(10000);
} }

View File

@ -1,65 +1,65 @@
/** /**
* reuseConnection.ino * reuseConnection.ino
* *
* Created on: 22.11.2015 * Created on: 22.11.2015
* *
*/ */
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266httpClient.h> #include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial #define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti; ESP8266WiFiMulti WiFiMulti;
httpClient http; HTTPClient http;
void setup() { void setup() {
USE_SERIAL.begin(115200); USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true); // USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) { for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush(); USE_SERIAL.flush();
delay(1000); delay(1000);
} }
WiFiMulti.addAP("SSID", "PASSWORD"); WiFiMulti.addAP("SSID", "PASSWORD");
} }
void loop() { void loop() {
// wait for WiFi connection // wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) { if((WiFiMulti.run() == WL_CONNECTED)) {
http.begin("192.168.1.12", 80, "/test.html"); http.begin("192.168.1.12", 80, "/test.html");
int httpCode = http.GET(); int httpCode = http.GET();
if(httpCode) { if(httpCode) {
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == 200) {
http.writeToStream(&USE_SERIAL); http.writeToStream(&USE_SERIAL);
} }
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
} }
delay(1000); delay(1000);
} }

View File

@ -1,98 +1,98 @@
/** /**
* StreamHttpClient.ino * StreamHTTPClient.ino
* *
* Created on: 24.05.2015 * Created on: 24.05.2015
* *
*/ */
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266httpClient.h> #include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial #define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti; ESP8266WiFiMulti WiFiMulti;
void setup() { void setup() {
USE_SERIAL.begin(115200); USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true); // USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.println(); USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) { for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush(); USE_SERIAL.flush();
delay(1000); delay(1000);
} }
WiFiMulti.addAP("SSID", "PASSWORD"); WiFiMulti.addAP("SSID", "PASSWORD");
} }
void loop() { void loop() {
// wait for WiFi connection // wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) { if((WiFiMulti.run() == WL_CONNECTED)) {
httpClient http; HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n"); USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url // configure traged server and url
http.begin("192.168.1.12", 80, "/test.html"); http.begin("192.168.1.12", 80, "/test.html");
USE_SERIAL.print("[HTTP] GET...\n"); USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header // start connection and send HTTP header
int httpCode = http.GET(); int httpCode = http.GET();
if(httpCode) { if(httpCode) {
// HTTP header has been send and Server response header has been handled // HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server // file found at server
if(httpCode == 200) { if(httpCode == 200) {
// get lenght of document (is -1 when Server sends no Content-Length header) // get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize(); int len = http.getSize();
// create buffer for read // create buffer for read
uint8_t buff[128] = { 0 }; uint8_t buff[128] = { 0 };
// get tcp stream // get tcp stream
WiFiClient * stream = http.getStreamPtr(); WiFiClient * stream = http.getStreamPtr();
// read all data from server // read all data from server
while(http.connected() && (len > 0 || len == -1)) { while(http.connected() && (len > 0 || len == -1)) {
// get available data size // get available data size
size_t size = stream->available(); size_t size = stream->available();
if(size) { if(size) {
// read up to 128 byte // read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to Serial // write it to Serial
USE_SERIAL.write(buff, c); USE_SERIAL.write(buff, c);
if(len > 0) { if(len > 0) {
len -= c; len -= c;
} }
} }
delay(1); delay(1);
} }
USE_SERIAL.println(); USE_SERIAL.println();
USE_SERIAL.print("[HTTP] connection closed or file end.\n"); USE_SERIAL.print("[HTTP] connection closed or file end.\n");
} }
} else { } else {
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n"); USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
} }
} }
delay(10000); delay(10000);
} }

View File

@ -1,9 +1,9 @@
name=ESP8266httpClient name=ESP8266HTTPClient
version=1.0 version=1.0
author=Markus Sattler author=Markus Sattler
maintainer=Markus Sattler maintainer=Markus Sattler
sentence=http Client for ESP8266 sentence=http Client for ESP8266
paragraph= paragraph=
category=Communication category=Communication
url=https://github.com/Links2004/Arduino/tree/libraries/ESP8266httpClient url=https://github.com/Links2004/Arduino/tree/libraries/ESP8266HTTPClient
architectures=esp8266 architectures=esp8266

View File

@ -1,129 +1,129 @@
/** /**
* ESP8266httpClient.h * ESP8266HTTPClient.h
* *
* Created on: 02.11.2015 * Created on: 02.11.2015
* *
* Copyright (c) 2015 Markus Sattler. All rights reserved. * Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP8266httpClient for Arduino. * This file is part of the ESP8266HTTPClient for Arduino.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * 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, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
#ifndef ESP8266HTTPCLIENT_H_ #ifndef ESP8266HTTPClient_H_
#define ESP8266HTTPCLIENT_H_ #define ESP8266HTTPClient_H_
//#define DEBUG_HTTPCLIENT(...) Serial1.printf( __VA_ARGS__ ) //#define DEBUG_HTTPClient(...) Serial1.printf( __VA_ARGS__ )
#ifndef DEBUG_HTTPCLIENT #ifndef DEBUG_HTTPClient
#define DEBUG_HTTPCLIENT(...) #define DEBUG_HTTPClient(...)
#endif #endif
#define HTTPCLIENT_TCP_TIMEOUT (1000) #define HTTPClient_TCP_TIMEOUT (1000)
/// HTTP client errors /// HTTP client errors
#define HTTPC_ERROR_CONNECTION_REFUSED (-1) #define HTTPC_ERROR_CONNECTION_REFUSED (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED (-2) #define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3) #define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
#define HTTPC_ERROR_NOT_CONNECTED (-4) #define HTTPC_ERROR_NOT_CONNECTED (-4)
#define HTTPC_ERROR_CONNECTION_LOST (-5) #define HTTPC_ERROR_CONNECTION_LOST (-5)
#define HTTPC_ERROR_NO_STREAM (-6) #define HTTPC_ERROR_NO_STREAM (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER (-7) #define HTTPC_ERROR_NO_HTTP_SERVER (-7)
class httpClient { class HTTPClient {
public: public:
httpClient(); HTTPClient();
~httpClient(); ~HTTPClient();
void begin(const char *url, const char * httpsFingerprint = ""); void begin(const char *url, const char * httpsFingerprint = "");
void begin(String url, String httpsFingerprint = ""); void begin(String url, String httpsFingerprint = "");
void begin(const char *host, uint16_t port, const char * url = "/", bool https = false, const char * httpsFingerprint = ""); void begin(const char *host, uint16_t port, const char * url = "/", bool https = false, const char * httpsFingerprint = "");
void begin(String host, uint16_t port, String url = "/", bool https = false, String httpsFingerprint = ""); void begin(String host, uint16_t port, String url = "/", bool https = false, String httpsFingerprint = "");
void end(void); void end(void);
bool connected(void); bool connected(void);
void setReuse(bool reuse); /// keep-alive void setReuse(bool reuse); /// keep-alive
void setUserAgent(const char * userAgent); void setUserAgent(const char * userAgent);
/// request handling /// request handling
int GET(); int GET();
int POST(uint8_t * payload, size_t size); int POST(uint8_t * payload, size_t size);
int POST(String payload); int POST(String payload);
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0); int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
int sendRequest(const char * type, Stream * stream, size_t size = 0); int sendRequest(const char * type, Stream * stream, size_t size = 0);
void addHeader(const String& name, const String& value, bool first = false); void addHeader(const String& name, const String& value, bool first = false);
/// Response handling /// Response handling
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
String header(const char* name); // get request header value by name String header(const char* name); // get request header value by name
String header(size_t i); // get request header value by number String header(size_t i); // get request header value by number
String headerName(size_t i); // get request header name by number String headerName(size_t i); // get request header name by number
int headers(); // get header count int headers(); // get header count
bool hasHeader(const char* name); // check if header exists bool hasHeader(const char* name); // check if header exists
int getSize(void); int getSize(void);
WiFiClient & getStream(void) __attribute__ ((deprecated)) ; WiFiClient & getStream(void) __attribute__ ((deprecated)) ;
WiFiClient * getStreamPtr(void); WiFiClient * getStreamPtr(void);
int writeToStream(Stream * stream); int writeToStream(Stream * stream);
String getString(void); String getString(void);
protected: protected:
struct RequestArgument { struct RequestArgument {
String key; String key;
String value; String value;
}; };
WiFiClient * _tcp; WiFiClient * _tcp;
WiFiClientSecure * _tcps; WiFiClientSecure * _tcps;
/// request handling /// request handling
String _host; String _host;
uint16_t _port; uint16_t _port;
bool _reuse; bool _reuse;
String _url; String _url;
bool _https; bool _https;
String _httpsFingerprint; String _httpsFingerprint;
String _Headers; String _Headers;
String _userAgent; String _userAgent;
/// Response handling /// Response handling
RequestArgument* _currentHeaders; RequestArgument* _currentHeaders;
size_t _headerKeysCount; size_t _headerKeysCount;
int _returnCode; int _returnCode;
int _size; int _size;
bool _canReuse; bool _canReuse;
bool connect(void); bool connect(void);
bool sendHeader(const char * type); bool sendHeader(const char * type);
int handleHeaderResponse(); int handleHeaderResponse();
}; };
#endif /* ESP8266HTTPCLIENT_H_ */ #endif /* ESP8266HTTPClient_H_ */

View File

@ -10,7 +10,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266httpClient.h> #include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h> #include <ESP8266httpUpdate.h>
#define USE_SERIAL Serial #define USE_SERIAL Serial

View File

@ -41,7 +41,7 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
* @return t_httpUpdate_return * @return t_httpUpdate_return
*/ */
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint) { t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint) {
httpClient http; HTTPClient http;
http.begin(url, httpsFingerprint); http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version); return handleUpdate(&http, current_version);
} }
@ -56,24 +56,24 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * cur
* @return * @return
*/ */
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint) { t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint) {
httpClient http; HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint); http.begin(host, port, url, https, httpsFingerprint);
return handleUpdate(&http, current_version); return handleUpdate(&http, current_version);
} }
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint) { t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint) {
httpClient http; HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint); http.begin(host, port, url, https, httpsFingerprint);
return handleUpdate(&http, current_version.c_str()); return handleUpdate(&http, current_version.c_str());
} }
/** /**
* *
* @param http httpClient * * @param http HTTPClient *
* @param current_version const char * * @param current_version const char *
* @return t_httpUpdate_return * @return t_httpUpdate_return
*/ */
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(httpClient * http, const char * current_version) { t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version) {
t_httpUpdate_return ret = HTTP_UPDATE_FAILED; t_httpUpdate_return ret = HTTP_UPDATE_FAILED;

View File

@ -30,7 +30,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ESP8266httpClient.h> #include <ESP8266HTTPClient.h>
//#define DEBUG_HTTP_UPDATE(...) Serial1.printf( __VA_ARGS__ ) //#define DEBUG_HTTP_UPDATE(...) Serial1.printf( __VA_ARGS__ )
@ -54,7 +54,7 @@ class ESP8266HTTPUpdate {
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = ""); t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "");
protected: protected:
t_httpUpdate_return handleUpdate(httpClient * http, const char * current_version); t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version);
bool runUpdate(Stream& in, uint32_t size, String md5); bool runUpdate(Stream& in, uint32_t size, String md5);
}; };