Smart home gadgets are cool but can be expensive. With a Raspberry Pi and one or two components, it’s easy and cheap to connect existing devices to the Internet.

Creating your garage door’s internet door is a great introduction to learning how to control the real world with the Pi. And let’s be honest, who doesn’t want to feel a little like Batman and have their garage open when they pull up to their driveway?

What you need

To automate your garage doors, you will need:

  • Garage door motor that can use an external trigger.
  • Raspberry Pi (any model) is connected to the internet. This guide assumes that your Pi is running the Raspbian operating system (if it isn’t, see our article on how to install Raspbian on a Raspberry Pi).
  • Relay expansion board, which can be found on Amazon.
  • Power supply 2A for Raspberry Pi. The lower rated model may have trouble driving the relay board as well as the Pi.
  • Four connecting cables (plus one more for each additional door you want to control).
  • A two-wire cable is enough to get between the pi and the garage door motor. A cheap speaker is ideal, but a phone or ethernet cable might work too.

Once you’ve got these components together, it’s time to get started.

How this automated garage door works

Most garage door motors can be triggered to open or close through an external input. Manufacturers include these inputs so installers can plug in a simple button somewhere in the home to open or close a door without using a conventional remote control. When the start button is pressed and released, it momentarily closes a circuit that tells the engine to start or stop.

You are going to use a relay instead of an external button. Shorting the relay will close the circuit, just as if the start button had been pressed. A Python script running on a Raspberry Pi will allow you to control the relay and therefore the door from your home network.

Step 1: Connect Raspberry Pi to Relay

You will be making at least four connections between the Raspberry Pi and the relay board. If you’re using a Pi Zero, you’ll either need to solder the connections directly, or solder the GPIO header to the Pi and use jumper wires for the connections. The last option is recommended because if you ever want to disconnect the relay board and use your Raspberry Pi for something else, you won’t need to solder your connections. The larger Pis already has pins for connecting push-on jumpers.

If you are new to using GPIO, be sure to read our article Everything You Need to Know About Raspberry Pi GPIO Pins.

Before connecting, check if your relay board has a jumper connecting the VCC and JD-VCC pins. If so, remove it because you need to connect VCC and JD-VCC separately.

JD-VCC relay board to VCC jumper

With everything off, connect the relay board to your Pi like this:

  • Start by connecting Pi Pin 2 (5V rail) to the JD-VCC on the relay board.
  • Connect Pi pin 1 or pin 17 (3.3V rail) to VCC on the relay board.
  • Connect the pin of Pi 6 (GND) to GND on the relay board.
  • Finally, connect Pi Pin 7 (GPIO 4) to IN1 on the relay board. This is the connection that switches the relay.

If you have more than one garage door, or if you want to add control for electric gates, you must add additional connections between IN2, IN3, etc. on the relay board and other free GPIO input/output pins on the Pi.

Raspberry Pi connected to relay board

Step 2Install Dependencies on Raspberry Pi

Raspbian comes pre-installed with Python, but you will need to add the GPIO library. Enter the following in the terminal window on your Pi:

sudo apt-get update sudo apt-get -y install python-rpi.gpio 

Now create a new folder in your home directory somewhere to put the python script that will control the relay:

 mkdir ~/garagedoor cd ~/garagedoor 

Finally, download bottle, a lightweight framework that will create a simple web server on your Pi:

 wget https://bottlepy.org/bottle.py 

Step 3: Create a control script

Here is a very simple python script to control the relay board via HTTP:

 # Python Script To Control Garage Door # Load libraries import RPi.GPIO as GPIO import time from bottle import route, run, template # Set up the GPIO pins GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) GPIO.setup(11, GPIO.OUT) GPIO.output(7, True)   GPIO.output(11, True) # Handle http requests to the root address @route('/') def index(): return 'Go away.' # Handle http requests to /garagedoor @route('/garagedoor/:doornum') def garagedoor(doornum=0): if doornum == '0': return 'No door number specified' elif doornum == '1': GPIO.output(7, False) time.sleep(.8) GPIO.output(7, True) return 'Door number 1 cycled.' elif doornum == '2': GPIO.output(11, False) time.sleep(.8) GPIO.output(11, True) return 'Door number 2 cycled' run(host="0.0.0.0", port=1234) 

On your Raspberry Pi, create a new Python file using nano:

 nano door.py 

Copy and paste the script above into a blank document. Exit and save by clicking CTRL+X then Y and Enter for confirmation.

Now test the script by running it:

 python door.py 

If everything works, you will see the following message:

Python script fires a message

If you get any error messages, check that everything is pasted into the file correctly and that you don’t have another web server like Apache running on the same port (this will interfere with the server that creates the script).

Assuming there are no errors, go to a web browser on another computer on the same network and in the address bar enter your Pi’s IP address followed by a colon and 1234. For example, if your Raspberry Pi’s IP address was 11.22.33.44, you must enter 11.22.33.44:1234 to your browser.

If everything works, you will see a message telling you to leave!

Now add /garagedoor/1 after the IP address and port number, for example: 11.22.33.44:1234/garagedoor/1

Click Enter and you will hear the first relay on the board double-click as it closes and opens again. If you change 1 to 2 and reload the page, you will hear the second relay cycle.

Step 4: Connect the relay to the door motor

Refer to your garage door motor manual for where you can connect an external controller. Connect a two-wire cable to it and connect the other end of the cable to the screw terminals of the relay board.

Connecting the relay board to the door motor

There are three terminals on the relay — use a normally open pair as shown.

Connecting the garage door motor

Test everything from the web browser again (you can just refresh the page). If all goes well, the relay will click a couple of times and your garage door should start to open.

Step 5: Install the autoload script

If your Raspberry Pi reboots for any reason, such as a power failure, your Python script will stop working. To load it on startup add the following line to your file /etc/sc.local (if you are logged into your Pi as a different user, change Pi to your username):

 nohup python /home/pi/garagedoor/door.py & 

Do this with a regular text editor, saving the file when you’re done.

Security Issues to Keep in Mind

There is no security in this sample Python script — anyone who can access your Raspberry Pi by its IP address will be able to open and close the garage door. It’s tempting to think that if no one knows your script is there, no one will find it, but security has been a bad idea more than once because of obscurity.

A complete authentication system is beyond the scope of this guide, but a simple security solution is to not make your Pi accessible outside of your home network.

Here are some more tips for keeping your Raspberry Pi safe.

Garage door control with IFTTT or Siri

If you choose to make your Pi accessible from the open internet, you can control your garage door with services like If This Then That (IFTTT). For example, you can combine Alexa and IFTTT Webhooks to open your garage doors when you say your Amazon Echo trigger phrase.

IFTTT Alexa and webhook applet

If you are driving a vehicle with the IFTTT service connected, you can create an applet. to open the door when your car approaches your property, Batman style.

Want to know more? View our downloadable guide to using IFTTT like a pro

Another way to launch your newly online garage door is to use the Siri keyboard shortcut. Adding a really simple shortcut to the widgets means you can forget about carrying annoying keys and instead open your garage by swiping and tapping on your iPhone.

Siri Shortcut to Call Garage Door Script

Beyond the Garage: More DIY Projects Smart Home

Relay boards can switch mains voltage and therefore can control most home appliances. This project could serve as the basis for adding smarts to just about anything with a power supply. Coffee machines, lighting, air conditioners are all ready to be automated with a Raspberry Pi and some relays.

Interested in ways to automate your ceiling fan too? And be sure to check out these other smart home automation projects. home automation for smart home for more ideas.

Похожие записи