We’ve already talked about the relative merits of Arduino and Raspberry Pi — they each have their own strengths. They don’t have to be either — combine them to get the best of both worlds. Home automation is the perfect candidate for this. The home automation market is flooded with expensive consumer systems that are incompatible with each other and costly to install. If you have a Raspberry Pi and Arduino, you can basically do the same for a fraction of the price if you’re willing to put in the time and effort.

Update: Since this article was written, I’ve discovered OpenHAB, a free and open source home automation platform that runs on the Raspberry Pi and can be integrated with a huge range of prebuilt smart home kits as well as Arduino. Watch the video below to see the top of the line, then skip to Getting Started with OpenHAB for Raspberry Pi guide to learn more.

Heimcontrol.js is a Node.js application built to run on the Raspberry Pi. Combined with an Arduino and some standard remote control sockets, this makes it easy to control AC devices. You can add temperature sensors and even control your TV — but today we’ll stick to the basics and expand on the project in the next tutorial.

Here is the breakdown of the project:

  • The Raspberry Pi will act as the brain and gateway of operations — it will run a Node application tied to a Mongo database and provide a front-end interface to any web browser.
  • The Arduino, powered by the Pi, will communicate between the electronics — the radio control’s power switches.

For this you will need:

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

RC sockets

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

Start over

We are going to use Raspian for this project. and I highly recommend starting with a fresh install if you’ve done other hacks and such before. Download the latest Raspian image, copy it to your SD card, and be sure to expand the filesystem and enable SSH. The rest of this guide assumes that you have done this and are connecting via SSH using the default user.

If you haven’t already, 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 laborious, and follows from the instructions here. Unfortunately they were outdated or not intended for Raspian, so I have adjusted them significantly below. The codes below can basically be pasted in blocks — you don’t have to paste one by one. Since we’re putting together a few things on the Pi itself, keep in mind that this whole process will take a long time. I’d say go 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 entered into the Terminal and you may need to press Enter at some points. These first few 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 Node. Use the nano text editor like this:

 sudo nano /etc/profile 

Find the line that says export PATH and replace it with:

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

Press Ctrl-X to exit and Y to save.

You may need to logout and log back in for the path changes to take effect, but you can check with the command:

 which node 

If you didn’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 increasingly being used by web applications. Unfortunately this will take forever as we have to compile it. When you run the following commands, you will get many errors such as:

 {standard input}:13085: Warning: swp{b} use is deprecated for this architecture 

Do not worry about it. 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 

Once we’re done, we need to do a bit more tweaking to fix permission issues and make sure it runs on startup.

 sudo useradd mongodb sudo mkdir /data/dbb sudo chown $USER /data/db cd /etc/init.d sudo wget -O mongodb https://gist.github.com/ni-c/fd4df404bda6e87fb718/raw/36d45897cd943fbd6d071c096eb4b71b37d0fcbb/mongodb.sh sudo chmod +x mongodb sudo update-rc.d mongodb defaults mongod 

This last command will start the Mongo server and you will need to open a new terminal to continue with other commands. I’m not entirely sure about this step, so if someone can fix it in the comments on how to automatically start mongod on startup, that would be very helpful. While this works, it’s not elegant.

 sudo shutdown -r now 

Finally, it’s time to install the Heimcontrol.js Node app.

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

You can start the application by typing

 node heimcontrol.js 

At this point, you should be able to access the management interface with http://localhost:8080 from the Pi, or replace localhost with an IP address if you’re accessing it from another computer (and you can also set up a Port Forward to access it from anywhere in the world), so I’d suggest you search and check all the database work before connecting the Arduino.

hardware

After all, I’d love to have a wired relay, but for now I’ll use the safer option of radio controlled switches.

I used some inexpensive £20 kits from Maplin that come with 3 connectors and opened up the remote so I could directly interface with the 433MHz chip inside. I found instructions for this here.

RC controller-bypass

You can also purchase individual 433MHz transmitters ready to use on eBay or hobby electronics suppliers. All you need is to connect VCC to 5V on the Arduino, GND and one control pin — remember which one you used. (Scheme by Willy Thiel)

arduino-rcswitch

The plugin works by sending «three-way codes», but these will vary depending on the manufacturer. Refer to the RCSwitch documentation to find your exact codes. This wiki guide may also help.

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

 npm install duino 

The Arduino should download this code — I suggest you copy and paste it in order to install it from another computer. It’s essentially a listener program that responds to serial commands from the Pi, but there’s nothing stopping you from extending it with your own functionality.

Radio Controlled Settings

After launching the web application, go to the menu » Settings» > » Arduino .

heimcontrol-menu-settings

From there, you can add a new item by selecting the RCSwitch method, your transmitter’s contact, and the Tristate address code. Don’t forget to save and then return to the main screen screen for the new button.

rcswitch-setup

errors:

After many hours of debugging the code, I found that single digit pin numbers don’t work — make sure your transmitter is set to pin ten, to be sure.

I also discovered that the Arduino plugin was hardcoded with the wrong end bits for the tri-state codes my receivers need. First, a little explanation: 3-way codes consist of 3 bytes of information. The first gives us the network number (1-4) and the second gives us the transceiver address (again 1-4, creating a maximum of 16 addressable sockets). The last byte consists of 2 padding bits, plus 2 bits for on/off. Unfortunately the last byte is hardcoded into the Arduino plugin — and in my case the on/off code was wrong.

I had to manually edit plugins/arduino/index.js, to use the correct codes. If you are using the same remote connectors as mine, change lines 80 onwards to:

 // Send RC code if (item.value) { return that.pins[item.pin].triState(item.code + "FFFF");//change from FF0F } else { return that.pins[item.pin].triState(item.code + "FFF0");//change from FF00 } 

Here is a demo video of how it all works:

I’m going to leave this here, but readings from sensors and IR remotes are also supported. I will probably come back to them later with some additional improvements. If all of this was too much for you, maybe check out these Arduino friendly projects.

Похожие записи