Control Your Raspberry Pi From Anywhere Using aREST

Control Your Raspberry Pi From Anywhere Using aREST

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:

Control Your Raspberry Pi From Anywhere Using aREST

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:

// Start
var 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.io
piREST.connect();

// Start server
var 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):

Control Your Raspberry Pi From Anywhere Using aREST

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:

Control Your Raspberry Pi From Anywhere Using aREST

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:

Control Your Raspberry Pi From Anywhere Using aREST

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

Control Your Raspberry Pi From Anywhere Using aREST

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!

Want to learn more about aREST?
If yes, join over 6,000 people who receive exclusive information about the aREST framework. You will also receive a free sample from my book, Discover the aREST Framework. Simply click on the button below!

Leave a Comment

Ignacio 4 months ago
When I use any other GPIO than GPIO4, I get "Error: GPIO17 is currently in use by /sys/class/gpio" and server shuts down. only way to get it back up is to restart the server?
Reply
Marco Schwartz Ignacio 3 months ago

Hi, seems like a GPIO library issue more than aREST. Make sure to contact us at [email protected] to get help :)

Johan 5 months ago
Hi, Followed these instructions, but when I do "sudo node cloud.js" i'm getting this error. Any idea how to fix this? [email protected]:~ $ sudo node cloud.js module.js:327 throw err; ^ Error: Cannot find module './../../index.js' at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object. (/home/pi/cloud.js:4:14) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) thanks
Reply
Marco Schwartz Johan 5 months ago
Hi, looks like you are still using the demo sketch - you need to include pi-arest inside the sketch and also install it from npm. Then it should work :)
duan.tranhuy 6 months ago
Hi guy, I run application successful, but when try to connect to application on cloud using cloud.arest.io/1024 so receive message '{"message":"Requested device not found"}'. How to i fix this problem? i son't see any error when setup pi-arest and run application. In src code is change piREST.set_id('p3gfct'); -> piREST.set_id('1024');
Reply
Marco Schwartz duan.tranhuy 5 months ago
I recommend using an ID with at least 6 characters, otherwise it can cause problems.
William paavola 7 months ago
Right now the system appears to be locked up. Cannot logout. Thanks. Unusual. Normally it works great
Reply
Marco Schwartz William paavola 5 months ago
This is now fixed :)
Raspix a year ago
I am very excited to use this for a pool pump control project. However, I am unable to access the cloud.arest.io server due to the server being down. Also there is no way to access the helpful guides on the get started page. Regardless, one could benefit from more documentation on how the variable and function elements are utilized on the dashboard. Do I need to include those functions and variables in the simple script used to connect the pi to the cloud server? It would be useful to be able to manipulate a variable based on another variable in a script, but then have the dashboard make those values visible. It would also be useful to use the dashboard to pass a discrete level to a variable which could be used to control several GPIO pins. I could see how this would work with the function element, but how do I pass parameters into that function. In other words, I want my script and the dashboard to be able to control and manipulate variables and GPIO and call functions.
Reply
Marco Schwartz Raspix a year ago
Thanks for the feedback! We had a lot of requests on the server lately, but now it's up again and holding well :) And thanks for all the feedback!
Paul M. a year ago
Thank you, Marco, for this application. It appears to be the easiest method to connect to my pi through the web. However, I am having a bit of difficulty getting it to work. I have installed node v4.3.2, but when I run the sudo node cloud.js command, it returns:Camera module offListening on port 80Should it also indicate that i am connected to aREST.io? When I test it on another laptop, using cloud.arest.io ...., I receive a bad gateway error 502.Not sure what I am doing wrong here. Thank you in advance for any help that you can offer.
Reply
Marco Schwartz Paul M. a year ago
Welcome! I answered to you via email :)
Eriksen 2 years ago
Hi, I'm running pi3 and get "command not found" when trying "sudo npm install pi-arest express". I've installed node v0.10.29 and saved the cloud.js text file in a separate folder. Any clue to what might resolve this..?
Reply
Shares
Control Your Raspberry Pi From Anywhere Using aREST Control Your Raspberry Pi From Anywhere Using aREST Control Your Raspberry Pi From Anywhere Using aREST Control Your Raspberry Pi From Anywhere Using aREST Control Your Raspberry Pi From Anywhere Using aREST Control Your Raspberry Pi From Anywhere Using aREST
Someone from India
Recently signed up
3 months ago
Someone from India
Recently signed up
5 months ago
Someone from France
Recently signed up
5 months ago
Someone from Japan
Recently signed up
5 months ago
11 people recently viewed this page
Someone from the United States
Recently visited this page
7 minutes ago
Someone from India
Recently visited this page
4 hours ago
Someone from India
Recently visited this page
4 hours ago
Someone from Belgium
Recently visited this page
5 hours ago
Someone from India
Recently visited this page
5 hours ago
Someone from India
Recently visited this page
6 hours ago