My image

Control a Relay From Anywhere Using the Raspberry Pi

If you found this article after doing a search on Google, welcome! On this website you will find plenty of content around DIY home automation using open-source hardware. Enjoy the article!

I have been asked a lot about writing tutorials using the Raspberry Pi for home automation, as well as how to access your home automation systems from anywhere. And this is exactly what I will show you in this tutorial: you are going to learn how you can control a relay that is attached to your Raspberry Pi, from any device like your computer or smartphone, and from anywhere in the world.

In this project, we will connect the relay to a simple LED, but of course this LED could be replaced by anything like a lamp, some lights on the ceiling, or the motor of your electric curtains. And to control this relay from anywhere, we are going to run a small app based on Node.js to control the relay from any web browser. Sounds exciting ? Let’s dive in !

Hardware & Software Requirements

For this tutorial, of course you will need a Raspberry Pi board. The version of the board or the model doesn’t really matter, but keep in mind that you will have to connect it to your local network, so you will need a WiFi model or use a WiFi dongle. In this tutorial, I used a Raspberry Pi model B with a WiFi dongle, but it would work just fine with the latest versions of the Raspberry Pi.

For the components, you will need a small 5V relay, a P2N2222A transistor (but any similar NPN transistor will do the job), a 1N4001 diode, a 1K ohm resistor, a 220 ohm resistorone LED, and of course a breadboard and some jumper wires. To connect the Raspberry Pi to the breadboard, I also used the Adafruit cobbler kit, but any cobbler kit for the Raspberry Pi will work fine.

This is the list of the components that have been used in this tutorial:

On the software side, you will need a fully usable Raspberry Pi. And by usable I mean already configured with the Raspbian Linux distribution installed on it. There are many tutorials on the web that will guide you through the installation of Raspbian on your Pi, but I recommend this one:

http://www.raspberrypi.org/documentation/installation/installing-images/

You also have to check that your Raspberry Pi is connected to the Internet. Again, this will depend on your configuration (Ethernet or WiFi) and your router, but is usually really easy. If you are using the Ethernet connection, simply connect a cable to your router and it should work automatically. If you’re using a WiFi dongle, the easiest solution is to use the GUI that comes with Raspbian to find your wireless network and enter your WEP/WPA password.

The server part is based on Node.js, so you will need to install it on your Pi with the following instructions:

https://www.golinuxcloud.com/install-nodejs-and-npm-on-raspberry-pi/

Now, you need to download the GitHub repository of the project somewhere on your Pi:

https://github.com/makecademy/pi-node-relay

Finally, you need to go into the folder you just downloaded, and install the node.js module to interface directly with the GPIO pins of the Pi:

npm install express pi-arest

If it doesn’t work, just restart the Pi and try again the same operation. You are now ready to build the hardware!

Hardware Configuration

There is quite a lot of hardware to connect for this project, so pay attention to this paragraph. First, let’s speak about the relay itself. A relay is an electromagnetic switch. The one I used in this project basically has 2 parts. The first part, the coil, is the low-power part of the circuit, and will be controlled by the Raspberry Pi. The second part of the relay is the switch, which can sustain higher powers. This part is actually mechanical on the relay I used, so you should hear a “click” when the relay is switching to another state. Activating the low-power part by applying 5V on the coil will activate the switch and change the state of the relay. To monitor in which state the relay is, I used one LED on one part of the switch. Of course, the LED can be replaced by any device you want to switch on or off, for example a lamp.

But … there is still a problem. The relay is rated at 5V to switch, and the Raspberry Pi GPIO port can only deliver 3.3V. This is why we need a transistor in between to activate the relay. The transistor is basically a solid-state switch, which will be activated via the digital output of the Raspberry Pi board. When the transistor is on, the 5V pin of the Raspberry Pi board will directly power the relay, thus making the relay switch.

We still need to place one component I haven’t spoken about yet: the diode. The role of this diode will be to protect the low-power circuit when the relay is switching. In case any current is flowing through the input of the relay because of the switching of the high-power part, it will just flow through this diode instead of destroying the transistor or the output of the Raspberry Pi. Just place this diode in parallel of the input part of the relay, with the cathode connected to the positive 5V power supply.

This is how it should look like at the end:

Connecting the Relay to The Cloud

Now it’s time to connect our project to the cloud. Again, make sure that the Raspberry Pi is connected to your local network & to the web, for example by trying to ping a web page from a terminal.

What we have to do now is to build the Node.js script that will connect the Raspberry Pi to the aREST.io cloud platform, which is a tool that I created specifically to control boards like the Raspberry Pi remotely. And from this cloud server, we’ll be able to control the relay from your web browser. Here is the complete code for this part:

// Modules
var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);

// Set unique ID
piREST.set_id('p5dgwt');
piREST.set_name('pi_cloud');

// Connect to cloud.aREST.io
piREST.connect();

// Start server
var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

Let’s now see the details of this file. It starts by including all the required libraries:

var express = require('express');
var app = express();
var piREST = require('pi-arest')(app);

Then, we’ll set a unique ID and a name to your board. Make sure to change at least the ID here, so you are sure that only you can access the device:

piREST.set_id('p5dgwt');
piREST.set_name('pi_cloud');

After that, we connect the Raspberry Pi to the cloud server with:

piREST.connect();

Finally, we start the server on the Pi:

var server = app.listen(3000, function() {
    console.log('Listening on port %d', server.address().port);
});

It’s now time to test the project. To do so, simply go over to the folder where you downloaded the files from the Github repository for this project and type in a terminal:

sudo npm install pi-arest express

After that, type:

You should see the following message in your terminal:

You can now go to any web browser, and type:

You should immediately hear the relay turning on. You can of course do the same with a 0 at the end to turn the relay off again. Congratulations, you can now command your relay directly from any web browser!

Access The Interface From Anywhere

You can now control the relay from anywhere, but only by typing commands in your web browser. That’s nice, but it would be much better if you could do the same from a web-based dashboard, right. Well, that’s what I did inside the aREST framework, and I created a way to easily create a dashboard for devices connected to aREST.io.

The first step is to visit the aREST dashboard website:

http://dashboard.arest.io/

Create an account there, and then create a new dashboard, for example called Raspberry Pi.

After that, open this dashboard, and you will be able to create a new element.

Here, give a name to your dashboard element, and enter the ID you assigned to the device in the ‘device ID’ input. Also set the element type to ‘Digital’, on pin 7, and with an On/Off switch.

As you can see, the application automatically detected that your Raspberry Pi was online. You can now try the buttons: it should automatically turn the relay on or off. This dashboard is of course accessible from anywhere on the planet, so you can now control your relay from anywhere using this nice graphical interface!

Update 9/12/15: Many of you had problems with the initial versions of the project which were using a combination of custom HTML & JavaScript code. That’s why I completely re-written the project using my own aREST framework, which made the project much easier to build. You can now build your own graphical interface without any code, and control your Raspberry Pi from anywhere. Enjoy!

How to Go Further

There are many things you can do to improve this project with what you just learnt. You can connect more relays to the project, and command them all from the same interface. You can also install several Raspberry Pi’s in your home to command them all separately. You might also use what you learned in this project to read data from the GPIO pins (for example from a light or motion sensor), and display this data on the web interface we created in the project. Finally, if you built an exciting project based on this tutorial, please share in the comments!