Skip to content

Commit fc3e6c6

Browse files
committed
Initial port of examples from RestClient
1 parent 94a8723 commit fc3e6c6

File tree

16 files changed

+710
-0
lines changed

16 files changed

+710
-0
lines changed

‎examples/DweetGet/DweetGet.ino

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Dweet.io GET client for ArduinoHttpClient library
3+
Connects to dweet.io once every ten seconds,
4+
sends a GET request and a request body. Uses SSL
5+
6+
Shows how to use Strings to assemble path and parse content
7+
from response. dweet.io expects:
8+
https://dweet.io/get/latest/dweet/for/thingName
9+
10+
For more on dweet.io, see https://dweet.io/play/
11+
12+
note: WiFi SSID and password are stored in config.h file.
13+
If it is not present, add a new tab, call it "config.h"
14+
and add the following variables:
15+
char ssid[] = "ssid"; // your network SSID (name)
16+
char pass[] = "password"; // your network password
17+
18+
created 15 Feb 2016
19+
updated 16 Feb 2016
20+
by Tom Igoe
21+
22+
this example is in the public domain
23+
*/
24+
#include <ArduinoHttpClient.h>
25+
#include <WiFi101.h>
26+
#include "config.h"
27+
28+
const char serverAddress[] = "dweet.io"; // server address
29+
int port = 80;
30+
String dweetName = "scandalous-cheese-hoarder"; // use your own thing name here
31+
32+
WiFiClient wifi;
33+
HttpClient client = HttpClient(wifi, serverAddress, port);
34+
int status = WL_IDLE_STATUS;
35+
int statusCode = 0;
36+
int contentLength = 0;
37+
String response;
38+
39+
void setup() {
40+
Serial.begin(9600);
41+
while (!Serial);
42+
while ( status != WL_CONNECTED) {
43+
Serial.print("Attempting to connect to Network named: ");
44+
Serial.println(ssid); // print the network name (SSID);
45+
46+
// Connect to WPA/WPA2 network:
47+
status = WiFi.begin(ssid, pass);
48+
}
49+
50+
// print the SSID of the network you're attached to:
51+
Serial.print("SSID: ");
52+
Serial.println(WiFi.SSID());
53+
54+
// print your WiFi shield's IP address:
55+
IPAddress ip = WiFi.localIP();
56+
Serial.print("IP Address: ");
57+
Serial.println(ip);
58+
}
59+
60+
void loop() {
61+
// assemble the path for the GET message:
62+
String path = "/get/latest/dweet/for/" + dweetName;
63+
64+
// send the GET request
65+
Serial.println("making GET request");
66+
client.beginRequest();
67+
client.get(path);
68+
client.endRequest();
69+
70+
// read the status code of the response
71+
statusCode = client.responseStatusCode();
72+
Serial.print("Status code: ");
73+
Serial.println(statusCode);
74+
75+
// read the content length of the response
76+
contentLength = client.contentLength();
77+
Serial.print("Content Length: ");
78+
Serial.println(contentLength);
79+
80+
// read the response body
81+
response = "";
82+
response.reserve(contentLength);
83+
while (client.available()) {
84+
response += (char)client.read();
85+
}
86+
87+
Serial.print("Response: ");
88+
Serial.println(response);
89+
90+
/*
91+
Typical response is:
92+
{"this":"succeeded",
93+
"by":"getting",
94+
"the":"dweets",
95+
"with":[{"thing":"my-thing-name",
96+
"created":"2016-02-16T05:10:36.589Z",
97+
"content":{"sensorValue":456}}]}
98+
99+
You want "content": numberValue
100+
*/
101+
// now parse the response looking for "content":
102+
int labelStart = response.indexOf("content\":");
103+
// find the first { after "content":
104+
int contentStart = response.indexOf("{", labelStart);
105+
// find the following } and get what's between the braces:
106+
int contentEnd = response.indexOf("}", labelStart);
107+
String content = response.substring(contentStart + 1, contentEnd);
108+
Serial.println(content);
109+
110+
// now get the value after the colon, and convert to an int:
111+
int valueStart = content.indexOf(":");
112+
String valueString = content.substring(valueStart + 1);
113+
int number = valueString.toInt();
114+
Serial.print("Value string: ");
115+
Serial.println(valueString);
116+
Serial.print("Actual value: ");
117+
Serial.println(number);
118+
119+
Serial.println("Wait ten seconds\n");
120+
delay(10000);
121+
}

‎examples/DweetGet/config.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
char ssid[] = "ssid"; // your network SSID (name)
2+
char pass[] = "password"; // your network password

‎examples/DweetPost/DweetPost.ino

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Dweet.io POST client for ArduinoHttpClient library
3+
Connects to dweet.io once every ten seconds,
4+
sends a POST request and a request body.
5+
6+
Shows how to use Strings to assemble path and body
7+
8+
note: WiFi SSID and password are stored in config.h file.
9+
If it is not present, add a new tab, call it "config.h"
10+
and add the following variables:
11+
char ssid[] = "ssid"; // your network SSID (name)
12+
char pass[] = "password"; // your network password
13+
14+
created 15 Feb 2016
15+
by Tom Igoe
16+
17+
this example is in the public domain
18+
*/
19+
#include <ArduinoHttpClient.h>
20+
#include <WiFi101.h>
21+
#include "config.h"
22+
23+
const char serverAddress[] = "dweet.io"; // server address
24+
int port = 80;
25+
26+
WiFiClient wifi;
27+
HttpClient client = HttpClient(wifi, serverAddress, port);
28+
int status = WL_IDLE_STATUS;
29+
int statusCode = 0;
30+
int contentLength = 0;
31+
String response;
32+
33+
void setup() {
34+
Serial.begin(9600);
35+
while(!Serial);
36+
while ( status != WL_CONNECTED) {
37+
Serial.print("Attempting to connect to Network named: ");
38+
Serial.println(ssid); // print the network name (SSID);
39+
40+
// Connect to WPA/WPA2 network:
41+
status = WiFi.begin(ssid, pass);
42+
}
43+
44+
// print the SSID of the network you're attached to:
45+
Serial.print("SSID: ");
46+
Serial.println(WiFi.SSID());
47+
48+
// print your WiFi shield's IP address:
49+
IPAddress ip = WiFi.localIP();
50+
Serial.print("IP Address: ");
51+
Serial.println(ip);
52+
}
53+
54+
void loop() {
55+
// assemble the path for the POST message:
56+
String dweetName = "scandalous-cheese-hoarder";
57+
String path = "/dweet/for/" + dweetName;
58+
59+
// assemble the body of the POST message:
60+
int sensorValue = analogRead(A0);
61+
String postData = "{\"sensorValue\":\"";
62+
postData += sensorValue;
63+
postData += "\"}";
64+
65+
Serial.println("making POST request");
66+
67+
// send the POST request
68+
client.beginRequest();
69+
client.post(path);
70+
client.sendHeader("Content-Type", "application/json");
71+
client.sendHeader("Content-Length", postData.length());
72+
client.endRequest();
73+
client.write((const byte*)postData.c_str(), postData.length());
74+
75+
// read the status code and content length of the response
76+
statusCode = client.responseStatusCode();
77+
contentLength = client.contentLength();
78+
79+
// read the response body
80+
response = "";
81+
response.reserve(contentLength);
82+
while (client.available()) {
83+
response += (char)client.read();
84+
}
85+
86+
Serial.print("Status code: ");
87+
Serial.println(statusCode);
88+
Serial.print("Response: ");
89+
Serial.println(response);
90+
91+
Serial.println("Wait ten seconds\n");
92+
delay(10000);
93+
}

‎examples/DweetPost/config.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
char ssid[] = "ssid"; // your network SSID (name)
2+
char pass[] = "password"; // your network password

‎examples/HueBlink/HueBlink.ino

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* HueBlink example for ArduinoHttpClient library
2+
3+
Uses ArduinoHttpClient library to control Philips Hue
4+
For more on Hue developer API see http://developer.meethue.com
5+
6+
To control a light, the Hue expects a HTTP PUT request to:
7+
8+
http://hue.hub.address/api/hueUserName/lights/lightNumber/state
9+
10+
The body of the PUT request looks like this:
11+
{"on": true} or {"on":false}
12+
13+
This example shows how to concatenate Strings to assemble the
14+
PUT request and the body of the request.
15+
16+
note: WiFi SSID and password are stored in config.h file.
17+
If it is not present, add a new tab, call it "config.h"
18+
and add the following variables:
19+
char ssid[] = "ssid"; // your network SSID (name)
20+
char pass[] = "password"; // your network password
21+
22+
modified 15 Feb 2016
23+
by Tom Igoe (tigoe) to match new API
24+
*/
25+
26+
#include <SPI.h>
27+
#include <WiFi101.h>
28+
#include <ArduinoHttpClient.h>
29+
#include "config.h"
30+
31+
int status = WL_IDLE_STATUS; // the Wifi radio's status
32+
char hueHubIP[] = "192.168.0.3"; // IP address of the HUE bridge
33+
String hueUserName = "huebridgeusername"; // hue bridge username
34+
35+
// make a wifi instance and a HttpClient instance:
36+
WiFiClient wifi;
37+
HttpClient httpClient = HttpClient(wifi, hueHubIP);
38+
39+
40+
void setup() {
41+
//Initialize serial and wait for port to open:
42+
Serial.begin(9600);
43+
while (!Serial); // wait for serial port to connect.
44+
45+
// attempt to connect to Wifi network:
46+
while ( status != WL_CONNECTED) {
47+
Serial.print("Attempting to connect to WPA SSID: ");
48+
Serial.println(ssid);
49+
// Connect to WPA/WPA2 network:
50+
status = WiFi.begin(ssid, pass);
51+
}
52+
53+
// you're connected now, so print out the data:
54+
Serial.print("You're connected to the network IP = ");
55+
IPAddress ip = WiFi.localIP();
56+
Serial.println(ip);
57+
}
58+
59+
void loop() {
60+
sendRequest(3, "on", "true"); // turn light on
61+
delay(2000); // wait 2 seconds
62+
sendRequest(3, "on", "false"); // turn light off
63+
delay(2000); // wait 2 seconds
64+
}
65+
66+
void sendRequest(int light, String cmd, String value) {
67+
// make a String for the HTTP request path:
68+
String request = "/api/" + hueUserName;
69+
request += "/lights/";
70+
request += light;
71+
request += "/state/";
72+
73+
// make a string for the JSON command:
74+
String hueCmd = "{\"" + cmd;
75+
hueCmd += "\":";
76+
hueCmd += value;
77+
hueCmd += "}";
78+
// see what you assembled to send:
79+
Serial.print("PUT request to server: ");
80+
Serial.println(request);
81+
Serial.print("JSON command to server: ");
82+
83+
// make the PUT request to the hub:
84+
httpClient.beginRequest();
85+
httpClient.put(request);
86+
httpClient.sendHeader("Content-Type", "application/json");
87+
httpClient.sendHeader("Content-Length", hueCmd.length());
88+
httpClient.endRequest();
89+
httpClient.write((const byte*)hueCmd.c_str(), hueCmd.length());
90+
91+
// read the status code and content length of the response
92+
int statusCode = httpClient.responseStatusCode();
93+
int contentLength = httpClient.contentLength();
94+
95+
// read the response body
96+
String response = "";
97+
response.reserve(contentLength);
98+
while (httpClient.available()) {
99+
response += (char)httpClient.read();
100+
}
101+
102+
Serial.println(hueCmd);
103+
Serial.print("Status code from server: ");
104+
Serial.println(statusCode);
105+
Serial.print("Server response: ");
106+
Serial.println(response);
107+
Serial.println();
108+
}
109+
110+

‎examples/HueBlink/config.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
char ssid[] = "ssid"; // your network SSID (name)
2+
char pass[] = "password"; // your network password

0 commit comments

Comments
 (0)