From 03674799403c91e381fbd6b688cdadfd71fb4604 Mon Sep 17 00:00:00 2001 From: James Marlowe Date: Mon, 21 Nov 2016 18:01:59 -0600 Subject: [PATCH 1/4] POST http client example --- .../PostHttpClient/PostHttpClient.ino | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino new file mode 100644 index 0000000000..bce7a42ffc --- /dev/null +++ b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino @@ -0,0 +1,71 @@ +/** + * PostHTTPClient.ino + * + * Created on: 21.11.2016 + * + */ + +#include + +#include +#include + +#include + +#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("http://192.168.1.12/post/"); //HTTP + http.addHeader("Content-Type", "application/json"); + + USE_SERIAL.print("[HTTP] POST...\n"); + // start connection and send HTTP header and body + int httpCode = http.POST("{\"hello\":\"world\"}"); + + // httpCode will be negative on error + if(httpCode > 0) { + // HTTP header has been send and Server response header has been handled + USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode); + + // file found at server + if(httpCode == HTTP_CODE_OK) { + String payload = http.getString(); + USE_SERIAL.println(payload); + } + } else { + USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); + } + + delay(10000); +} From 841d593bf42a0bd6b7e8ed9550a504a55f32a19d Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 30 Nov 2019 00:36:38 +0100 Subject: [PATCH 2/4] avoid deprecated api --- .../examples/PostHttpClient/PostHttpClient.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino index bce7a42ffc..320b0bc754 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino @@ -39,11 +39,12 @@ void loop() { // wait for WiFi connection if((WiFiMulti.run() == WL_CONNECTED)) { + WiFiClient client; HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); // configure traged server and url - http.begin("http://192.168.1.12/post/"); //HTTP + http.begin(client, "http://192.168.1.12/post/"); //HTTP http.addHeader("Content-Type", "application/json"); USE_SERIAL.print("[HTTP] POST...\n"); From 85bdc6ebca68f6b94ac464a95b5a1ad0af78be5b Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 2 Dec 2019 23:56:23 +0100 Subject: [PATCH 3/4] Update PostHttpClient.ino style, comments --- .../PostHttpClient/PostHttpClient.ino | 114 ++++++++++-------- 1 file changed, 63 insertions(+), 51 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino index 320b0bc754..f8e0b7ece2 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino @@ -1,72 +1,84 @@ /** - * PostHTTPClient.ino - * - * Created on: 21.11.2016 - * - */ + PostHTTPClient.ino -#include + Created on: 21.11.2016 -#include -#include +*/ +#include #include #define USE_SERIAL Serial -ESP8266WiFiMulti WiFiMulti; +/* this can be run with an emulated server on host: + cd esp8266-core-root-dir + cd tests/host + make ../../libraries/ESP8266WebServer/examples/PostServer/PostServer + bin/PostServer/PostServer + then put your PC's IP address in SERVER_IP below, port 9080 (instead of default 80): +*/ +//#define SERVER_IP "10.0.1.7:9080" // PC address with emulation on host +#define SERVER_IP "192.168.1.42" + +#ifndef STASSID +#define STASSID "your-ssid" +#define STAPSK "your-password" +#endif void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + USE_SERIAL.begin(115200); - 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--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); - } + WiFi.begin(STASSID, STAPSK); - WiFiMulti.addAP("SSID", "PASSWORD"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + USE_SERIAL.print("."); + } + USE_SERIAL.println(""); + USE_SERIAL.print("Connected! IP address: "); + USE_SERIAL.println(WiFi.localIP()); } void loop() { - // wait for WiFi connection - if((WiFiMulti.run() == WL_CONNECTED)) { - - WiFiClient client; - HTTPClient http; - - USE_SERIAL.print("[HTTP] begin...\n"); - // configure traged server and url - http.begin(client, "http://192.168.1.12/post/"); //HTTP - http.addHeader("Content-Type", "application/json"); - - USE_SERIAL.print("[HTTP] POST...\n"); - // start connection and send HTTP header and body - int httpCode = http.POST("{\"hello\":\"world\"}"); - - // httpCode will be negative on error - if(httpCode > 0) { - // HTTP header has been send and Server response header has been handled - USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode); - - // file found at server - if(httpCode == HTTP_CODE_OK) { - String payload = http.getString(); - USE_SERIAL.println(payload); - } - } else { - USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); - } - - http.end(); + // wait for WiFi connection + if ((WiFiMulti.run() == WL_CONNECTED)) { + + WiFiClient client; + HTTPClient http; + + USE_SERIAL.print("[HTTP] begin...\n"); + // configure traged server and url + http.begin(client, "http://" SERVER_IP "/postplain/"); //HTTP + http.addHeader("Content-Type", "application/json"); + + USE_SERIAL.print("[HTTP] POST...\n"); + // start connection and send HTTP header and body + int httpCode = http.POST("{\"hello\":\"world\"}"); + + // httpCode will be negative on error + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode); + + // file found at server + if (httpCode == HTTP_CODE_OK) { + const String& payload = http.getString(); + USE_SERIAL.println("received payload:\n<<"); + USE_SERIAL.println(payload); + USE_SERIAL.println(">>"); + } + } else { + USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); } - delay(10000); + http.end(); + } + + delay(10000); } From 92a357c2521c925efdcb57159cb158fbbb0dd7ba Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 8 Dec 2019 22:52:00 +0100 Subject: [PATCH 4/4] Update PostHttpClient.ino --- .../examples/PostHttpClient/PostHttpClient.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino index f8e0b7ece2..94d57bdbf4 100644 --- a/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/PostHttpClient/PostHttpClient.ino @@ -47,7 +47,7 @@ void setup() { void loop() { // wait for WiFi connection - if ((WiFiMulti.run() == WL_CONNECTED)) { + if ((WiFi.status() == WL_CONNECTED)) { WiFiClient client; HTTPClient http;