My image

Control Your Raspberry Pi From Anywhere Using aREST

Last Update: Tue, May 23 2023

The Raspberry Pi is an amazing board that can be used for a wide range of applications, from being used as the hub of an home automation system to controlling a mobile robot. It’s also a great board to build Internet of Things (IoT) projects, as it easy to connect to the Internet & can be interfaced with a lot of other components.

However, it can sometimes be intimidating to configure your Raspberry Pi and then connect it to an Internet of Things platform. This is one of the main reason why I created aREST, which is now also compatible with the Raspberry Pi. In this project, we are going to see how to use the aREST framework on the Raspberry Pi, so you can easily control your board from anywhere in the world. Let’s start!

Hardware & Software Requirements

Let’s first see what we need for this project. For the Raspberry Pi itself, you can use any model. I used a Raspberry Pi 2 board for this project, but you could perfectly use a Raspberry 1, 3, or Zero.

To illustrate the behaviour of the project, I’ll simply use an LED & a 220 Ohm resistor that we’ll connect to the Raspberry Pi.

Finally, you will need a breadboard and some jumper wires.

On the software side, you will need to have Node.js installed on your board. You can for example learn how to install it by following this tutorial:

https://blog.wia.io/installing-node-js-on-a-raspberry-pi-3

Hardware Configuration

Let’s now see how to configure the project. As we’ll only connect an LED to the Pi, it will be quite simple. First, place the LED in series with the resistor on the breadboard, with the longest pin of the LED connected to the resistor. Then, connect the other end of the resistor to GPIO3 of the Pi (pin 5), and the other end of the LED to one Ground pin of the Pi

This is the final result:


Once this is done, make sure your Raspberry Pi is also connected to the Internet, via WiFi or Ethernet.

Connecting Your Raspberry Pi to the aREST Cloud

We are now going to configure the Raspberry Pi so it connects to the aREST cloud server, that will allow you to control it from anywhere in the world. This is the complete code for this project:

// Startvar express = require('express');var app = express();var piREST = require('pi-arest')(app);piREST.set_id('p3gfct');piREST.set_name('pi_cloud');piREST.set_mode('bcm');// Connect to cloud.aREST.iopiREST.connect();// Start servervar server = app.listen(80, function() {    console.log('Listening on port %d', server.address().port);});

Make sure to modify the ID of the board in the set_id() function: this is what will identify the board on the server. Then, place the code inside a file called cloud.js on your Raspberry Pi. Inside the same folder, type the following command inside a terminal:

sudo npm install pi-arest express

This will install the required modules for the project. Then, start the software with:

sudo node cloud.js

You Raspberry Pi should then be connected to the aREST cloud server. You can now test it by entering the following command in any web browser (make sure to change the ID of the board by the one you set in the code):


You should immediately see that the LED is turning on, meaning your Pi can now be controlled from anywhere in the world!

Controlling Your Raspberry Pi From Anywhere

This is already great, but we can do better: create a simple dashboard that will allow you to control your Raspberry Pi from anywhere. For that, we’ll use the dashboard functionalities of aREST. To get started, simply create an account at:

http://dashboard.arest.io/

There, you will be able to create a new dashboard:


In order to control the LED, we’ll create a simple On/Off element inside this dashboard. Create an element with the following parameters, of course by setting the ID of your board:


You should immediately see the new element inside the dashboard, along with the current status of the board:


You can now try to use the buttons: your Raspberry Pi should respond nearly instantly. Of course, as this dashboard is also in the cloud, you can now use it to control your Raspberry Pi from anywhere on the planet!

How to Go Further

In this tutorial, we saw how to use the aREST framework to control a Raspberry Pi from anywhere in the world. We saw how to use the aREST dashboard to control a simple LED, but you could of course directly call the aREST cloud server from any web-based application.

You can of course do much more using what you learned in this project. For example, you can also use aREST to read data from the board and have it displayed inside the same dashboard. You can then apply what you learned in this project for all your IoT projects, for example to monitor your home remotely or control a flying drone from anywhere in the world!