Hack Your House: Making the Most of Raspberry Pi

by Michael Post

After recently moving, it was soon realized that my home that has been abandoned for approximately 15 years was going to need some serious TLC - first and foremost being water and power.  Water was a breeze: run some pipe, call the city.  A few days later, voilà - water.  Power, on the other hand, was a bit different, replacing fabric wrapped wire with proper wire, replacing fuse boxes with a breaker box, a new meter box just so the power company would come out to turn it on.  Once that was done, I felt pretty comfortable dealing with AC power.

So I decided to introduce my house to the Raspberry Pi.  First was to decide on an OS for the Pi.  After a little consideration, I decided on Raspbmc - main reasons being, first, I was already running MediaTomb on my laptop and, although the Pi is a little light on processing and RAM, it thus far has made an excellent head unit.  Second was hardware - what would I have to buy, what could I fabricate myself, and how much would it cost?  Lastly, and quite possibly most importantly, what would be the best way to communicate with the GPIO pins on Pi?

Raspbmc requires very little to no configuration to get up and running and plays all matter of file format streams.  Plus there is pre-built smartphone apps for iOS and Android, very convenient for couch sitting, or armchair, if that is your thing.

On to the hardware.

First, the Pi was going to need power.  I figured probably the more the better, so I chose an LM2596S.  It can take from 36 VDC in and step it down to 1.5 VDC and is rated up to 2A.  It seemed to be a pretty good choice at the time and I haven't been disappointed.

Any old power supply will do.  I have a 12 volt, 2.67 amp power supply and it works fine with the converter and the Pi.  It will power all nine pins I'm currently using plus the Pi and USB keyboard at 5.5 volts input, so that is a great thing.  (I'm not sure if a regular cell phone charger can power the above without a voltage drop.)

Second, I needed switches.  The first set of solid-state relays I bought was from SainSmart on Amazon for about $16.  The next two I bought were knockoffs at $8 apiece, but, as far as I can tell, they're just about identical.  All three have four inputs, four outputs, and run on 5 volts.

Lastly was wire and connectors.  I used 14/2 wire (left over from wiring my house) for the lights to relays and some Plain Jane Cat 5e for the GPIO pins to the input pins on the relays.  I used female-to-female jumper wires cut in half and soldered to the Cat 5e to complete the connection from Pi to relay, and I found some lever nuts to use between the lights, relay, and breaker box.

HTTP, the sweet jelly filling.  After kicking a few ideas around my laboratory, I decided that for me, controlling the Pi via web page was the most ideal.  Mostly, because writing separate apps for Android and iOS seems time consuming and a little daunting.

Now, with a little HTML and JavaScript, you can execute the commands needed to turn your GPIO pins from any device connected to your network that has a web browser via CGI scripting.  My Pi was running Raspbmc which already has a web site used to remotely control XBMC.

I added another step.  I host my website on my laptop and execute the CGI scripts via SSH.  My reasoning for this was to keep as much heavy lifting on the server side.  Running Cron to automate and run the full-blown web server just seemed more reasonable.  I didn't want my movies or music or whatnot to deteriorate because of programs running in the background on my Pi.

So, to achieve this, first you need to create an RSA key for your laptop to your Pi, then write your scripts on your Pi end.  There are a lot of ways to activate the GPIO pins on the Pi.  I chose to just use bash scripting - it's quick and efficient.  I also used shell scripting on the server end for the same reason.

The Pi has three scripts: one to activate the Pi's GPIO pins, one to turn the relay ON, and one to turn the relay OFF.

The script to activate the pin is in /etc/init.d/ and looks like so:

gpio-activate.sh:

#!/bin/bash
# GPIO Activate Script
# Place in /etc/init.d/
echo 24 > /sys/class/gpio/export
echo out > direction
echo 0 > value
echo 1 > value

The other two are in ~/ (home directory) and are as follows.

To turn the lights on:

turn-on.sh:

#!/bin/bash
# Turn On
echo 1 > /sys/class/gpio/gpio24/value

And to turn them off:

turn-off.sh:

#!/bin/bash
# Turn Off
echo 0 > /sys/class/gpio/gpio24/value

On the server side, I have a lot of scripts, but there are four basic ones.

The first two are in ~/.

This is an example to run the GPIO active script via SSH:

#!/bin/bash
ssh root@IP_ADDRESS_OF_PI /etc/init.d/gpio-activate.sh

This is an example of how to run the "turn on or off" scripts via SSH:

#!/bin/bash
ssh root@IP_ADDRESS_OF_PI ./script_for_on_or_off > /dev/null

The next is a CGI script to execute the relay on/off scripts.

I could have streamlined this and just written the whole script with the CGI scripts.  I used the first two for testing purposes though, so I just left it as is.  Most of the CGI scripts look as so.

They are in /usr/lib/cgi-bin:

turn-on.cgi:

#!/bin/bash
# This first part keeps your browser from switching pages.
# Except on my iPhone, still looking for a work around there.
echo "No content"
echo "text/plain"
echo ""

~/script_for_on

# This next part is here to edit my website that lets me know
# what lights are currently off or on.
sed -i 's/offbutton/onbutton/' /var/www/index3.html

turn-off.cgi:

#!/bin/bash
# This first part keeps your browser from switching pages.
# Except on my iPhone, still looking for a work around there.
echo "No content"
echo "text/plain"
echo ""

~/script_for_off

# This next part is here to edit my website that lets me know
# what lights are currently off or on.
sed -i 's/onbutton/offbutton/' /var/www/index3.html

That was the best way I could figure to get accurate feedback on what was currently on or running or not.  In my opinion, sed is probably one of the greatest tools in any shell scripter's tool bag.

So what I ended up with was a PC that can run my lights, play streaming media on my TV, and, with a little creativity, run just about anything in my home.

It cost me a little over $150 for the Pi, DC-to-DC power converter, 250' 14/2 electrical wire, female-to-female jumper wires, and lever nuts.  The Cat 5e and everything else I used I had available.

To control every ceiling light in my house, I think it's a pretty cheap route.

Code: gpio-activate.sh

Code: turn-on.sh

Code: turn-off.sh

Code: turn-on.cgi

Code: turn-off.cgi

Return to $2600 Index