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!


14 comments:

  1. Hi Don,
    Great project! If you are interested in making a version using our service (https://www.wia.io) let me know, I'd be happy to help.

    Cheers,
    Conall @ Wia

    ReplyDelete
  2. I'd like to hear more about the Android Table / ODBII project.

    ReplyDelete
    Replies
    1. Sure. I traded some work for an LG V410 (AT$T model) and had some issues downgrading the OS to be able to root and run a custom ROM (http://forum.xda-developers.com/g-pad-10/general/tutorial-lg-gpad-v410-5-1-to-4-4-t3232150) but it's all good now.

      It's not a particularly good looking mount, but I basically took an old GPS mount from a friend, took it apart to get the flexible metal arm, cut a hole in the dash and used plastic welding epoxy to attach it to the back and used a different epoxy to attach the other end to a universal tablet mount. I got custom sized 90 degree audio and MicroUSB cables and hid them along the metal arm with heat shrink.

      I grabbed a Bluetooth OBD2 ELM327 reader from Amazon for about $10 and Torque Pro for $5. From there it was Nova Launcher with one home screen and all kinds of convenience widgets and settings (Play Music, Torque, aCar Pro, Weather widgets, Maps, double tap to sleep and wake, status bar brightness slider, Google Now hotwords anywhere, and other things to make it useful while driving. We have PBS Kids on there so the kids can stream Daniel Tiger, Cat in the Hat, and Dinosaur Train when we don't need to use it.

      Also, we have an NFC sticker under the dash so we can turn our WiFi hotspot on our phones on and of quickly for the tablet's Internet connectivity.

      Pretty straightforward. Grab me on Google+ if you would like more information.

      Thanks for checking out my project!

      Delete
  3. What relay part did you use? Which switches, just reed switches??

    ReplyDelete
    Replies
    1. It was just a SPDT 5v relay from Radio Shack. You can us any 5v relay for this.

      And yes, the magnetic switches were Reed switches. These: http://www.amazon.com/gp/product/B00PZMG980

      Thanks for reading!

      Delete
  4. I built this exact project a few years ago. It worked great until at dinner one night the garage door opened by itself. I investigated and found that the Pi had decided to bring that GPIO line high. There was nothing in the software logs. What was especially disturbing was that the relay was kept shut by the misbehaving GPIO pin, so pressing the normal garage door button did nothing. I couldn't close my own door! I had to restart the software, which released the GPIO pin. That scared me enough that I disconnected the relay. Otherwise I could be on vacation and my neighbors wouldn't even be able to close the door for me. I don't know what I should do to get enough confidence to hook it back up. Maybe upgrade to the latest Pi and Raspbian and hope the problem is fixed. Or separately monitor the relay and reboot if it's stuck.

    ReplyDelete
    Replies
    1. Damn. That would suck, especially if you were out of town. Thanks for sharing your experience - it's definitely a valid concern with this sort of thing.

      Delete
    2. Just a thought... add a check to the loop that looks at the switches to verify that the signal is in the state you put it in. This should be a separate pin, not just checking a variable or reading the same pin. If not, drive it back or self reset to recover. Add a weak pulldown to the base of the transister to ensure that if the Pi fails or the power glitches, the relay will stay off.

      Delete
    3. GPSGeek: Yes exactly, I have the circuit for it, I just need to hook it up. I can't test it properly, though, since I can't reproduce the original problem. Pretty scary!

      Delete
    4. @GPSGeek
      Good idea. I'll probably end up implementing that.
      (also, I deleted the duplicate post)

      Thanks for checking the project out and providing feedback.

      Delete
  5. Don, I'm attempting to replicate this but I'm having some trouble. I have the webserver up and running and the circuit built and functioning but I just can't get the website to work. I can test the relay and the switch status' manually from terminal or by executing the shell scripts but the website won't reflect the door status or activate the relay.

    I found one typo in your ReadMe file for the status.sh script (missing "/" on the 5th line of the script) and corrected that and I can run status.sh from Terminal and reports the correct status.

    I can also run both door.sh and toggle.php from Terminal and both correctly activate the relay for 1/2 second.

    When I load the webpage though it shows the door as closed no matter the switch configuration and when I tap on the image the page will reload but it never activates the relay.

    Hoping you might have some pointers.

    Thanks!

    ReplyDelete
    Replies
    1. Well, I think I figured it out. For right or wrong I changed the user:group that apache uses from www-data to the local user and now its working.

      Thanks for posting this and all of the code. Now to actually get it all installed and verify it's working. I have a webcam in the garage and will show the stream right below the svg image for visual verification at least for a little while.

      Delete
    2. Hey, sorry I'm late to responding. Glad that you have it working. I'd advise against running apache under a different user because Ubuntu does some nice things for the www-data user to protect your system from being hacked. That being said, you probably needed to give your www-data user access to the scripts. I have the scripts chmod 755 so any user on the system can run them regardless of the fact that root owns them. My guess is that is what was going on with you. Keep in mind that you can do 'tail -f /var/log/apache2/error.log' while you are clicking on the SVG to try to see what is erroring. Anyhow, for what it's worth, I'd recommend trying to resolve this without changing the apache user before exposing it to the automated scripts that look for exploits on the web.

      If you have any other questions, I'm happy to help! Thank you for checking out my project and I really hope that it helps make your life better.

      Delete
  6. I read many articls but your article is unique and informative. keep it up Float level sensor

    ReplyDelete