Garage Door Sensor

Using the ESP8266 to broadcast open/close MQTT events over WiFi

Similar projects worth following
I want to be able to get remote reassurance that the garage door is closed, as well as notifications when it opens.

HomeAssistant Integration

This is another one of my additions to HomeAssistant, the amazing Smart Home/Home Automation software.

In order to track MQTT messages from this project, I added the following to my HomeAssistant configuration:

  - platform: mqtt
    name: "Garage Door"
    state_topic: "hass/garagedoor"
    device_class: opening
    payload_on: "1"
    payload_off: "0"


An alternative board, implemented by @juan11perez

Adobe Portable Document Format - 1.22 MB - 03/08/2018 at 21:46



Compiles to both the ESP8266 (tested and working) and Arduino with WiFi shield (not tested).

ino - 3.19 kB - 11/22/2017 at 19:27


Standard Tesselated Geometry - 233.09 kB - 05/30/2017 at 00:23


Standard Tesselated Geometry - 246.76 kB - 05/30/2017 at 00:23



Change the Fan parameter to automatically modify the front of the model.

- 596.00 kB - 05/30/2017 at 21:00


View all 7 files

  • Sonoff Alternative

    Andy Castille03/08/2018 at 21:52 0 comments

    @juan11perez has ported this project to Sonoff!

    He has nicely documented the build in this PDF.

  • Debugging over the network

    Andy Castille11/22/2017 at 19:22 1 comment

    I just posted a new version of the code that also provides a server so that you can check the current status while it is running remotely:

    $ telnet
    Connected to
    Escape character is '^]'.
    mqtt_connected = 1
    distance_cm = 102
    door_open = 1
    Connection closed by foreign host.

  • It works!

    Andy Castille06/05/2017 at 14:53 1 comment

    I had to modify the code to check the state twice and make sure the values match before sending an event because sometimes there are fluctuations in the sensor that would report it as open for a very small amount of time.

    Because making that change involved taking the unit off the ceiling for USB programming, I also added Arduino OTA support so that future updates could be flashed over WiFi.

  • Housing

    Andy Castille05/30/2017 at 00:32 0 comments

    While waiting for my Amazon order to arrive, I designed a mount to contain the hardware and hold it to the ceiling:

    Read more »

  • Wiring

    Andy Castille05/29/2017 at 21:42 0 comments


  • Hardware Selection

    Andy Castille05/28/2017 at 14:24 0 comments

    All final hardware decisions are listed and Amazon-linked in the components section.

    I started with my goal: to remotely check if the garage door is closed for peace-of-mind.

    I decided that I didn't want to interfere in any way with the existing opener unit because I'd rather not break it trying to interface with it and I don't want to control it because that introduces a security risk. I decided to use a non-contact sensor to reduce wear on the door and/or sensor as well as any scratching noise or resistance to movement. This left one good option: an ultrasonic distance sensor. I can mount it near the bottom of the door opening or on the ceiling and check if it detects a door close in front of it.

    I have an Arduino UNO, however, WiFi shields are expensive and I'd rather keep it available for tinkering. While researching WiFi-enabled microcontrollers, I stumbled across this article about the ESP8266. I found a nicer one for just under $10 on Amazon and now it's on its way.

View all 6 project logs

  • 1
    Step 1

    Set up the Arduino IDE

    1. Install Arduino IDE 1.6.4 or newer.
    2. Enable the ESP8266 board:
      1. Add to File, Preferences, Additional Boards Manager URLs.
      2. Open Tools, Board, Boards Manager and wait for it to refresh.
      3. Search for and install esp8266 by ESP8266 Community.
      4. Choose your ESP2866 module from Tools, Board, ESP2866 Modules. Using my Amazon purchase, this is NodeMCU 1.0 (ESP-12E Module).
    3. Install libraries:
      1. Open Sketch, Include Library, Manage Libraries...
      2. Search for and install PubSubClient by Nick O'Leary.
      3. Search for and install Ultrasonic by Erick Simões.
  • 2
    Step 2

    Wire it up

    Connect the sensor to the microcontroller board according to my setup so you don't have to remap pins in code. I used prebuilt F2F wires to make it easier on myself.

  • 3
    Step 3

    Flash it

    1. Connect the microcontroller to your computer using a microUSB cable.
    2. Download and open GarageDoorSensor.ino.
    3. Replace the values at the top of the file with your own WiFi network info, MQTT server config, etc. For the distance threshold, choose something a little bit (about 5 cm) higher than the distance between your ceiling and the garage door when it is open.
    4. If you want an easy way to make sure it works correctly from your computer, set DEBUG to 1 and open the Serial Monitor at 115200 baud. If you're ready to install it in its permanent location, set DEBUG to 0 in order to save some CPU cycles.
    5. Be sure you have the correct options specified in the Tools menu:
      Board NodeMCU 1.0 (ESP-12E Module) if you're using the same board
      CPU Frequency 80 MHz
      Flash Size 4M (3M SPIFFS)
      Upload Speed 9600
    6. Press Upload and wait for it to flash. Don't worry if it's slow, it normally takes quite a bit longer than flashing a regular Arduino.

View all 4 instructions

Enjoy this project?



Guillaume R. wrote 12/20/2018 at 12:58 point

I want to do a door sensor check like you, but with magnet and magnetic sensor. And ultrasonic sensor to check if car is inside or not, and maybe if it will work, check the sens of open/close during movement of my 2 doors.

All if possible with an ESP32 based board.

  Are you sure? yes | no

Dave Sergeant wrote 12/04/2018 at 03:30 point

Andy, this looks great.  I'm curious though - if I did this with the ESP, rather than the Sonoff, would it be possible to add in a few more ultrasonic sensors?  I'm thinking one fo the door position and two more for vehicle presence.

I don't have all the parts to try this yet, but I'm under the impression the Sonoff doesn't have enough inputs.  The ESP should, but I know next to nothing about programming them.  I assume I'd just have to find the part where you deal with the one and duplicate it a couple times and stick them on different digital pins on the ESP.

Does that sound sane?

  Are you sure? yes | no

Matt wrote 04/19/2018 at 20:56 point

Why would you need a fan for this thing? And it looks like you have designs for fan parts. Why not just get an off-the-shelf?

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates