A RESTful environment for Arduino
Version 3.1.4
A simple library that implements a REST API for Arduino & the ESP8266/ESP32 WiFi chips.
Version 3.1.4: This version migrates to MQTT protocol for cloud connectivity, providing lightweight, reliable IoT communication with automatic reconnection and status LED feedback.
It is designed to be universal and currently supports REST calls via HTTP (using the CC3000 WiFi chip, the Arduino WiFi library or the Ethernet shield), via the Serial port (using the USB serial connection, Bluetooth, and XBee) and also via Bluetooth Low Energy.
It also works with the ESP8266 and ESP32 WiFi chips, operating as independent IoT devices that can be controlled from anywhere in the world via the aREST cloud.
The library is compatible with most ESP8266 modules & development boards (NodeMCU, Wemos D1, etc.), as well as most boards based on the ESP32 WiFi chip.
Compatible with: Uno, Mega, Due, Yun, Teensy 3.x, and MKR1000.
http://arduino.esp8266.com/stable/package_esp8266com_index.jsonhttps://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.jsonInstall these libraries via Arduino Library Manager (Sketch > Include Library > Manage Libraries):
esp32_living_room_a1b2c3)k8f92j4kd8f7g6h5j4k3l2m1)Important: Save your API Key! It's only shown once.
…Once connected, you can control your device via:
REST API:
# Set pin HIGH
curl https://cloud.arest.io/{device_id}/digital/5/1
# Read pin
curl https://cloud.arest.io/{device_id}/digital/5
# Call function
curl https://cloud.arest.io/{device_id}/led?params=1
# Get variable
curl https://cloud.arest.io/{device_id}/temperature
# Get device info
curl https://cloud.arest.io/{device_id}Dashboard:
The library uses the following MQTT topics:
| Topic | Direction | Purpose |
|---|---|---|
devices/{device_id}/commands |
Server → Device | Receive commands |
devices/{device_id}/response |
Device → Server | Send command responses |
devices/{device_id}/telemetry |
Device → Server | Send sensor data |
devices/{device_id}/state |
Device → Server | Send heartbeat/status |
Connect an LED to show connection status:
client.setStatusLED(2); // GPIO2 (built-in LED on most boards)| LED State | Meaning |
|---|---|
| Off | No WiFi connection |
| Fast blink | WiFi connected, connecting to MQTT |
| Slow pulse | Connected to cloud |
To use your own MQTT broker instead of mqtt.arest.io:
client.setServer("your-broker.com", 1883);/digital/8/0 - Set pin 8 LOW
/digital/8/1 - Set pin 8 HIGH
/digital/8 - Read pin 8/analog/6/123 - PWM write 123 to pin 6
/analog/0 - Read analog pin A0/mode/8/o - Set pin 8 as OUTPUT
/mode/8/i - Set pin 8 as INPUTExpose variables in your sketch:
int temperature = 25;
rest.variable("temperature", &temperature);Access via: /temperature
Define custom functions:
int ledControl(String command) {
int state = command.toInt();
digitalWrite(LED_BUILTIN, state);
return state;
}
rest.function("led", ledControl);Call via: /led?params=1
Send data to the cloud:
rest.publish(client, "temperature", 25); // Send immediately
rest.publish(client, "temperature", 25, 5000); // Send max every 5 secondsCommands received on the commands topic use JSON format:
{
"command_id": "cmd_123456",
"type": "function",
"function": "led",
"params": { "value": 1 }
}Supported command types:
digital_write - Write to digital pindigital_read - Read digital pinanalog_write - PWM writeanalog_read - Read analog pinfunction - Call custom functionget_variable - Get variable valueget_info - Get device infoAdd before including aREST.h:
#define DEBUG_MODE 1
#include If upgrading from version 3.0.x:
CloudClient with MQTTClient// Old (v3.0.x)
CloudClient client;
// New (v3.1.4)
MQTTClient client;You can also use aREST locally via HTTP:
#include
#include
aREST rest;
WiFiServer server(80);
void setup() {
// ... WiFi setup ...
server.begin();
rest.variable("temperature", &temperature);
}
void loop() {
WiFiClient client = server.available();
rest.handle(client);
}Access via: http://192.168.1.x/temperature
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
No open issues yet, or sync has not completed.