Raspberry Pi

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!

What You'll Need

ComponentQty
Raspberry Pi1
Cobbler kit1
5V relay1
2N2222 transistor (or equivalent NPN transistor)1
1N4001 diode1
Red LED1
1K ohm resistor1
220 ohm resistor1
Breadboard and jumper wires1 set

Step by Step

1

Gather Hardware and Software Requirements

<p>For this tutorial, 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.</p><p>On the software side, you will need a fully usable Raspberry Pi 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.</p><p>You also have to check that your Raspberry Pi is connected to the Internet. 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.</p>

2

Install Node.js and Required Dependencies

<p>The server part is based on Node.js, so you will need to install it on your Pi. Once Node.js is installed, download the GitHub repository of the project somewhere on your Pi from: https://github.com/makecademy/pi-node-relay</p><p>Go into the folder you just downloaded, and install the node.js module to interface directly with the GPIO pins of the Pi by running: npm install express pi-arest</p><p>If it doesn't work, just restart the Pi and try again the same operation. You are now ready to build the hardware!</p>

3

Understanding the Relay Circuit

<p>A relay is an electromagnetic switch. The one 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 used, so you should hear a 'click' when the relay is switching to another state.</p><p>However, there is 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.</p><p>We also need to place a diode for protection. The role of this diode will be to protect the low-power circuit when the relay is switching. Just place this diode in parallel of the input part of the relay, with the cathode connected to the positive 5V power supply.</p>

4

Configure Hardware Connections

<p>Connect the relay circuit components according to the wiring diagram. Start by connecting the relay coil to the transistor circuit, ensuring the transistor base is connected to a GPIO pin on the Raspberry Pi. Connect the 1K ohm resistor between the GPIO pin and the transistor base. Place the 1N4001 diode in parallel with the relay coil with the cathode facing the positive 5V line. Connect the LED to the relay switch output through the 220 ohm resistor to limit current. Ensure all ground connections are properly made and double-check polarity on all components.</p>

5

Create and Run the Node.js Server Script

<p>Create a Node.js script that connects your Raspberry Pi to the aREST.io cloud platform. The script uses the express framework and pi-arest module to interface with the GPIO pins. Here is the complete code for this part:</p><pre><code>// 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); });</code></pre><p>The script starts by including all the required libraries. Then, it sets 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. After that, it connects the Raspberry Pi to the cloud server and starts the server on the Pi.</p>

6

Test the Relay Control via Browser

<p>Go to the folder where you downloaded the files from the Github repository and run the Node.js script. Once the server is running, you should see a message in your terminal indicating it's listening on port 3000.</p><p>You can now go to any web browser, and navigate to the appropriate URL to test the relay. 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!</p>

7

Create a Web Dashboard Interface

<p>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. Visit the aREST dashboard website at http://dashboard.arest.io/ and create an account there. Create a new dashboard, for example called 'Raspberry Pi'.</p><p>After that, open this dashboard, and create a new element. 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. The application will automatically detect that your Raspberry Pi is online. You can now try the buttons: it should automatically turn the relay on or off. This dashboard is accessible from anywhere on the planet, so you can now control your relay from anywhere using this nice graphical interface!</p>

8

Extend the Project Further

<p>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.</p><p>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!</p>

Wrapping Up

Update 9/12/15: Many users had problems with the initial versions of the project which were using a combination of custom HTML & JavaScript code. This project has been completely re-written using the 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!