Version 2.0.0
A simple Node.js package that implements a REST API for the Raspberry Pi with MQTT cloud connectivity.
Version 2.0.0: This version uses MQTT protocol for cloud connectivity, providing lightweight, reliable IoT communication with automatic reconnection.
It is designed to be universal and currently supports REST calls via HTTP locally, and MQTT for cloud access from anywhere in the world.
- index.js: the package file
- examples: several examples using the pi-aREST package
The library is compatible with all Raspberry Pi boards, including:
- Raspberry Pi 5, 4, 3, 2, 1
- Raspberry Pi Zero / Zero W / Zero 2 W
- Raspberry Pi Pico (with appropriate Node.js runtime)
Install Node.js on your Raspberry Pi:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejssudo npm install pi-arest --unsafe-perm- Go to dashboard.arest.io
- Create an account or sign in
- Click "Add Device" in the dashboard
- Give your device a name (e.g., "Living Room Pi")
- The dashboard will generate:
- Device ID: A unique identifier (e.g.,
rpi_living_room_a1b2c3) - API Key: A secret key for authentication (e.g.,
k8f92j4kd8f7g6h5j4k3l2m1)
- Device ID: A unique identifier (e.g.,
Important: Save your API Key! It's only shown once.
var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);
// Device credentials from dashboard
piREST.set_id('rpi_living_room_a1b2c3'); // Your Device ID
piREST.setKey('k8f92j4kd8f7g6h5j4k3l2m1'); // Your API Key
piREST.set_name('Living Room Pi');
piREST.set_mode('bcm');
// Variables to expose
var temperature = 24;
piREST.variable('temperature', temperature);
// Custom function
function ledControl(params, callback) {
var state = parseInt(params) || 0;
piREST.digitalWrite(18, state);
callback(state);
}
piREST.function('led', ledControl);
// Connect to cloud
piREST.connect();
// Publish telemetry every 5 seconds
setInterval(function() {
temperature = 20 + Math.random() * 10;
piREST.setVariable('temperature', Math.round(temperature));
piREST.publish('temperature', Math.round(temperature));
}, 5000);
// Start local server
app.listen(80, function() {
console.log('Server running on port 80');
});sudo node your-script.jsOnce connected, control your device via:
REST API:
# Set pin HIGH
curl https://cloud.arest.io/{device_id}/digital/18/1
# Read pin
curl https://cloud.arest.io/{device_id}/digital/18
# 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:
- View real-time telemetry
- Send commands
- Create dashboards with widgets
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 |
To use your own MQTT broker instead of mqtt.arest.io:
piREST.setServer('your-broker.com', 1883);
piREST.connect();Or pass directly to connect:
piREST.connect('your-broker.com', 1883);/digital/18/0 - Set GPIO18 LOW
/digital/18/1 - Set GPIO18 HIGH
/digital/18 - Read GPIO18
Expose variables in your script:
var temperature = 25;
piREST.variable('temperature', temperature);
// Update variable value
piREST.setVariable('temperature', 30);Access via: /temperature
Define custom functions:
function motorControl(params, callback) {
var speed = parseInt(params) || 0;
// Control motor...
callback(speed);
}
piREST.function('motor', motorControl);Call via: /motor?params=100
Send data to the cloud:
piREST.publish('temperature', 25); // Send immediately
piREST.publish('temperature', 25, 5000); // Rate-limited (max every 5 seconds)Commands received on the commands topic use JSON format:
{
"command_id": "cmd_123456",
"type": "function",
"function": "led",
"params": "1"
}Supported command types:
digital_write- Write to digital pindigital_read- Read digital pinfunction- Call custom functionget_variable- Get variable valueget_info- Get device info
piREST.set_mode('bcm'); // BCM GPIO numbering (recommended)
piREST.set_mode('physical'); // Physical pin numberingYou can also access your Pi locally via HTTP:
# Local access
curl http://192.168.1.x/temperature
curl http://192.168.1.x/digital/18/1- Check your Device ID and API Key are correct
- Ensure your network allows outbound MQTT (port 1883)
- Check console output for error messages
- Ensure device shows as "online" in dashboard
- Check that the function/variable is registered
- Look at console output for incoming commands
Run with sudo:
sudo node your-script.jsOr add your user to the gpio group:
sudo usermod -a -G gpio $USERIf upgrading from version 1.x:
- The MQTT topic structure has changed
- Commands are now JSON format instead of path-based
- Add API key authentication with
setKey()
// Old (v1.x)
piREST.set_id('device_id');
piREST.connect();
// New (v2.0.0)
piREST.set_id('device_id');
piREST.setKey('your_api_key'); // New: API key authentication
piREST.connect();This work is licensed under the ISC License.