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.

Control a Relay From Anywhere Using the Raspberry Pi

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 web server based on Node.js to control the relay from a 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 (A or B) 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 dongle if you are using the A model which doesn’t have an Ethernet port. In this tutorial, I used a Raspberry Pi model B with the WiPi dongle.

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:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb

Then, you will need to install the gpio-admin package. To do so, follow these steps in a terminal (if pi is your default username):

git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
make
sudo make install
sudo adduser pi gpio

After that, you’ll need to log out and in again.

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

git clone https://github.com/openhomeautomation/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:

sudo npm install pi-gpio express

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.

Connecting the Relay to The Network

Now it’s time to connect our project to the local network of your home. Again, make sure that the Raspberry Pi is connected to the local network, for example by pinging your own computer in a terminal. Also, check the IP of your Raspberry Pi, you will need it for later. You can do this by typing ifconfig in a terminal. This is the result for my Pi:

Screen Shot 2013-05-28 at 9.18.49 AMControl a Relay From Anywhere Using the Raspberry Pi schematicYou will use this address (192.168.0.47) to access the web page later on from your computer. What we have to do now is to build the Node.js script that will create our server on the Raspberry Pi. And from this server, we’ll be able to control the relay from your web browser. The following will simply be a walkthrough of the most important parts of the code. To actually make the project work, please refer to the files inside the GitHub repository of the project. If you want to learn more about Javascript, I recommend the excellent Introducing the JavaScript Language course.

The most important commands are included into a JavaScript file called app.js. Let’s see the details of this file. It starts by including all the required libraries, to start the web server and to control the GPIO pins:

Then, we create the server itself on port 3700 with:

Now, we need to create the different routes for our server, which will be the URLs that the user can type or get access to from the outside. The first one concerns the interface to control our relay:

The second route will be to send commands to the GPIO pin on which the relay is connected to (pin number 7 using the pi-gpio mapping). This is done by creating a new router:

Inside this function, we first process the incoming request, and print it on the console for debugging purposes:

 

For more detail: Control a Relay From Anywhere Using the Raspberry Pi

Scroll to Top