Tuesday, March 12, 2013


I'm in the habit of naming anything I build with the suffix "-OTRON9000". Always good for a laugh. More so when such hacks make it into production on client's systems or become useful to all and sundry :)

This quick post is about how to hack a gate remote, graft it onto a Raspberry Pi, and have a web server controlled system up and running in about an hour, so everyone in your company can open and close the gate without running around to find a remote...

No doubt I'll pretty-fy the code listings, some day, but you should get the idea here.

First, THANK YOU Raspberry Pi people for making a great piece of kit. The Raspberry Pi (in case you've been in a cave for about two years) is an ARM based computer on a little board the size of a credit card (http://www.raspberrypi.org/). It's a full blown computer, only nice and cheap. And it's got some pins on it, which allow you to talk to the outside world. Here's the one I used:

My Pi - with power input (blue), Network(grey) and output pins all wired up (red)
This one has the stock standard Raspbian image on it, possibly with a custom build of Emacs 24 on it. I don't remember spending too much time getting Emacs running on it, so it's possible that either Raspbian now comes with Emacs 24, or that the installation process for building from scratch is painless. Anyway - the other tweak is that the IP address is static. I did this by tweaking the /etc/network/interfaces file to look something like this:

iface eth0 inet static

Right now, the Pi plugs directly into a wireless router. Much simpler this way.

Then, getting the pins set up so we can do fun things with them: I found the most basic example Gordon had put together (big THANKS again): https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/gpio-examples-1-a-single-led/

It starts with wiring up an LED to the ground and 3.3V pin on the board, then on to actually using WiringPi to turn a light on and off. A ribbon cable, bit of wire stripping, careful counting of pins, and I had the thing connected exactly the same as in Gordon's example. Using the shell, I could enter commands to turn the LED on and off.

I then made a "press-button.sh" shell script - which looks like this:

gpio mode 0 out
gpio write 0 1
sleep 1s
gpio write 0 0

All it's telling the Pi to do, is : set pin 0 as an output pin (gpio mode 0 out), send voltage through it (gpio write 0 1), wait a bit (sleep 1s) and then stop sending the voltage (gpio write 0 0).

Right. So we have a Rpi that can switch a light on and off. Nice, but hardly reaching out and touching the real world.

Next step: hack a remote. Ideally, I'd have had some sort of switching transistor or relay handy for this, but all I had was a photo resistor. One like this (courtesy bkbelectronics.com)

I then ripped open the gate remote, removed the battery and soldered the photo resistor over the little push button switch. If the remote had worked off 5v or 3.3v, I'd have possibly just hooked the Rpi wires that power the LED to the switch, but with 12v flying around, I wasn't going anywhere near it.

With the photo resistor in place, it was fun watching the remote light go on and off as I covered it with my hand and exposed it to sunlight. Because I was using a resistor, which looks like it went up to about 1K ohm or something when it was dark, I just wired a 12 DC transformer to the terminals that used to hook up to the battery. Without a real switch in there, it's sure to be sucking power. Long term: I expect something to burn out here, but nothing's getting hot or letting off a burned plastic smell just yet.

With an LED on one side, and Photo Resistor on the other, we have a photo-optically isolated interface, so weird spikes in voltage on the remote won't affect the Pi, and vice versa. Here's what the outside looks like:

Black thing is the gate remote, duct tape wrapped thing is the LED pointing at the Photo Resistor.
Sorry I don't have a shot of the insides, but the thing works right now, so I'm not going to mess with it.

So now, we have a way to easily trigger the gate remote from the Rpi, using a shell script. To trigger the gate remote, I'd just run the "press-button.sh" script, and that'd do the trick. But expecting folks to ssh into the Pi and TYPE stuff is a bit much. What I needed was a web page people could bookmark, so they could fire it up by visiting it.

In steps Elnode (http://emacswiki.org/emacs/Elnode). Sure, I could have put PHP in there, running on Apache, and be finished quickly, but this whole thing had only taken me about an hour so far, so I was feeling confident. Elnode is a web server, which runs inside Emacs - a highly extensible text editor. Installing and getting it up and running is easy - if you're an Emacs user. I'm not going to go into the details here, but it's a thoroughly worth it exercise to install Emacs and Elnode! Stretch yourself.

Just to see what happened, I thought I'd give it a bash. So I put a bit of elisp code together:

(require 'elnode)
(defun base-handler (httpcon)
   "Gate opener"
   (elnode-http-start httpcon 200 '("Content-type" . "text/html"))
   (shell-command-to-string "bash /home/pi/wiringPi/click-button.sh")
   (elnode-http-return httpcon "<h1>GATE-OTRON9000 Activated!!!</h1>"))
(elnode-start 'base-handler :port 9000 :host "")

... Evaluating that (M-x eval-buffer) then gave me a lovely page when I visited the address: It just said GATE-OTRON9000 Activated!!! in big bold letters, but more importantly..

The thing worked! First time! straight off the bat. We expect the motor on the gate to be burned out soonish, but no one seems to mind, we're all having too much fun opening and closing the gate with anything with a browser and wi-fi connection.

Here's our model, Sipho, showing the sheer awesomeness of it all...

I'd upload a video. But seriously. It's a gate, opening and closing...

Just to recap - here's what's going on:

The user surfs to the web page, which is served by Elnode, running inside Emacs. The handler for the page fires off a shell script that uses the WiringPi library to turn an LED on and off. The LED is pointed at a photo resistor which is sitting across the switch of an actual gate remote. When the light from the LED hits the photo resistor, the button on the gate remote is "pressed" and the gate moves.

Duct tape, twisted wires, bash scripts and elisp at it's best!

Give it a go. It's very rewarding :)

No comments: