Introduction: Garage Door Opener Using a Raspberry Pi
Control garage motor from a smartphone or any device able to browse a webpage (with AJAX!). The project was started as I only had one remote for my garage. How fun was it to buy a second one? Not enough. My target was to be able to control and monitor my garage door from my smartphone with a single page. I haven't put any fancy security around it as the RPi is not exposed outside of my LAN. Thereby I rely on VPN to access the web page. Using an Android smartphone it's very easy to both setup a VPN and add a webpage to my homescreen. All i needed was the software for it.
Step 1: Some Background
What I found online
As a programmer, I like to reuse tutorials or already "production level" code. In this case I wasn't happy with what I found :
- Raspberry Pi Garage Door Opener by quartarian. Where I started, a simple button to command the relay. No open/close buttons, no feedback. It was anyway very helpful to understand the concept around dedicated RPi to control a motor from a relay. The cabling I use now is still the same for the command part.
- Raspberry Pi Garage Door Opener with GaragePi by Chase Chou. Same kind of setup but using an extra wireless remote. I wasn't willing to kill my only remote. It proved to anyway use the same kind of electrical setup for the command part. Along the way I learnt about WebIOPi to check my GPIO over a webpage. That's were I also learnt about putting a shortcut to my Android homescreen. It also started to light the idea of relying on services, after all, I want it to be reliable.
- Making Your Garage Door Email, Tweet, or SMS: Part 1 By Richard L. Lynch. This one added the idea to use a Magnetic Sensor to control the door state. However I haven't used much as I wasn't building an alarm system but a command/monitor system.
- Completely Wireless Garage Door Opener / Security Cam by DeckerEgo. This made me better aware about webcam streaming to monitor the door. I still have to put some effort into that. Maybe I'd have to recycle a USB hub or fetch another webcam.
- Raspberry Pi control from mobile device or desktop web browser by Frédérick Blais. This is now my base for the software stack. It uses Flask to run a very simple web page along with AJAX. This is premium to me as it provides real-time monitoring and command options. Read below.
About my RPi
As I'm still learning about the RPi and electronical stuff in general, I bought a number of stuff for my freshly acquired Raspberry Pi: a few cables, a T-cobbler, some leds, a one-relay module, a eight-relays module, a LCD display and so on. I'm in no way an electronician and this proved very instructive. To buy the extra hardware Amazon and Ebay are your best friends.
The current state of the project includes following hardware :
- A Raspberry Pi model B running Raspbian Wheezy 2014-09-09 with latest updates
- An 8GB SD Card to hold the OS, files and so on
- Two zinc alloy garage reed switches to detect open/close state
- One 3v relay module to send commands to the door motor (mine has a built-in LED to monitor the relay state, very handy!)
- A breadboard and 2 momentary switches to simulate the door on my desk
- A wifi dongle to remove extra cabling when it'll be hanging from the garage ceiling
A limitation I still have is that I don't know when the door is halfway, stopped or still moving. This is also why I'd like to later add an old webcam to stream from inside the garage. Probably I'll actually add some sensor to monitor the motor itself.
Step 2: Software Stack
The software relies on Python, Flask and WiringPi GPIO Python module. As explained in background, I started from the tutorial Raspberry Pi control from mobile device or desktop web browser published by Frédérick Blais. The current software is composed of :
- An HTML template file containing some jQuery Mobile code and controls
- A Python Pins module to access the GPIO of the Raspberry Pi
- A Python go module to run Flask and support the AJAX queries
What is present in the repository is the raw software. It's in no way fancy or overwhelm of features. It's just what I was able to achieve from what I've learned.
Currently the state for doors is read every 0.5s. Two switches are used for both open/close state. A relay is used to command the motor. Current code still outputs some debug. Below screen shot is from a Nexus 5.
Step 3: Hardware - Alpha
My test bed is pretty simple, a RPi, a relay and two switches. To hold things tight, a few zip ties is all you need. Here is a picture of it. LAN cable is for desktop debug and WIFI dongle for garage use.
RPi is connected to motor only for now, switches will follow. I must wait a bit to get my heat-shrinking tubing. I should also be more steady. Sorry for blur.
The blue, yellow and green cables on the left are for the magnetic switches.
- Blue is the GND
- Yellow and green are pins 18 (open state) and 27 (closed state).
Step 4: Hardware - Beta
This is now a direct cardboard with zip ties. The LCD and relay are also bundled. The LCD is 20x4. First 2 lines display welcome and date/time. Last 2 lines cycle between pins states and network state (eth0/wan0 IPs). The door state line is a nice GFX to say :
- | | is open
- |-| is operating
- |.| is closed
Fritzing schematic with 20x4 I2C LCD, open/close switches and relay. The only addition is the I2C LCD screen. I prefered that to the 18x2 I had.
The beta HW is now "production level" as it fits nicely in an old screws box. I kept one separator to host relay and misc cables while RPi stands at lower level. You might notice the Synology on the right side, it is used to power the RPi directly. By night with modem, switch and NAS.
Close view of screws box, the LCD shows door' state and Open/Close/Relay pinout.
By night, inner view from top.
Step 5: Source Code and Ressources
You can find all ressources at this GitHub repository:
We have a be nice policy.
Please be positive and constructive.
Hi Rudy, it's a nice project. I'm managing to make something similar with NodeMCU as a cheaper solution for such a simple task. However I'll give you a tip about how to check the door position and movement. You can use ultrasonic proximity sensor HC-SR04 to check how far is the door from the sensor and if it changes its position.
Hi Stan, thanks for your interest!
NodeMCU / ultrasonic was indeed a solution. However I had a RPi around and wanted to use it in my garage (next to networking equipment). This is why I used it. Final target was to use it for home automation .
In a perfect world I'd be happy to use more simple D1 Mini boards (similar to NodeMCU, I actually own a few NodeMCU but bought a few different models from Ali Express). Ultra sonic sensor was an option but lakes a precise reading for "fully closed" / "fully opened" vs "slightly opened", for instance to let some fresh air flows when doing wood working.
To link all these I'm having a look at Homie for ESP8266 framework (https://github.com/marvinroger/homie-esp8266), this is very promising but I'm still lacking time to investigate, especially regarding IR controlled HVAC units.
Please see my other 'able : https://www.instructables.com/id/Building-Homie-De...