Skip to end of metadata
Go to start of metadata

Originally written on Jun 10, 2017

This is a quick tutorial for turning an OpenWRT router into a garage door controller. In this tutorial, I will be using the MT300N from GL-Inet to control my garage door over WiFi.

First, you'll need to SSH into the router. We need to edit the file /etc/rc.local. In this file I will put the following lines before exit 0:

/bin/echo 44 > /sys/class/gpio/export
/bin/echo "out" > /sys/class/gpio/gpio44/direction

The first command enabled GPIO pin 44. The 2nd line sets the GPIO port as "output", what we will use to control the relay. Now reboot the router.

Next, lets wire up the relay to the router. For my MT300N router, I used GPIO port 44 (labeled as LINK4 on the router board). Conect GPIO pin 44 to the "IN" pin on the relay. Now, connect GND to GND on relay, and connect the 3.3v pin on the router to the VCC pin on the relay. Here is a wiring diagram:

Now to test if it works, run the following commands. To turn the relay ON, run:

/bin/echo "1" > /sys/class/gpio/gpio44/value

The green light on the relay module should turn on, and you should hear a click. Now to turn the relay off, run:

/bin/echo "0" > /sys/class/gpio/gpio44/value

and the green light will turn off. If this works, your relay is wired correctly.

Now we need to install PHP to run the commands from a web interface. Run the following commands in order to install PHP to your router.

opkg update
opkg install php5 php5-cgi

Now edit /etc/config/uhttpd to enable PHP. Right below the first block of text, add the following lines:

        option interpreter '.php=/usr/bin/php-cgi'
        option index_page 'index.php index.html'

And now restart uhttpd.

/etc/init.d/uhttpd restart

Now lets create a quick script to turn the relay on, wait 1 second, then turn the relay off again. This simulates someone pressing a button to make the garage door open. I wrote this quick and dirty bash script to do just that. I saved this file to /root/

/bin/echo "0" > /sys/class/gpio/gpio44/value
sleep 1;
/bin/echo "1" > /sys/class/gpio/gpio44/value

And lastly, lets create the quick PHP page to run this shell script. I put this file in /www/garage.php:

if ($_GET['run']) {
  # This code will run if ?run=true is set.

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<center><button type="button"><a href="?run=true">Garage Door Control</a></button>

Lastly, we need to wire the relay to the garage door opener itself. This process in concept is simple, you just need to find the two screw terminals on your garage door opener that connect to the garage door button, and wire them to the NO and COM ports on your relay.

Now, when I navigate to http://router-ip-here/garage.php, I can activate the relay, opening my garage door.

A picture of the final setup:


  • No labels