Internet Of Things

Sunday, 8 June 2014

Using the Raspberry Pi for web based Home Automation.....

In this tutorial I will show you how to use Raspberry Pi for home automation, as well as how to access your home automation systems from anywhere. : 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 web server based on Node.js to control the relay from a web browser. Sounds exciting ? Let’s dive in !

Hardware 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.

Software requirements

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://elinux.org/RPi_Easy_SD_Card_Setup
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:
sudo apt-get install nodejs npm
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:
npm install pi-gpio
If it doesn’t work, just restart the Pi and try again the same operation. You are now ready to go 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 schematic describes the whole project:
Relay_schem_small

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 AM
You 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.
All the files are included into a script file called server.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 a function to handle the incoming requests. This function will be called every time you access the project from your browser:
Inside this function, we get the data that is passed in the URL inside the state variable:
Then, we apply the command on the GPIO pin accordingly. For example, if the received state is “on”, we apply it to the GPIO pin where the relay is connected:
Finally, we send an answer to the browser, and close the connection:
And we end the script by actually starting the server:
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:
nodejs server.js
You should see the following message in your terminal:
Server has started.
You can now go to the web browser of your Pi, and type:
localhost/command?state=on
And the relay should turn on instantly. You can also check that the command was received inside the terminal. You can also do the same from your computer using the address of the Pi. For example, in my case:
192.168.0.47/command?state=off
This should turn the relay off again. Congratulations, you can now command your relay directly from your web browser!

Access the interface from anywhere

Now this is how to command a relay remotely, from your computer or your phone, from anywhere … in your home. Which is already nice, but I have been asked a lot about how to access this project from anywhere in the world. Wouldn’t it be cool to just access your home automation projects from wherever you are in the world, to check for example the status of your alarm system ? This is actually quite simple (depending on your router), and I will show you how.
Of course I can’t do a tutorial for every router in this world, but I will tell you how I did with mine (which is a router made by my Internet Service Provider). Generally you have to activate a mode named “remote access” on your router. Some routers will even give you an URL so you can access the router, but for mine I had to use the IP address. Be careful, I am not talking about the local IP address of your router (for example 192.168.0.254 for me), but the IP of the router on the web. It is generally easy to find in the interface of your router.
Then, you need to route the port corresponding to your Apache server on the Raspberry Pi (usually 80 if you didn’t change anything), so that every external access to this port goes directly to the Raspberry Pi. Usually, there is a “routing” menu in the interface of your router, where you can specify the external port (put 80), the internal port (80 as well) and the IP address (the one of the Raspberry Pi). Save the setting, and then it should just work out of the box if you type in the IP address of your router in any browser, wherever you are in the world !
One last word: be careful with this. Right now anyone can access this interface with the right IP address, and now it is just connected to a relay that is actually not connected to anything, but don’t do this if your whole alarm system is connected to the Raspberry Pi ! In this case, you better put a solid login/password system on your server so that only you can access it. I will show you how to do this in an upcoming article.
Update 06/05/14: Many of you had problems with the initial version of the project which was using a combination of HTML, Javascript and PHP. This was mainly due to file permissions inside the Apache web server on the Pi. That’s why I completely re-written the project using modern technologies like Node.js. Enjoy!
Finally, here is the list of the components that have been used in this tutorial:
- Raspberry Pi Model B
- Cobbler kit
- 5V relay
- 2N2222 transistor (or any equivalent NPN transistor)
1N4001 diode
- Red LED
- 1K ohm and 220 ohm resistors
- Breadboard and some jumper wires

1 comment:

  1. It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. camera systems mcallen

    ReplyDelete