HTTP Request library for the ESP8266 using the Arduino development environment.
Forked from the original library which was for a regular Arduino plus Ethernet shield. This only required very minimal changes, to get it working with the ESP8266, thanks CSquared!.
For details on how to get started with the ESP8266 using the Arduino IDE take a look at Adafruit's tutorial.
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/alexmordue/arduino-restclient.git RestClient
Just include it and the ESP8266 library.
#include <ESP8266.h>
#include "RestClient.h"
You should setup the WiFi connection before making a request, look at the included examples for more details (if needed).
const char* ssid = "yourSSID";
const char* password = "yourPassword";
void setup(){
Serial.begin(115200);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Constructor to create an RestClient object to make requests against.
Use domain name and default to port 80:
RestClient client = RestClient("arduino-http-lib-test.herokuapp.com");
Use a local IP and an explicit port:
RestClient client = RestClient("192.168.1.50",5000);
All methods return an HTTP status code or 0 if there was an error.
Start making requests!
int statusCode = client.get("/"));
Pass in a string by reference for the response:
String response = "";
int statusCode = client.get("/", &response);
String response = "";
int statusCode = client.post("/", &response);
statusCode = client.post("/", "foo=bar");
response = "";
statusCode = client.post("/", "foo=bar", &response);
String response = "";
int statusCode = client.put("/", &response);
statusCode = client.put("/", "foo=bar");
response = "";
statusCode = client.put("/", "foo=bar", &response);
String response = "";
int statusCode = client.del("/", &response);
I test every way of calling the library (against a public heroku app)[https://github.com/csquared/arduino-http-test].
You can find the file in File->Examples->RestClient->full_test_suite
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.
csquared For all his work creating this library!