HTTP Request library for Arduino.
This library is derived almost entirely from the great work done here: https://github.com/csquared/arduino-restclient
The library comes with a number of example sketches. See File > Examples > PlugPlayREST within the Arduino application.
The library uses the Arduino Ethernet Client api for interacting with the underlying network hardware. This means it Just Works with a growing number of boards and shields, including:
- Arduino Ethernet
- Arduino Ethernet Shield
- Arduino YUN โ use the included
YunClient
in place ofEthernetClient
, and be sure to do aBridge.begin()
first - Arduino WiFi Shield
- Sparkfun WiFly Shield โ library
- TI CC3000 WiFi - library
- Intel Galileo/Edison
- ESP8266
Clone (or download and unzip) the repository to ~/Documents/Arduino/libraries
where ~/Documents/Arduino
is your sketchbook directory.
> cd ~/Documents/Arduino
> mkdir libraries
> cd libraries
> git clone https://github.com/plugplayco/plugplay-arduino-rest.git PlugPlayREST
You need to have the Ethernet
library already included.
#include <Ethernet.h>
#include <SPI.h>
#include "PlugPlayREST.h"
Constructor to create an PlugPlayREST object to make requests against.
Use domain name and default to port 80:
PlugPlayREST client("plugplay.co");
Use a local IP and an explicit port:
PlugPlayREST client("192.168.1.50",5000);
Set authentication to connect to PlugPlay
Arguments:
- userKey - user Key of your user account.
- boardId - board Id of board you want to GET/PUT to.
Sets up EthernetClient
with a mac address of DEADBEEFFEED
. Returns true
or false
to indicate if setting up DHCP
was successful or not
client.dhcp()
Note: you can have multiple PlugPlayREST objects but only need to call this once.
Note: if you have multiple Arduinos on the same network, you'll need to give each one a different mac address.
It just wraps the EthernetClient
call to begin
and DHCPs.
Use this if you need to explicitly set the mac address.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
if (client.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
You can skip the above methods and just configure the EthernetClient yourself:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192, 168, 2, 11 };
Ethernet.begin(mac,ip);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Ethernet.begin(mac);
This is especially useful for debugging network connection issues.
Create a message to send a PUT request
Arguments:
- dvName - device name
- data0 - first data
- data1 - second data
- data2 - third data
All methods return an HTTP status code or 0 if there was an error.
Start making requests!
int statusCode = client.getPlugPlay("inTopic");
Pass in a string by reference for the response:
String response = "";
int statusCode = client.getPlugPlay("inTopic", &response);
int statusCode = 0;
char* msg = client.createMsg("arduino", 0, 1, 2);
statusCode = client.putPlugPlay("outTopic", msg);
String response = "";
statusCode = client.putPlugPlay("outTopic", msg, &response);
Get device name from a received response
String response = "";
int statusCode = client.getPlugPlay("inTopic", &response);
client.getName(response.c_str());
Get data from a received response
String response = "";
int statusCode = client.getPlugPlay("inTopic", &response);
// Get the first data
client.getData(response.c_str(),"data0");
// Get the second data
client.getData(response.c_str(),"data1");
// Get the third data
client.getData(response.c_str(),"data2");
You can refer here: https://github.com/csquared/arduino-restclient
If you're having trouble, you can always open RestClient.cpp
and throw at the top:
#define HTTP_DEBUG
Everything happening in the client will get printed to the Serial port.
This code is released under the MIT License.