first commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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 with HTTP basic authentication");
|
||||
client.beginRequest();
|
||||
client.get("/secure");
|
||||
client.sendBasicAuth("username", "password"); // send the username and password for authentication
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Custom request header example for the ArduinoHttpClient
|
||||
library. This example sends a GET and a POST request with a custom header every 5 seconds.
|
||||
|
||||
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 <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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");
|
||||
client.beginRequest();
|
||||
client.get("/");
|
||||
client.sendHeader("X-CUSTOM-HEADER", "custom_value");
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("GET Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("GET Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
|
||||
Serial.println("making POST request");
|
||||
String postData = "name=Alice&age=12";
|
||||
client.beginRequest();
|
||||
client.post("/");
|
||||
client.sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
|
||||
client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
|
||||
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();
|
||||
response = client.responseBody();
|
||||
|
||||
Serial.print("POST Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("POST Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
103
libraries/ArduinoHttpClient/examples/DweetGet/DweetGet.ino
Normal file
103
libraries/ArduinoHttpClient/examples/DweetGet/DweetGet.ino
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
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/
|
||||
|
||||
created 15 Feb 2016
|
||||
updated 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
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;
|
||||
|
||||
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.get(path);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
79
libraries/ArduinoHttpClient/examples/DweetPost/DweetPost.ino
Normal file
79
libraries/ArduinoHttpClient/examples/DweetPost/DweetPost.ino
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
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
|
||||
|
||||
created 15 Feb 2016
|
||||
modified 22 Jan 2019
|
||||
by Tom Igoe
|
||||
|
||||
this example is in the public domain
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
const char serverAddress[] = "dweet.io"; // server address
|
||||
int port = 80;
|
||||
|
||||
WiFiClient wifi;
|
||||
HttpClient client = HttpClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
|
||||
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;
|
||||
String contentType = "application/json";
|
||||
|
||||
// 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.post(path, contentType, postData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait ten seconds\n");
|
||||
delay(10000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
98
libraries/ArduinoHttpClient/examples/HueBlink/HueBlink.ino
Normal file
98
libraries/ArduinoHttpClient/examples/HueBlink/HueBlink.ino
Normal file
@@ -0,0 +1,98 @@
|
||||
/* 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.
|
||||
|
||||
modified 15 Feb 2016
|
||||
by Tom Igoe (tigoe) to match new API
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi101.h>
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
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 WiFiClient 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/";
|
||||
|
||||
String contentType = "application/json";
|
||||
|
||||
// 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.put(request, contentType, hueCmd);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = httpClient.responseStatusCode();
|
||||
String response = httpClient.responseBody();
|
||||
|
||||
Serial.println(hueCmd);
|
||||
Serial.print("Status code from server: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Server response: ");
|
||||
Serial.println(response);
|
||||
Serial.println();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
29
libraries/ArduinoHttpClient/examples/ParseURL/ParseURL.ino
Normal file
29
libraries/ArduinoHttpClient/examples/ParseURL/ParseURL.ino
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "URLParser.h"
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
|
||||
while(!Serial);
|
||||
|
||||
Serial.println("starting");
|
||||
|
||||
ParsedUrl url(
|
||||
"https://www.google.com/search?q=arduino"
|
||||
);
|
||||
|
||||
Serial.print("parsed URL schema: \"");
|
||||
Serial.print(url.schema());
|
||||
Serial.print("\"\nparsed URL host: \"");
|
||||
Serial.print(url.host());
|
||||
Serial.print("\"\nparsed URL path: \"");
|
||||
Serial.print(url.path());
|
||||
Serial.print("\"\nparsed URL query: \"");
|
||||
Serial.print(url.query());
|
||||
Serial.print("\"\nparsed URL userinfo: \"");
|
||||
Serial.print(url.userinfo());
|
||||
Serial.println("\"");
|
||||
|
||||
}
|
||||
|
||||
void loop() { }
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
POST with headers client for ArduinoHttpClient library
|
||||
Connects to server once every five seconds, sends a POST request
|
||||
with custom 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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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.sendHeader("X-Custom-Header", "custom-header-value");
|
||||
client.beginBody();
|
||||
client.print(postData);
|
||||
client.endRequest();
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Simple DELETE client for ArduinoHttpClient library
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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 contentType = "application/x-www-form-urlencoded";
|
||||
String delData = "name=light&age=46";
|
||||
|
||||
client.del("/", contentType, delData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
62
libraries/ArduinoHttpClient/examples/SimpleGet/SimpleGet.ino
Normal file
62
libraries/ArduinoHttpClient/examples/SimpleGet/SimpleGet.ino
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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");
|
||||
client.get("/");
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
// (c) Copyright 2010-2012 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
//
|
||||
// Simple example to show how to use the HttpClient library
|
||||
// Gets the web page given at http://<kHostname><kPath> and
|
||||
// outputs the content to the serial port
|
||||
|
||||
#include <SPI.h>
|
||||
#include <WiFi101.h>
|
||||
#include <ArduinoHttpClient.h>
|
||||
|
||||
// This example downloads the URL "http://arduino.cc/"
|
||||
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
|
||||
|
||||
// Name of the server we want to connect to
|
||||
const char kHostname[] = "arduino.cc";
|
||||
// Path to download (this is the bit after the hostname in the URL
|
||||
// that you want to download
|
||||
const char kPath[] = "/";
|
||||
|
||||
// Number of milliseconds to wait without receiving any data before we give up
|
||||
const int kNetworkTimeout = 30*1000;
|
||||
// Number of milliseconds to wait if no data is available before trying again
|
||||
const int kNetworkDelay = 1000;
|
||||
|
||||
WiFiClient c;
|
||||
HttpClient http(c, kHostname);
|
||||
|
||||
void setup()
|
||||
{
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// attempt to connect to WiFi network:
|
||||
Serial.print("Attempting to connect to WPA SSID: ");
|
||||
Serial.println(ssid);
|
||||
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
|
||||
// unsuccessful, retry in 4 seconds
|
||||
Serial.print("failed ... ");
|
||||
delay(4000);
|
||||
Serial.print("retrying ... ");
|
||||
}
|
||||
|
||||
Serial.println("connected");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int err =0;
|
||||
|
||||
err = http.get(kPath);
|
||||
if (err == 0)
|
||||
{
|
||||
Serial.println("startedRequest ok");
|
||||
|
||||
err = http.responseStatusCode();
|
||||
if (err >= 0)
|
||||
{
|
||||
Serial.print("Got status code: ");
|
||||
Serial.println(err);
|
||||
|
||||
// Usually you'd check that the response code is 200 or a
|
||||
// similar "success" code (200-299) before carrying on,
|
||||
// but we'll print out whatever response we get
|
||||
|
||||
// If you are interesting in the response headers, you
|
||||
// can read them here:
|
||||
//while(http.headerAvailable())
|
||||
//{
|
||||
// String headerName = http.readHeaderName();
|
||||
// String headerValue = http.readHeaderValue();
|
||||
//}
|
||||
|
||||
int bodyLen = http.contentLength();
|
||||
Serial.print("Content length is: ");
|
||||
Serial.println(bodyLen);
|
||||
Serial.println();
|
||||
Serial.println("Body returned follows:");
|
||||
|
||||
// Now we've got to the body, so we can print it out
|
||||
unsigned long timeoutStart = millis();
|
||||
char c;
|
||||
// Whilst we haven't timed out & haven't reached the end of the body
|
||||
while ( (http.connected() || http.available()) &&
|
||||
(!http.endOfBodyReached()) &&
|
||||
((millis() - timeoutStart) < kNetworkTimeout) )
|
||||
{
|
||||
if (http.available())
|
||||
{
|
||||
c = http.read();
|
||||
// Print out this character
|
||||
Serial.print(c);
|
||||
|
||||
// We read something, reset the timeout counter
|
||||
timeoutStart = millis();
|
||||
}
|
||||
else
|
||||
{
|
||||
// We haven't got any data, so let's pause to allow some to
|
||||
// arrive
|
||||
delay(kNetworkDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Getting response failed: ");
|
||||
Serial.println(err);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Connect failed: ");
|
||||
Serial.println(err);
|
||||
}
|
||||
http.stop();
|
||||
|
||||
// And just stop, now that we've tried a download
|
||||
while(1);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Simple POST client for ArduinoHttpClient library
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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 contentType = "application/x-www-form-urlencoded";
|
||||
String postData = "name=Alice&age=12";
|
||||
|
||||
client.post("/", contentType, postData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
66
libraries/ArduinoHttpClient/examples/SimplePut/SimplePut.ino
Normal file
66
libraries/ArduinoHttpClient/examples/SimplePut/SimplePut.ino
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
Simple PUT client for ArduinoHttpClient library
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
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;
|
||||
|
||||
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 contentType = "application/x-www-form-urlencoded";
|
||||
String putData = "name=light&age=46";
|
||||
|
||||
client.put("/", contentType, putData);
|
||||
|
||||
// read the status code and body of the response
|
||||
int statusCode = client.responseStatusCode();
|
||||
String response = client.responseBody();
|
||||
|
||||
Serial.print("Status code: ");
|
||||
Serial.println(statusCode);
|
||||
Serial.print("Response: ");
|
||||
Serial.println(response);
|
||||
|
||||
Serial.println("Wait five seconds");
|
||||
delay(5000);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
Simple WebSocket client for ArduinoHttpClient library
|
||||
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
|
||||
*/
|
||||
#include <ArduinoHttpClient.h>
|
||||
#include <WiFi101.h>
|
||||
#include "arduino_secrets.h"
|
||||
|
||||
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
|
||||
/////// WiFi Settings ///////
|
||||
char ssid[] = SECRET_SSID;
|
||||
char pass[] = SECRET_PASS;
|
||||
|
||||
char serverAddress[] = "echo.websocket.org"; // server address
|
||||
int port = 80;
|
||||
|
||||
WiFiClient wifi;
|
||||
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
|
||||
int status = WL_IDLE_STATUS;
|
||||
int count = 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("starting WebSocket client");
|
||||
client.begin();
|
||||
|
||||
while (client.connected()) {
|
||||
Serial.print("Sending hello ");
|
||||
Serial.println(count);
|
||||
|
||||
// send a hello #
|
||||
client.beginMessage(TYPE_TEXT);
|
||||
client.print("hello ");
|
||||
client.print(count);
|
||||
client.endMessage();
|
||||
|
||||
// increment count for next message
|
||||
count++;
|
||||
|
||||
// check if a message is available to be received
|
||||
int messageSize = client.parseMessage();
|
||||
|
||||
if (messageSize > 0) {
|
||||
Serial.println("Received a message:");
|
||||
Serial.println(client.readString());
|
||||
}
|
||||
|
||||
// wait 5 seconds
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
Serial.println("disconnected");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#define SECRET_SSID ""
|
||||
#define SECRET_PASS ""
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
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
|
||||
var WebSocketServer = require('ws').Server // include Web Socket server
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
app.get('/chunked', function(request, response) {
|
||||
response.write('\n');
|
||||
response.write(' `:;;;,` .:;;:. \n');
|
||||
response.write(' .;;;;;;;;;;;` :;;;;;;;;;;: TM \n');
|
||||
response.write(' `;;;;;;;;;;;;;;;` :;;;;;;;;;;;;;;; \n');
|
||||
response.write(' :;;;;;;;;;;;;;;;;;; `;;;;;;;;;;;;;;;;;; \n');
|
||||
response.write(' ;;;;;;;;;;;;;;;;;;;;; .;;;;;;;;;;;;;;;;;;;; \n');
|
||||
response.write(' ;;;;;;;;:` `;;;;;;;;; ,;;;;;;;;.` .;;;;;;;; \n');
|
||||
response.write(' .;;;;;;, :;;;;;;; .;;;;;;; ;;;;;;; \n');
|
||||
response.write(' ;;;;;; ;;;;;;; ;;;;;;, ;;;;;;. \n');
|
||||
response.write(' ,;;;;; ;;;;;;.;;;;;;` ;;;;;; \n');
|
||||
response.write(' ;;;;;. ;;;;;;;;;;;` ``` ;;;;;`\n');
|
||||
response.write(' ;;;;; ;;;;;;;;;, ;;; .;;;;;\n');
|
||||
response.write('`;;;;: `;;;;;;;; ;;; ;;;;;\n');
|
||||
response.write(',;;;;` `,,,,,,,, ;;;;;;; .,,;;;,,, ;;;;;\n');
|
||||
response.write(':;;;;` .;;;;;;;; ;;;;;, :;;;;;;;; ;;;;;\n');
|
||||
response.write(':;;;;` .;;;;;;;; `;;;;;; :;;;;;;;; ;;;;;\n');
|
||||
response.write('.;;;;. ;;;;;;;. ;;; ;;;;;\n');
|
||||
response.write(' ;;;;; ;;;;;;;;; ;;; ;;;;;\n');
|
||||
response.write(' ;;;;; .;;;;;;;;;; ;;; ;;;;;,\n');
|
||||
response.write(' ;;;;;; `;;;;;;;;;;;; ;;;;; \n');
|
||||
response.write(' `;;;;;, .;;;;;; ;;;;;;; ;;;;;; \n');
|
||||
response.write(' ;;;;;;: :;;;;;;. ;;;;;;; ;;;;;; \n');
|
||||
response.write(' ;;;;;;;` .;;;;;;;, ;;;;;;;; ;;;;;;;: \n');
|
||||
response.write(' ;;;;;;;;;:,:;;;;;;;;;: ;;;;;;;;;;:,;;;;;;;;;; \n');
|
||||
response.write(' `;;;;;;;;;;;;;;;;;;;. ;;;;;;;;;;;;;;;;;;;; \n');
|
||||
response.write(' ;;;;;;;;;;;;;;;;; :;;;;;;;;;;;;;;;;: \n');
|
||||
response.write(' ,;;;;;;;;;;;;;, ;;;;;;;;;;;;;; \n');
|
||||
response.write(' .;;;;;;;;;` ,;;;;;;;;: \n');
|
||||
response.write(' \n');
|
||||
response.write(' \n');
|
||||
response.write(' \n');
|
||||
response.write(' \n');
|
||||
response.write(' ;;; ;;;;;` ;;;;: .;; ;; ,;;;;;, ;;. `;, ;;;; \n');
|
||||
response.write(' ;;; ;;:;;; ;;;;;; .;; ;; ,;;;;;: ;;; `;, ;;;:;; \n');
|
||||
response.write(' ,;:; ;; ;; ;; ;; .;; ;; ,;, ;;;,`;, ;; ;; \n');
|
||||
response.write(' ;; ;: ;; ;; ;; ;; .;; ;; ,;, ;;;;`;, ;; ;;. \n');
|
||||
response.write(' ;: ;; ;;;;;: ;; ;; .;; ;; ,;, ;;`;;;, ;; ;;` \n');
|
||||
response.write(' ,;;;;; ;;`;; ;; ;; .;; ;; ,;, ;; ;;;, ;; ;; \n');
|
||||
response.write(' ;; ,;, ;; .;; ;;;;;: ;;;;;: ,;;;;;: ;; ;;, ;;;;;; \n');
|
||||
response.write(' ;; ;; ;; ;;` ;;;;. `;;;: ,;;;;;, ;; ;;, ;;;; \n');
|
||||
response.write('\n');
|
||||
response.end();
|
||||
});
|
||||
|
||||
// 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);
|
||||
|
||||
// create a WebSocket server and attach it to the server
|
||||
var wss = new WebSocketServer({server: server});
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
// new connection, add message listener
|
||||
ws.on('message', function incoming(message) {
|
||||
// received a message
|
||||
console.log('received: %s', message);
|
||||
|
||||
// echo it back
|
||||
ws.send(message);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "node_test_server",
|
||||
"version": "0.0.1",
|
||||
"author": {
|
||||
"name": "Tom Igoe"
|
||||
},
|
||||
"dependencies": {
|
||||
"body-parser": ">=1.11.0",
|
||||
"express": ">=4.0.0",
|
||||
"multer": "*",
|
||||
"ws": "^1.1.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user