Google Analytics

Wednesday, October 21, 2015

Opening a garage door from the Internet

Who wouldn't want to be able to control their garage door over the Internet?  No one, that's who.

I've recently mounted a 7" Android tablet in my wife's car to control navigation, music, realtime ODB II data, and various other tasks.  My wife then asked if she could control the garage door with the tablet.  "Well..... you can, but I have to build something to do that."  "Ok, then build it."

The requirements of the build are as follows:
  • It must give the status of the door (open/closed/unknown)
  • It must be able to toggle the status of the door
  • The WAF must be very high
  • It must work with at least Android, but all platforms would be ideal 
  • It must be secure since it's Internet facing
  • It must be reliable and therefore simple

The obvious place for me to start is with a Raspberry Pi - it's cheap, has network connectivity, runs Linux, and I have a spare sitting around the house waiting for a project.

It also seemed logical to make this as a web app instead of a native Android app so 0) it would be instantaneously up to date, 1) it wouldn't cause any battery drain from having to check the status every 30 seconds to stay up to date, and 2) for me, a web app is much easier to develop.

My garage door (like most) has a simple mechanism to trigger opening and closing the door.  Simply short two pins via a push button or, in this case, a relay.  The problem is that a Pi can't activate a relay directly with GPIO.  Well, not safely anyhow.  So we need to a very small amount of power to trigger an NPN transistor in order to trip the relay.  Since I'm using a 5V relay, I can safely pull that voltage from the 5V pin on the Pi.  The diode is there to prevent damage to the Pi when the field from the relay's coil collapses.

The electronics are pretty straightforward. A transistor activates a relay to open and close the door and 2 magnetic switches report the door's status back to the Pi.


The 10K resistors are there as pull up resistors.  Basically it makes GPIO pins 23 and 24 read as "high" unless the switch is activated.  Once the switch is activated the voltage goes to ground and the pin goes "low".

If you look at other projects on this page, you'll notice that I'm a big fan of using Cat5.  It's great: it's cheap, reliable, and able to carry signals over long distances without degrading.  In this case I ran 1 line of Cat5 to the garage and split 3 pairs off of it. The blue pair go to the garage door, the green pair go to the "opened" switch, and the orange pair go to the "closed" switch.

I decided to use 2 different switches instead of one because "not closed" or "not open" is not good enough for this project.  I wanted 2 switches to be in agreement as to the state of the door.  These are cheap door switches from Amazon - about $7 for a pack of 5 but they work great.


The activation circuit.  The blue and blue/white wires go to the garage door.


The switch to confirm that the door is opened

The switch to confirm that the door is closed

I simply soldered the resistors inline instead of bothering with another PCB.
Heatshrink tubing is awesome.
The finished hardware on a shelf in the basement (where no one can see it).
Now on to the software!

I used Inkscape to generate the SVG that I used to display the status of the door.  This project was largely an exercise for me to learn a little about CSS and SVGs.  This is the first thing like this that I've done from scratch and I know that there's room for improvement.  Please share ideas in the comments section if you have any constructive insight!

The idea is that the SVG will have either opaque or transparent doors depending on the status of the magnetic switches when the page loads.  When clicked, the SVG will have an animation of the doors either going up or down.  Once the animation is complete (which gives the door plenty of time to open or close) the page will refresh showing the status of the door as a confirmation. 



The code can be found on my GitHub account here: https://github.com/dhowdy/GarageDoor.

Security is accomplished via Apache with ModSSL and basic authentication.  The guys at DuckDNS and PortForward have everything that you need to access your Raspberry Pi from the Internet.  


Lastly, I have to say thank you to to Kevin Sangeelee for his great write-up on using a Pi's GPIO to activate a relay, Justin McCandless for patching jQuery's GLARING lack of SVG class support, and to Nate Krofft for giving me a huge hand with some of the CSS.  And, since I know that I'm going to get a lot of this... I know that I'm not the first person to do this; I simply wanted to share *my* way of doing this with everyone in case it can be helpful to someone else.

Thanks for reading!


Sunday, February 22, 2015

Building a clock to help kids tell time

My daughter is about 2 1/2.  She loves learning about colors, shapes, animals, dinosaurs, and all sorts of other things.  However, she's still too young to understand how to read a clock.  But now is a good time to start teaching her.

We wanted a clock that would change colors based on what it was time to do: play quietly, go to bed, get up and play, pick up toys, and so on.  Here is the result:



Alarm clock next to the baby monitors and entertainment center.



To start, I purchased a wife-approved, battery-driven clock from a local retailer for about $10.  This one was perfect for me: it has plenty of room inside, a nice, big face, some weight to it to hold it in place, and, to top it all off, it matched the wall.

OK, enough introduction, let's get down to it.  I've got a Pi running a baby monitor (another project, another write-up, but the LEDs in the lower left corner are a super bright infrared spotlight for night vision on the cameras), so I'll be driving it from that. And I've got some ShiftBrites leftover from version 1 of my DIY Ambilight (scroll down for more info on that project).  Perfect.  Let's build.

First, I needed a power supply.  Since we have an excess of USB ports, that's what we'll use.  I've already built a power supply based on the LM317T and it's been running for 7 or 8 years now.  Using this basic schematic, I built the power supply to take a 5V input:
(credit to http://kb.kaminskiengineering.com/ for the picture)




(I know, my workbench is cluttered.  I do a lot of work on it.)
This power supply is going to supply ~3V to the clock internals and 5V to the ShiftBrite.

Now we need software to test with.  After I could not get this tutorial to work, I found Hive13's project on GitHub.  After playing with it for a bit, I compiled the C program and was getting good tests.  However, I could not figure out a dead simple way to to just set certain colors and the program looped until interrupted - all of this is great for what Hive13 and likely most people want to do with a ShiftBrite on a Pi, but I needed something different.  I forked his GitHub repo and made a few changes, namely the ability to push red, green, blue, purple, yellow, and cyan from the command line and immediately exit.  I also added an "install script" as well as a "crazyclock" script to cycle through the colors.  It's all kind of hackish, but since the code was written for only my specific example, I don't care - but sharing that code back to the community is still the right thing to do.  

Essentially, I just added 6 command line arguments so instead of only having "-c [0-255]" emitting a white color with the given intensity, I've added the following code:

printf("  -R: Set to constant color Red\n");
printf("  -G: Set to constant color Green\n");
printf("  -B: Set to constant color Blue\n");
printf("  -P: Set to constant color Purple\n");
printf("  -Y: Set to constant color Yellow\n");
printf("  -C: Set to constant color Cyan\n");
.
.
.
} else if (opt->mode == RED && opt->constant_value_red >= 0) {
img[0] = opt->constant_value_red;
shiftbrite_refresh();
return 0;
} else if (opt->mode == GREEN && opt->constant_value_green >= 0) {
img[1] = opt->constant_value_green;
shiftbrite_refresh();
        return 0;
} else if (opt->mode == BLUE && opt->constant_value_blue >= 0) {
        img[2] = opt->constant_value_blue;
        shiftbrite_refresh();
        return 0;
} else if (opt->mode == YELLOW && opt->constant_value_yellow >= 0) {
        img[1] = opt->constant_value_yellow;
img[0] = opt->constant_value_yellow;
shiftbrite_refresh();
        return 0;
} else if (opt->mode == PURPLE && opt->constant_value_purple >= 0) {
        img[2] = opt->constant_value_purple;
        img[0] = opt->constant_value_purple;
        shiftbrite_refresh();
        return 0;
 } else if (opt->mode == CYAN && opt->constant_value_cyan >= 0) {
        img[2] = opt->constant_value_cyan;
        img[1] = opt->constant_value_cyan;
        shiftbrite_refresh();
        return 0;

All of this is on GitHub, but essentially calling the binary with "-R 255" will turn the LED red at full brightness and immediately exit.  Simple and straightforward.

It's also worth mentioning that since this box is secure and I didn't want to mess with the proper permissions to grant other users the ability to use SPI, I just changed the setuid attribute of the binary to always run as root.  Again, this would should never fly in a production environment but in this case it's nothing to worry about.  

SPI pins on the Pi.  
Next is the case mods.  There was not enough room to put the ShiftBrite, so my Dremel helped me make it fit. 

 
Essentially from here, I just hooked the Vout pin of the LM317T to the clock's battery terminal lines and attached the ShiftBrite directly to the 5V line coming off of the USB cable.  Then I crammed everything as neatly back in to the case as I could.  I had to cut some pins and use some hot glue to insulate some things, but it worked.


There is a USB cable running in to the back for the power supply and a piece of Cat5e running out the back for data from the Pi.
The clock installed next to the baby monitor cameras.


The last thing is setting the clock color automatically.  I found a nifty little project called Minicron. Essentially, it's a client/server setup that modifies your cron jobs and sends the output back to the server.  It's simple, straightforward to set up, and perfect for what I need (given my lack of web development skills).  I installed it on my web server and on this Pi.  Then I just configure what colors I want the clock to be and when.


Additionally, I've configured ConnectBot on my wife's phone to have different connections that will just log in using a public key and execute "/usr/local/bin/clock -G 255" for play time or "/usr/local/bin/clock -R 75" to turn the clock red at reduced brightness for nap.  The cron job will take care of almost everything, but she needed the ability to change it on demand.  

So, that's pretty much it.  I'll be happy to answer any questions below.  



UPDATE: I realize that I forgot to mention that it is important to keep the clock and Pi synchronized.  Since I use Raspbian on the Pi, here's how I ensure it has correct time by using a Stratum 1 time server in my home town.

apt-get install ntpdate
service ntp stop
update-rc.d ntp disable
echo -e '#!/bin/sh \n/usr/sbin/ntpdate 0.ntp.mst.edu' > /etc/cron.daily/ntpdate
chmod +x /etc/cron.daily/ntpdate
service cron restart

This will get the Pi to update its time every day.  If you notice a skew over the course of a day (since the Pis lack a realtime clock) then you can just move this script to the /etc/cron.hourly folder.  If you do so, please us a different time server than this one.