Garage Door Control with the Raspberry Pi

I wanted a simple project to start my home automation adventure, so I went with something that I perceived to be a fairly simple; garage door control. First off, disclaimers: I am by no means an electronics expert. I’ve never had any formal training in electronics, just one of those “200 in 1 Electronic Kits” as a child. So all of this was fairly new to me. I know basic electronics from reading a few books as an adult so I would think that this project should be fairly accessible to newbies.  All that said, let’s get started with the electronics side of things.

Relay Schematic

Relay Circuit for the Raspberry Pi

The Hardware

The garage door opener is a button. It is my professional opinion that the garage door is controlled by a rather uncomplicated button. The way I understood it, this functionality could easily be replicated with a relay. Confident with that theory (somewhat), I went to the Google Machine.  A quick internet search on the subject of controlling relays with the Raspberry Pi turned up this page: www.susa.net. This is a most excellent page on the topic. I recommend the read. If you’d like to cut to the chase, here is the circuit that I used.

 

Raspberry Pie Pinout.

Raspberry Pi Pinout. Captured from http://pi.gadgetoid.com/pinout where there is an awesome interactive model.

 

The pins that I connected to on the Raspberry Pi were GPIO 25 (pin 22) and Ground (pin 25). For the 5v power supply, I connected to an external power source although I think you could use pin 2 or 4 for the 5v and pin 6 for the ground. I stand ready to be corrected if this is not the case.

The Software

WARNING:

If you use this method to open your garage door, be sure that you have a secure wireless network and only allow access from your internal network. Readers are going to scream about the insecurity of this and they are right. It is recommended that you take further steps to secure this than outlined here. To properly secure your Pi for this kind of project check out this post about securing your Raspberry Pi.

I wanted to be able to access this via a web GET request in order to make it very accessible. If you need help, be sure to check out this full tutorial for getting your Raspberry Pi environment prepared.

Prerequisites for this project.

  1. Raspberry Pi with the latest version of Raspbian installed.
  2. uWSGI, Flask, and Nginx installed. See Tutorial
  3. WiringPi Python Module http://wiringpi.com/

Once you have the above installed correctly create a new file called “PiAutomate.py” and paste the following code. It is way beyond the scope of this post to explain how to program python but if have ever done any programming, you should be able to see what is happening here. Feel free to modify to suit your needs.

from flask import Flask
from flask import render_template
from flask import request
import wiringpi
import time
import time, os

@app.route('/openDoor')
def this_is():
 os.system('/usr/local/bin/gpio export 25 out')
 io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
 io.pinMode(25,io.OUTPUT) # Setup pin 22 (GPIO25)
 try:
   io.digitalWrite(25,io.HIGH) # engage the relay
 except:
   print "An error has occurred while engaging the relay"
 time.sleep(2)
 try:
   io.digitalWrite(25,io.LOW) # disengage the relay
   print "An error has occurred while disengaging the relay"
  return "Success"

Once the file is written,you can test it out. First run the script

  #python PiAutomate.py

Then visit your pi from a web browser.

http://your.ip.address/openDoor

This should actuate the relay and you should hear a click, a two second pause, another click, and “Success” should be printed to the browser window.
If that worked, you can then set your flask app to start at startup by taking the following steps:

Copy the default uWSGI configuration script to “PiAutomation.ini”

#cp /etc/uwsgi/apps-available/app-uwsgi.ini /etc/uwsgi/apps-available/PiAutomation.ini

Edit the new file to open your PiAutomation.py script. It should look something like this:

[uwsgi]
socket = /tmp/uwsgi.sock
plugin    = python
wsgi-file = /path/to/PiAutomation.py
callable = app
process   = 3

Reboot your Pi and the script should now start automatically. Feel free to repeat the web browser test.

If all is well, you have a couple of steps forward. First re-read the security warning. The simplest option is to add a bookmark to your Python script. I like creating a Tasker task on my cell phone creating an app with app factory. (I smell a future posting…)

Good luck with your project, if you have any questions/comments please feel free to leave them in the comments below.

About

I’m just your everyday average home automation hobbyist. I’ve been into technology since I was a little guy and I am currently an IT director at a small community college in rural Oklahoma. While working and playing I’ve had a great deal of fun getting to play with a very wide variety of technology, and I love to play with technology!

Posted in Home Automation

Social