diff --git a/examples/DweetGet/DweetGet.ino b/examples/DweetGet/DweetGet.ino new file mode 100644 index 0000000..11d3daa --- /dev/null +++ b/examples/DweetGet/DweetGet.ino @@ -0,0 +1,121 @@ +/* + Dweet.io GET client for ArduinoHttpClient library + Connects to dweet.io once every ten seconds, + sends a GET request and a request body. Uses SSL + + Shows how to use Strings to assemble path and parse content + from response. dweet.io expects: + https://dweet.io/get/latest/dweet/for/thingName + + For more on dweet.io, see https://dweet.io/play/ + + 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 + updated 16 Feb 2016 + by Tom Igoe + + this example is in the public domain +*/ +#include +#include +#include "config.h" + +const char serverAddress[] = "dweet.io"; // server address +int port = 80; +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; +int contentLength = 0; +String response; + +void setup() { + Serial.begin(9600); + while (!Serial); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + // assemble the path for the GET message: + String path = "/get/latest/dweet/for/" + dweetName; + + // send the GET request + Serial.println("making GET request"); + client.beginRequest(); + client.get(path); + client.endRequest(); + + // read the status code of the response + statusCode = client.responseStatusCode(); + Serial.print("Status code: "); + Serial.println(statusCode); + + // read the content length of the response + contentLength = client.contentLength(); + Serial.print("Content Length: "); + Serial.println(contentLength); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Response: "); + Serial.println(response); + + /* + Typical response is: + {"this":"succeeded", + "by":"getting", + "the":"dweets", + "with":[{"thing":"my-thing-name", + "created":"2016-02-16T05:10:36.589Z", + "content":{"sensorValue":456}}]} + + You want "content": numberValue + */ + // now parse the response looking for "content": + int labelStart = response.indexOf("content\":"); + // find the first { after "content": + int contentStart = response.indexOf("{", labelStart); + // find the following } and get what's between the braces: + int contentEnd = response.indexOf("}", labelStart); + String content = response.substring(contentStart + 1, contentEnd); + Serial.println(content); + + // now get the value after the colon, and convert to an int: + int valueStart = content.indexOf(":"); + String valueString = content.substring(valueStart + 1); + int number = valueString.toInt(); + Serial.print("Value string: "); + Serial.println(valueString); + Serial.print("Actual value: "); + Serial.println(number); + + Serial.println("Wait ten seconds\n"); + delay(10000); +} diff --git a/examples/DweetGet/config.h b/examples/DweetGet/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/DweetGet/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/DweetPost/DweetPost.ino b/examples/DweetPost/DweetPost.ino new file mode 100644 index 0000000..04b5f3e --- /dev/null +++ b/examples/DweetPost/DweetPost.ino @@ -0,0 +1,93 @@ +/* + Dweet.io POST client for ArduinoHttpClient library + Connects to dweet.io once every ten seconds, + sends a POST request and a request body. + + 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 + by Tom Igoe + + this example is in the public domain +*/ +#include +#include +#include "config.h" + +const char serverAddress[] = "dweet.io"; // server address +int port = 80; + +WiFiClient wifi; +HttpClient client = HttpClient(wifi, serverAddress, port); +int status = WL_IDLE_STATUS; +int statusCode = 0; +int contentLength = 0; +String response; + +void setup() { + Serial.begin(9600); + while(!Serial); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + // assemble the path for the POST message: + String dweetName = "scandalous-cheese-hoarder"; + String path = "/dweet/for/" + dweetName; + + // assemble the body of the POST message: + int sensorValue = analogRead(A0); + String postData = "{\"sensorValue\":\""; + postData += sensorValue; + postData += "\"}"; + + Serial.println("making POST request"); + + // send the POST request + client.beginRequest(); + client.post(path); + client.sendHeader("Content-Type", "application/json"); + client.sendHeader("Content-Length", postData.length()); + client.endRequest(); + client.write((const byte*)postData.c_str(), postData.length()); + + // read the status code and content length of the response + statusCode = client.responseStatusCode(); + contentLength = client.contentLength(); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Status code: "); + Serial.println(statusCode); + Serial.print("Response: "); + Serial.println(response); + + Serial.println("Wait ten seconds\n"); + delay(10000); +} diff --git a/examples/DweetPost/config.h b/examples/DweetPost/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/DweetPost/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/HueBlink/HueBlink.ino b/examples/HueBlink/HueBlink.ino new file mode 100644 index 0000000..3d6a028 --- /dev/null +++ b/examples/HueBlink/HueBlink.ino @@ -0,0 +1,110 @@ +/* HueBlink example for ArduinoHttpClient library + + Uses ArduinoHttpClient library to control Philips Hue + For more on Hue developer API see http://developer.meethue.com + + To control a light, the Hue expects a HTTP PUT request to: + + http://hue.hub.address/api/hueUserName/lights/lightNumber/state + + The body of the PUT request looks like this: + {"on": true} or {"on":false} + + This example shows how to concatenate Strings to assemble the + PUT request and the body of the request. + + 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 + + modified 15 Feb 2016 + by Tom Igoe (tigoe) to match new API +*/ + +#include +#include +#include +#include "config.h" + +int status = WL_IDLE_STATUS; // the Wifi radio's status +char hueHubIP[] = "192.168.0.3"; // IP address of the HUE bridge +String hueUserName = "huebridgeusername"; // hue bridge username + +// make a wifi instance and a HttpClient instance: +WiFiClient wifi; +HttpClient httpClient = HttpClient(wifi, hueHubIP); + + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial); // wait for serial port to connect. + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network IP = "); + IPAddress ip = WiFi.localIP(); + Serial.println(ip); +} + +void loop() { + sendRequest(3, "on", "true"); // turn light on + delay(2000); // wait 2 seconds + sendRequest(3, "on", "false"); // turn light off + delay(2000); // wait 2 seconds +} + +void sendRequest(int light, String cmd, String value) { + // make a String for the HTTP request path: + String request = "/api/" + hueUserName; + request += "/lights/"; + request += light; + request += "/state/"; + + // make a string for the JSON command: + String hueCmd = "{\"" + cmd; + hueCmd += "\":"; + hueCmd += value; + hueCmd += "}"; + // see what you assembled to send: + Serial.print("PUT request to server: "); + Serial.println(request); + Serial.print("JSON command to server: "); + + // make the PUT request to the hub: + httpClient.beginRequest(); + httpClient.put(request); + httpClient.sendHeader("Content-Type", "application/json"); + httpClient.sendHeader("Content-Length", hueCmd.length()); + httpClient.endRequest(); + httpClient.write((const byte*)hueCmd.c_str(), hueCmd.length()); + + // read the status code and content length of the response + int statusCode = httpClient.responseStatusCode(); + int contentLength = httpClient.contentLength(); + + // read the response body + String response = ""; + response.reserve(contentLength); + while (httpClient.available()) { + response += (char)httpClient.read(); + } + + Serial.println(hueCmd); + Serial.print("Status code from server: "); + Serial.println(statusCode); + Serial.print("Server response: "); + Serial.println(response); + Serial.println(); +} + + diff --git a/examples/HueBlink/config.h b/examples/HueBlink/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/HueBlink/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/SimpleDelete/SimpleDelete.ino b/examples/SimpleDelete/SimpleDelete.ino new file mode 100644 index 0000000..591f4de --- /dev/null +++ b/examples/SimpleDelete/SimpleDelete.ino @@ -0,0 +1,80 @@ +/* + Simple DELETE client for ArduinoHttpClient library + Connects to server once every five seconds, sends a DELETE request + and a request 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 14 Feb 2016 + by Tom Igoe + + this example is in the public domain + */ +#include +#include +#include "config.h" + +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; +int contentLength = 0; + +void setup() { + Serial.begin(9600); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("making DELETE request"); + String delData = "name=light&age=46"; + + client.beginRequest(); + client.startRequest("/", HTTP_METHOD_DELETE); + client.sendHeader("Content-Type", "application/x-www-form-urlencoded"); + client.sendHeader("Content-Length", delData.length()); + client.endRequest(); + client.write((const byte*)delData.c_str(), delData.length()); + + // read the status code and content length of the response + statusCode = client.responseStatusCode(); + contentLength = client.contentLength(); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Status code: "); + Serial.println(statusCode); + Serial.print("Response: "); + Serial.println(response); + + Serial.println("Wait five seconds"); + delay(5000); +} diff --git a/examples/SimpleDelete/config.h b/examples/SimpleDelete/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/SimpleDelete/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/SimpleGet/SimpleGet.ino b/examples/SimpleGet/SimpleGet.ino new file mode 100644 index 0000000..86cdb9d --- /dev/null +++ b/examples/SimpleGet/SimpleGet.ino @@ -0,0 +1,74 @@ +/* + Simple GET client for ArduinoHttpClient library + Connects to server once every five seconds, sends a GET request + + 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 14 Feb 2016 + by Tom Igoe + + this example is in the public domain + */ +#include +#include +#include "config.h" + +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; +int contentLength = 0; + +void setup() { + Serial.begin(9600); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("making GET request"); + + // read the status code and content length of the response + client.beginRequest(); + client.get("/"); + client.endRequest(); + + statusCode = client.responseStatusCode(); + contentLength = client.contentLength(); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Status code: "); + Serial.println(statusCode); + Serial.print("Response: "); + Serial.println(response); + Serial.println("Wait five seconds"); + delay(5000); +} diff --git a/examples/SimpleGet/config.h b/examples/SimpleGet/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/SimpleGet/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/SimplePost/SimplePost.ino b/examples/SimplePost/SimplePost.ino new file mode 100644 index 0000000..46982ca --- /dev/null +++ b/examples/SimplePost/SimplePost.ino @@ -0,0 +1,80 @@ +/* + Simple POST client for ArduinoHttpClient library + Connects to server once every five seconds, sends a POST request + and a request 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 14 Feb 2016 + by Tom Igoe + + this example is in the public domain + */ +#include +#include +#include "config.h" + +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; +int contentLength = 0; + +void setup() { + Serial.begin(9600); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("making POST request"); + String postData = "name=Alice&age=12"; + + client.beginRequest(); + client.post("/"); + client.sendHeader("Content-Type", "application/x-www-form-urlencoded"); + client.sendHeader("Content-Length", postData.length()); + client.endRequest(); + client.write((const byte*)postData.c_str(), postData.length()); + + // read the status code and content length of the response + statusCode = client.responseStatusCode(); + contentLength = client.contentLength(); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Status code: "); + Serial.println(statusCode); + Serial.print("Response: "); + Serial.println(response); + + Serial.println("Wait five seconds"); + delay(5000); +} diff --git a/examples/SimplePost/config.h b/examples/SimplePost/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/SimplePost/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/SimplePut/SimplePut.ino b/examples/SimplePut/SimplePut.ino new file mode 100644 index 0000000..611f55c --- /dev/null +++ b/examples/SimplePut/SimplePut.ino @@ -0,0 +1,80 @@ +/* + Simple PUT client for ArduinoHttpClient library + Connects to server once every five seconds, sends a PUT request + and a request 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 14 Feb 2016 + by Tom Igoe + + this example is in the public domain + */ +#include +#include +#include "config.h" + +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; +int contentLength = 0; + +void setup() { + Serial.begin(9600); + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to Network named: "); + Serial.println(ssid); // print the network name (SSID); + + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + } + + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + Serial.println("making PUT request"); + String putData = "name=light&age=46"; + + client.beginRequest(); + client.put("/"); + client.sendHeader("Content-Type", "application/x-www-form-urlencoded"); + client.sendHeader("Content-Length", putData.length()); + client.endRequest(); + client.write((const byte*)putData.c_str(), putData.length()); + + // read the status code and content length of the response + statusCode = client.responseStatusCode(); + contentLength = client.contentLength(); + + // read the response body + response = ""; + response.reserve(contentLength); + while (client.available()) { + response += (char)client.read(); + } + + Serial.print("Status code: "); + Serial.println(statusCode); + Serial.print("Response: "); + Serial.println(response); + + Serial.println("Wait five seconds"); + delay(5000); +} diff --git a/examples/SimplePut/config.h b/examples/SimplePut/config.h new file mode 100644 index 0000000..c263766 --- /dev/null +++ b/examples/SimplePut/config.h @@ -0,0 +1,2 @@ +char ssid[] = "ssid"; // your network SSID (name) +char pass[] = "password"; // your network password diff --git a/examples/node_test_server/getPostPutDelete.js b/examples/node_test_server/getPostPutDelete.js new file mode 100644 index 0000000..ec58f82 --- /dev/null +++ b/examples/node_test_server/getPostPutDelete.js @@ -0,0 +1,42 @@ +/* + Express.js GET/POST example + Shows how handle GET, POST, PUT, DELETE + in Express.js 4.0 + + created 14 Feb 2016 + by Tom Igoe +*/ + +var express = require('express'); // include express.js +var app = express(); // a local instance of it +var bodyParser = require('body-parser'); // include body-parser + +// you need a body parser: +app.use(bodyParser.urlencoded({extended: false})); // for application/x-www-form-urlencoded + +// this runs after the server successfully starts: +function serverStart() { + var port = server.address().port; + console.log('Server listening on port '+ port); +} + +// this is the POST handler: +app.all('/*', function (request, response) { + console.log('Got a ' + request.method + ' request'); + // the parameters of a GET request are passed in + // request.body. Pass that to formatResponse() + // for formatting: + console.log(request.headers); + if (request.method == 'GET') { + console.log(request.query); + } else { + console.log(request.body); + } + + // send the response: + response.send('OK'); + response.end(); +}); + +// start the server: +var server = app.listen(8080, serverStart); diff --git a/examples/node_test_server/package.json b/examples/node_test_server/package.json new file mode 100644 index 0000000..d6fb7cc --- /dev/null +++ b/examples/node_test_server/package.json @@ -0,0 +1,16 @@ +{ + "name": "node_test_server", + "version": "0.0.1", + "author": { + "name":"Tom Igoe" + }, + "dependencies": { + "express": ">=4.0.0", + "body-parser" : ">=1.11.0", + "multer" : "*" + }, + "engines": { + "node": "0.10.x", + "npm": "1.3.x" + } +}