Family Encyclopedia >> Home & Garden

Raspberry Pi and Arduino Home Automation Guide

We've already discussed the relative merits of the Arduino and Raspberry Pi:each has its strengths. However, they don't have to be one or the other:combine them to get the best of both worlds. Home automation is the perfect candidate for this. The home automation market is awash with expensive consumer systems that are incompatible with each other and costly to install. If you own a Raspberry Pi Raspberry Pi:The Unofficial Tutorial Raspberry Pi:The Unofficial Tutorial Whether you're a current Pi owner who wants to learn more, or a prospective owner of this credit card-sized device, this is not a guide for you. I want to lose Read More

Update:Since writing this article, I have discovered OpenHAB, a free and open source home automation platform that runs on the Raspberry Pi and can be integrated with a wide variety of out-of-the-box home hardware as well as with Arduino. Watch the video below for a sneak peek, then head over to our Introduction to OpenHAB on Raspberry Pi Introduction to OpenHAB Home Automation on Raspberry Pi Introduction to OpenHAB Home Automation on Raspberry Pi OpenHAB is a mature open source home automation platform that runs on a variety of hardware and is protocol independent, which means it can connect to almost any home automation hardware on the market today. Read more guide to learn more..

Heimcontrol.js is a Node.js application built to run on the Raspberry Pi. Combined with an Arduino and some remote control plug-ins it makes it easy to control AC devices. You can add temperature sensors and even control your TV, but today we're going to keep things basic and extend the project in a later tutorial.

Here is a breakdown of the project:

  • The Raspberry Pi will act as the brain and gateway of operations; will run a Node application, linked to a Mongo database, and serve the UI to any web browser.
  • An Arduino, powered from the Pi, will interface between electronics, for example radio control power switches.

To do this, you will need:

  • Arduino and a Raspberry Pi
  • Some remote controlled sockets and controller (I used them)
  • Powered USB hub

Raspberry Pi and Arduino Home Automation Guide

Before we start, here is a demo video from the creator of the project himself.

Start over

We are going to use Raspian for this project. 11 Operating Systems You Can Run On Raspberry Pi. 11 Operating Systems You Can Run On Raspberry Pi. The Raspberry Pi hardware is just one side of the coin. Here are some different Raspberry Pi operating systems that you can install. Read More Download the latest Raspian image, copy it to your SD card, and make sure to expand the file system and enable SSH. The rest of this guide will assume that you have done so and are connecting via SSH using the default user.

If you haven't done it before, this video explains the process of preparing your SD card in OS X:

And this one for Windows users:

Preparation

The installation process is quite labor intensive, and is derived from the instructions here. Unfortunately, these weren't up to date or designed for Raspian, so I've tweaked them much lower. Most of the code below can be pasted in blocks, you don't need to paste them one by one. Since we're compiling some stuff on the Pi itself, keep in mind that this whole process will take a long time. I'd say make yourself a cup of tea, but when I say long, I mean the best part of the day, so 178 cups would be more appropriate.

All commands must be typed into Terminal, and you may need to press Enter at some points. These first commands will update the system and install the prerequisites:

sudo apt-get update sudo apt-get upgrade sudo apt-get install git-core git scons build-essential scons libpcre ++ - dev xulrunner-dev libboost-dev libboost-program-options-dev libboost-thread-dev libboost-filesystem-dev 

Next we need to install Node:

sudo mkdir / opt / node wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-arm-pi.tar.gz tar xvzf node-v0.10.2-linux-arm-pi. tar.gz sudo cp -r node-v0.10.2-linux-arm-pi / * / opt / node sudo ln -s / opt / node / bin / node / usr / local / bin / node sudo ln -s / opt / node / bin / npm / usr / local / bin / npm 

Add a PATH variable to your profile so the OS knows where the node is located. Use the nano text editor as follows:

sudo nano / etc / perfil 

Locate the line that says export path and replace it with:

NODE_JS_HOME = "/ opt / node" PATH = "$ PATH: $ NODE_JS_HOME / bin" export PATH 

Hit Ctrl-X to exit, and Y to save.

You may need to log out and back in for the route changes to take effect, but you can try the command:

cual nodo 

If you don't get any output pointing to the node binary, something went wrong.

Mongo

The next job is to install Mongo. Mongo is a document-based No-SQL database that is being used more and more by web applications. Unfortunately this will take forever to install as we have to compile it. While executing the following commands you will get many errors like:

entrada estándar: 13085: Advertencia: el uso de swp b está en desuso para esta arquitectura 

Do not worry about this. So run these commands to install Mongo:

git clone git: //github.com/RickP/mongopi.git cd mongopi scons sudo scons --prefix = / opt / mongo install scons -c 

When it's done, we first need a little more configuration to fix permission issues and make sure it's running at startup.

sudo useradd mongodb sudo mkdir / data / dbb sudo chown $ USUARIO / data / db cd /etc/init.d sudo wget -O mongodb https://gist.github .sh sudo chmod + x mongodb sudo update-rc.d mongodb por defecto mongod

This last command will start the Mongo server and you will need to open a new Terminal to continue with the other commands. I'm not entirely sure about this step, so if anyone can correct me in the comments on how to do mongod launch automatically on startup, it would be greatly appreciated. For now, it works, but not elegantly.

sudo shutdown -r ahora

Finally, it's time to install the Node Heimcontrol.js application.

cd ~ pi git clone git: //github.com/ni-c/heimcontrol.js.git cd heimcontrol.js npm install 

You can start running the application by typing

 nodo heimcontrol.js 

At this point you should be able to access the control interface with http://localhost:8080 from the Pi, or replace localhost with the IP address if you're accessing from another computer (and you can also set up port forwarding What is port forwarding and how can it help me? [MakeUseOf Explains] What is port forwarding &How can you help me? [MakeUseOf Explains] Do you cry a little inside when someone tells you there's a port forwarding problem and that's why your shiny new app doesn't work?Your Xbox won't let you play games, your torrent downloads Reject... Read More

Hardware

I would eventually like a hardwired relay, but for now I'll use the safer option of radio controlled switches.

I've used some reasonably cheap £20 sets from Maplin that come with 3 sockets, and opened up the remote so I could interface directly with the 433MHz chip inside. I found the instructions for this here.

Raspberry Pi and Arduino Home Automation Guide

You can also purchase individual off-the-shelf 433 MHz transmitters on eBay or from hobby electronics vendors. All you need is to connect the VCC to 5V on the Arduino, the GNDs, and a single control pin; remember which one you used. (Scheme by Willi Thiel)

Raspberry Pi and Arduino Home Automation Guide

The plugin works by sending “sad codes”, but these will vary by manufacturer. See the RCSwitch documentation to find its exact codes. This wiki guide can help too.

Communication with the Arduino is done through a node library called duino. Stop the Heimcontrol application if it is running and install the Arduino bridge using the following command.

npm instalar duino 

The Arduino must have this code loaded; I suggest you copy and paste it to install from another computer. Essentially, it's a listener that responds to serial commands from the Pi, but there's nothing to stop you from extending it with your own functions.

Raspberry Pi and Arduino Home Automation Guide

With the web application launched, go to the Settings menu> Arduino .

Raspberry Pi and Arduino Home Automation Guide

From there you can add a new item, selecting the RCSwitch method, your transmitter pin, and the tri-state address code. Remember to save, then go back to the main screen to see your new button.

Raspberry Pi and Arduino Home Automation Guide

Crazy:

After many hours of debugging the code I discovered that single digit pin numbers did not work. Make sure your transmitter is placed on the pin. 10 to be sure.

I also found that the Arduino plugin was coded with the wrong trailing bits for the triple codes my receivers needed. A little explanation first:triple codes consist of 3 bytes of information. The first gives us the network number (1-4) and the second gives the address of the transceiver (again, 1-4, yielding a maximum of 16 addressable sockets). The final byte consists of two padding bits, plus 2 enable/disable bits. Unfortunately, the trailing byte is encoded in the Arduino plugin, and in my case the on/off code was wrong.

I had to manually edit the plugins/arduino/index.js Use the correct codes. If you're using the same remote control plugs as I am, change lines 80 and up to:

// Enviar código RC si (item.value) return that.pins [item.pin] .triState (item.code + "FFFF"); // cambio de FF0F else return that.pins [item.pin] .triState (item.code + "FFF0"); // cambio desde FF00

Here is a demo video of everything that works:

I'm going to leave it here at this point, but sensor readouts and IR remotes are also supported. I'll probably revisit them at a later date with a few more improvements. If all of this has been too complex for you, maybe check out these Arduino Projects for Beginners 15 Great Arduino Projects for Beginners 15 Great Arduino Projects for Beginners Interested in Arduino but not sure where to start? Here are some of our best Arduino projects to get beginners started! Read more.