mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Initial port of examples from RestClient
This commit is contained in:
parent
94a8723420
commit
fc3e6c6fe7
121
examples/DweetGet/DweetGet.ino
Normal file
121
examples/DweetGet/DweetGet.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/DweetGet/config.h
Normal file
2
examples/DweetGet/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
93
examples/DweetPost/DweetPost.ino
Normal file
93
examples/DweetPost/DweetPost.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/DweetPost/config.h
Normal file
2
examples/DweetPost/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
110
examples/HueBlink/HueBlink.ino
Normal file
110
examples/HueBlink/HueBlink.ino
Normal file
@ -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 <SPI.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#include <ArduinoHttpClient.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
2
examples/HueBlink/config.h
Normal file
2
examples/HueBlink/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
80
examples/SimpleDelete/SimpleDelete.ino
Normal file
80
examples/SimpleDelete/SimpleDelete.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/SimpleDelete/config.h
Normal file
2
examples/SimpleDelete/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
74
examples/SimpleGet/SimpleGet.ino
Normal file
74
examples/SimpleGet/SimpleGet.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/SimpleGet/config.h
Normal file
2
examples/SimpleGet/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
80
examples/SimplePost/SimplePost.ino
Normal file
80
examples/SimplePost/SimplePost.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/SimplePost/config.h
Normal file
2
examples/SimplePost/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
80
examples/SimplePut/SimplePut.ino
Normal file
80
examples/SimplePut/SimplePut.ino
Normal file
@ -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 <ArduinoHttpClient.h>
|
||||||
|
#include <WiFi101.h>
|
||||||
|
#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);
|
||||||
|
}
|
2
examples/SimplePut/config.h
Normal file
2
examples/SimplePut/config.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
char ssid[] = "ssid"; // your network SSID (name)
|
||||||
|
char pass[] = "password"; // your network password
|
42
examples/node_test_server/getPostPutDelete.js
Normal file
42
examples/node_test_server/getPostPutDelete.js
Normal file
@ -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);
|
16
examples/node_test_server/package.json
Normal file
16
examples/node_test_server/package.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user