MQTT Garage Door Control

makermattfeatured, Projects, UncategorizedLeave a Comment

If you are like me then you have left the garage door open overnight more times than you care to remember. Or how about when you come home for the day only to find your garage door has been open the whole time. Always nice to realize not only was all the cool stuff in your garage just sitting out for anyone to take, but your home was open as well. With the help of an arduino, a relay, MQTT, and Home Assitant that is now all stuff of the past. I give you the remote garage door control. (pause for thunderous applause and cheering…)

Step 1 – What to use?

I needed something that was wireless, could transmit to an MQTT server, and could trigger a relay all the while being very fast and reliable. Enter my tried and true go to arduino the WeMos Mini D1.

This is pretty much my goto ESP8266 device right now. It has enough digital pins for most of my projects and it’s super tiny plus it’s wirelss so it’s kinda the bees knees of arduinos. I’ve found plenty of them on aliexpress for $3-4 apiece and I haven’t had a bad one yet.

Step 2 – Components

Next step is the components. So I need to accomplish 2 things.

1. Be able to open the garage door
2. Be able to sense whether the garage door is open or closed.

The first one is easy, that’s just a simple relay. The second though is where the fun comes in. I decided that using something to detect distance would be my best bet, so I used an ultrasonic sensor.

Here are the sensors I used.

Ultrasonic Sensor

5v Relay

So It’s not super complicated right?!? Just use the ultrasonic sensor to detect if the garage door is present or not. The idea is to hang it from the ceiling and detect if the door is up which would show as a close distance vs closed which would detect the floor. The other good thing about the location is it’s close to power and the garage door opener itself. The relay of course closes our circuit that essentially does the same things as pressing the garage door opener/closer on the wall.

So here are a few pictures of the finished product.

Step 3 – Program

First and foremost here is the link to the code on github. GitHub Garage Door Code

So I know what I needed to measure but now I have to write out an Arduino sketch to do it all. The only part I’m not sure of is how the ultrasonic sensor will perform. Turns out in the end though it did pretty good! Here are a few snippets of the code that I used.

The biggest issue that I found when doing the code was determining the distance but getting an average over x amount of time. The distance calculation would change a lot. So I used the following library which allowed me to get a running median, aptly named, running media. Running Median

#include <RunningMedian.h>
RunningMedian samples = RunningMedian(15);
if (millis() - currentMilis >= 2000 ) {
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);

    // Calculating the distance
    distance = duration*0.0133/2;
if (count >= 15) {
Serial.println("Checking Distance");
medianDistance = samples.getMedian();

So what I’m doing here is essentially taking a measurement every 2 seconds and adding it to the samples. Then if the count is 15 I then get the median number. By taking a median average over 30 seconds I can be confident that my reading is correct.

Now that I’m getting the distance the next thing is to send it to MQTT and then add it to my Home Assistant interface.

Step 4 – Interface

So now I have the device installed and it’s giving me the distance calculations now we need to interface with it. Since we are using MQTT for everything all we need to do is add it as an MQTT device in Home Assistant and now we can see it’s status and then open/close it.

Now with a few additions we can receive text messages when it’s still open after sunset or if everyone has left the house. We can also add this to a “nighttime” script that enables the security system and also ensures the garage door is close.

Pretty Cool!

Leave a Reply

Your email address will not be published. Required fields are marked *