From 5e1f1f21bf197370e850d504d2541682b20a34b8 Mon Sep 17 00:00:00 2001 From: tigoe Date: Tue, 22 Jan 2019 08:02:55 -0500 Subject: [PATCH] Updated examples and readme.md --- README.md | 1 + examples/BasicAuthGet/BasicAuthGet.ino | 11 ++++------ examples/CustomHeader/CustomHeader.ino | 21 ++++++++------------ examples/DweetGet/DweetGet.ino | 10 +++------- examples/DweetPost/DweetPost.ino | 14 +++---------- examples/HueBlink/HueBlink.ino | 6 +----- examples/PostWithHeaders/PostWithHeaders.ino | 10 ++++------ examples/SimpleDelete/SimpleDelete.ino | 9 +++------ examples/SimpleGet/SimpleGet.ino | 10 +++------- examples/SimplePost/SimplePost.ino | 10 +++------- examples/SimplePut/SimplePut.ino | 10 +++------- examples/SimpleWebSocket/SimpleWebSocket.ino | 6 +++--- 12 files changed, 39 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 655b618..9824ea0 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Derived from [Adrian McEwen's HttpClient library](https://github.com/amcewen/Htt ## Dependencies - Requires a networking hardware and a library that provides transport specific `Client` instance, such as: + - [WiFiNINA](https://github.com/arduino-libraries/WiFiNINA) - [WiFi101](https://github.com/arduino-libraries/WiFi101) - [Ethernet](https://github.com/arduino-libraries/Ethernet) - [WiFi](https://github.com/arduino-libraries/WiFi) diff --git a/examples/BasicAuthGet/BasicAuthGet.ino b/examples/BasicAuthGet/BasicAuthGet.ino index e793d47..ca801b7 100644 --- a/examples/BasicAuthGet/BasicAuthGet.ino +++ b/examples/BasicAuthGet/BasicAuthGet.ino @@ -2,12 +2,12 @@ GET client with HTTP basic authentication for ArduinoHttpClient library Connects to server once every five seconds, sends a GET request - - created 14 Feb 2016 by Tom Igoe modified 3 Jan 2017 to add HTTP basic authentication by Sandeep Mistry + modified 22 Jan 2019 + by Tom Igoe this example is in the public domain */ @@ -25,8 +25,6 @@ int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -56,8 +54,8 @@ void loop() { client.endRequest(); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); @@ -66,4 +64,3 @@ void loop() { Serial.println("Wait five seconds"); delay(5000); } - diff --git a/examples/CustomHeader/CustomHeader.ino b/examples/CustomHeader/CustomHeader.ino index fa44aca..e2131eb 100644 --- a/examples/CustomHeader/CustomHeader.ino +++ b/examples/CustomHeader/CustomHeader.ino @@ -2,18 +2,14 @@ Custom request header example for the ArduinoHttpClient library. This example sends a GET and a POST request with a custom header every 5 seconds. - note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" - and add the following variables: - char ssid[] = "ssid"; // your network SSID (name) - char pass[] = "password"; // your network password - based on SimpleGet example by Tom Igoe header modifications by Todd Treece + modified 22 Jan 2019 + by Tom Igoe this example is in the public domain - */ - +*/ + #include #include @@ -29,8 +25,6 @@ int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -53,7 +47,6 @@ void setup() { } void loop() { - Serial.println("making GET request"); client.beginRequest(); client.get("/"); @@ -61,8 +54,8 @@ void loop() { client.endRequest(); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("GET Status code: "); Serial.println(statusCode); @@ -81,6 +74,8 @@ void loop() { client.sendHeader("X-CUSTOM-HEADER", "custom_value"); client.endRequest(); client.write((const byte*)postData.c_str(), postData.length()); + // note: the above line can also be achieved with the simpler line below: + //client.print(postData); // read the status code and body of the response statusCode = client.responseStatusCode(); diff --git a/examples/DweetGet/DweetGet.ino b/examples/DweetGet/DweetGet.ino index 51abd3f..d191285 100644 --- a/examples/DweetGet/DweetGet.ino +++ b/examples/DweetGet/DweetGet.ino @@ -9,10 +9,8 @@ For more on dweet.io, see https://dweet.io/play/ - - created 15 Feb 2016 - updated 16 Feb 2016 + updated 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -33,8 +31,6 @@ String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -int statusCode = 0; -String response; void setup() { Serial.begin(9600); @@ -66,8 +62,8 @@ void loop() { client.get(path); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); Serial.print("Response: "); diff --git a/examples/DweetPost/DweetPost.ino b/examples/DweetPost/DweetPost.ino index 41c6f2f..1e2d792 100644 --- a/examples/DweetPost/DweetPost.ino +++ b/examples/DweetPost/DweetPost.ino @@ -5,13 +5,8 @@ Shows how to use Strings to assemble path and body - note: WiFi SSID and password are stored in config.h file. - If it is not present, add a new tab, call it "config.h" - and add the following variables: - char ssid[] = "ssid"; // your network SSID (name) - char pass[] = "password"; // your network password - created 15 Feb 2016 + modified 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -31,8 +26,6 @@ int port = 80; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -int statusCode = 0; -String response; void setup() { Serial.begin(9600); @@ -59,7 +52,6 @@ void loop() { // assemble the path for the POST message: String dweetName = "scandalous-cheese-hoarder"; String path = "/dweet/for/" + dweetName; - String contentType = "application/json"; // assemble the body of the POST message: @@ -74,8 +66,8 @@ void loop() { client.post(path, contentType, postData); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/HueBlink/HueBlink.ino b/examples/HueBlink/HueBlink.ino index c18ae47..eab74fe 100644 --- a/examples/HueBlink/HueBlink.ino +++ b/examples/HueBlink/HueBlink.ino @@ -13,8 +13,6 @@ This example shows how to concatenate Strings to assemble the PUT request and the body of the request. - - modified 15 Feb 2016 by Tom Igoe (tigoe) to match new API */ @@ -97,6 +95,4 @@ void sendRequest(int light, String cmd, String value) { Serial.print("Server response: "); Serial.println(response); Serial.println(); -} - - +} \ No newline at end of file diff --git a/examples/PostWithHeaders/PostWithHeaders.ino b/examples/PostWithHeaders/PostWithHeaders.ino index 835a26f..d061651 100644 --- a/examples/PostWithHeaders/PostWithHeaders.ino +++ b/examples/PostWithHeaders/PostWithHeaders.ino @@ -3,12 +3,12 @@ Connects to server once every five seconds, sends a POST request with custome headers and a request body - - created 14 Feb 2016 by Tom Igoe modified 18 Mar 2017 by Sandeep Mistry + modified 22 Jan 2019 + by Tom Igoe this example is in the public domain */ @@ -29,8 +29,6 @@ int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -66,8 +64,8 @@ void loop() { client.endRequest(); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/SimpleDelete/SimpleDelete.ino b/examples/SimpleDelete/SimpleDelete.ino index 374a145..84a802e 100644 --- a/examples/SimpleDelete/SimpleDelete.ino +++ b/examples/SimpleDelete/SimpleDelete.ino @@ -3,9 +3,8 @@ Connects to server once every five seconds, sends a DELETE request and a request body - - created 14 Feb 2016 + modified 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -27,8 +26,6 @@ int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -58,8 +55,8 @@ void loop() { client.del("/", contentType, delData); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/SimpleGet/SimpleGet.ino b/examples/SimpleGet/SimpleGet.ino index 75db9a7..845cfc6 100644 --- a/examples/SimpleGet/SimpleGet.ino +++ b/examples/SimpleGet/SimpleGet.ino @@ -2,9 +2,8 @@ Simple GET client for ArduinoHttpClient library Connects to server once every five seconds, sends a GET request - - created 14 Feb 2016 + modified 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -19,15 +18,12 @@ char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; - char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -54,8 +50,8 @@ void loop() { client.get("/"); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/SimplePost/SimplePost.ino b/examples/SimplePost/SimplePost.ino index 6cc3517..19fae75 100644 --- a/examples/SimplePost/SimplePost.ino +++ b/examples/SimplePost/SimplePost.ino @@ -3,9 +3,8 @@ Connects to server once every five seconds, sends a POST request and a request body - - created 14 Feb 2016 + modified 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -19,15 +18,12 @@ char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; - char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -57,8 +53,8 @@ void loop() { client.post("/", contentType, postData); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/SimplePut/SimplePut.ino b/examples/SimplePut/SimplePut.ino index 1b06105..82264ca 100644 --- a/examples/SimplePut/SimplePut.ino +++ b/examples/SimplePut/SimplePut.ino @@ -3,9 +3,8 @@ Connects to server once every five seconds, sends a PUT request and a request body - - created 14 Feb 2016 + modified 22 Jan 2019 by Tom Igoe this example is in the public domain @@ -19,15 +18,12 @@ char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; - char serverAddress[] = "192.168.0.3"; // server address int port = 8080; WiFiClient wifi; HttpClient client = HttpClient(wifi, serverAddress, port); int status = WL_IDLE_STATUS; -String response; -int statusCode = 0; void setup() { Serial.begin(9600); @@ -57,8 +53,8 @@ void loop() { client.put("/", contentType, putData); // read the status code and body of the response - statusCode = client.responseStatusCode(); - response = client.responseBody(); + int statusCode = client.responseStatusCode(); + String response = client.responseBody(); Serial.print("Status code: "); Serial.println(statusCode); diff --git a/examples/SimpleWebSocket/SimpleWebSocket.ino b/examples/SimpleWebSocket/SimpleWebSocket.ino index baac12f..92f3f55 100644 --- a/examples/SimpleWebSocket/SimpleWebSocket.ino +++ b/examples/SimpleWebSocket/SimpleWebSocket.ino @@ -3,9 +3,10 @@ Connects to the WebSocket server, and sends a hello message every 5 seconds - created 28 Jun 2016 by Sandeep Mistry + modified 22 Jan 2019 + by Tom Igoe this example is in the public domain */ @@ -76,5 +77,4 @@ void loop() { } Serial.println("disconnected"); -} - +} \ No newline at end of file