Skip to content
My Account

HomeKit

ESP8266 HomeKit Garage Door Opener

A HomeKit garage door opener on an ESP8266: a relay pulses the opener input and a reed sensor with a magnet on the door reports open or closed. A low-voltage test setup, built from source.

  • ESP8266
  • HomeKit
  • Safety notes
  • 4 min read
No ratings yet, be the first.

A HomeKit garage door opener on an ESP8266. It shows up in the Home app as a garage door, opens and closes it by pulsing a relay that is wired to the input of the opener, and reads a reed sensor with a magnet on the door to know whether the door is open or closed.

This is a low-voltage test setup, and part of learning: the author got more skilled at programming with this project and understood better how HomeKit works.

Read this first

When you add the accessory to the Home app, HomeKit sends an Identify request, and this firmware answers it by pulsing the relay three times. That can start the garage door, so keep people and objects clear of the door while you add the accessory.

The relay is wired to the input of the garage door opener, in the test setup on low voltage. Do not connect mains voltage to the relay unless you know how to work on it safely.

What it does

FeatureDetails
HomeKit serviceGarage Door Opener, with current door state, target door state and obstruction detected. Obstruction detection always reports no obstruction
Opening and closingA request pulses the relay for 0.4 seconds, like pressing the button of the opener
Door positionA reed sensor with a magnet on the door reports whether the door is open or closed
Travel timeAfter a request the state shows opening or closing for 22 seconds, then the sensor is read
No cloudControl stays on your own network

Hardware

ComponentNotes
ESP8266 moduleAn Ai-Thinker ESP8266 module of the ESP-12 type. See the ESP8266 pinout
1-channel relay moduleA Keyes SRD-05VDC-SL-C module, wired to the input of the garage door opener
Reed contact and magnetShown as the contact sensor in the diagram, with the magnet on the door
2 push buttonsRESET and PROGRAM
USB-serial (FTDI) adapterFor flashing and for power in this test setup. Set it to 3.3 V logic
470 µF capacitorAcross the supply, needed for stable operation when powering from the FTDI adapter
ResistorsPull-ups, as drawn

In this test setup the module is powered from the FTDI adapter, which is not recommended for long-term use.

Wiring and pins

The Fritzing diagram at the top of this page shows the build on a breadboard, with the pinout of the module in the corner. The pins are set in the Makefile of the project:

FunctionESP8266 pin
RelayGPIO2
Reed sensorGPIO4

The firmware switches the relay by pulling its pin low. The reed sensor input uses the internal pull-up, and every change of the input triggers an interrupt. Use an FTDI adapter set to 3.3 V, because a 5 V adapter can damage the module.

How the door state works

The firmware reads the reed contact as follows: when the contact is closed, the door is reported as open, and when the contact is open, the door is reported as closed. The magnet on the door decides which of the two happens where, so check the state in the Home app after you mount the sensor.

SituationWhat the firmware does
Request to open while closedPulses the relay, reports opening, and after 22 seconds reads the sensor
Request to close while openPulses the relay, reports closing, and after 22 seconds reads the sensor
Request equal to the current stateIgnored
Request while opening or closingIgnored
Sensor changes while opening or closingIgnored until the 22 seconds are over

Build from source

This repository has no release with a ready-made binary, so you compile the firmware yourself. It builds with ESP-Open-RTOS and the esp-homekit library by Maxim Kulkin. The Makefile expects the wifi_config, cJSON, wolfssl and homekit components in the folder layout of that project, and the http-parser and dhcpserver extras of the SDK.

FileWhat it is
garage/garage.cThe accessory, the door state logic and the relay toggle
garage/contact_sensor.cThe reed sensor input with its interrupt
garage/relay_actor.cThe relay output
garage/button_sensor.cA button input
garage/wemos.hGPIO names as printed on WEMOS D1 style boards
garage/garage.c2An alternative version of the source
garage/MakefileBuild settings: REED_PIN 4, RELAY_PIN 2, FLASH_SIZE 32, and a serial monitor at 9600 baud

The HomeKit setup code is set in garage.c. Change it to your own before you use the device for real.

Good to know

  • This is a low-voltage test setup and not a finished product.
  • The HomeKit Accessory Protocol specification the firmware follows is the non-commercial version, for accessories you build yourself and do not distribute or sell.
  • The repository is under the MIT licence.

Everything is in the ESP8266-HomeKit-Garage-Door repository on GitHub.

Advertisement
All HomeKit

More HomeKit

Keep building.

Switches, sensors, plugs and blinds: more accessories that you build yourself and add to the Home app.

All HomeKit