Chimera Computing Ltd
Electronic Projects


The following outlines how to build an Arduino Garage Door Opener that integrates into OpenHAB/MQTT. This is designed to work with an Arduino Uno Rev3 with Ethershield board (although this can be altered to work with the Arduino Yun, Pro or similar)

OpenHAB and Mosquitto

The Arduino code has been written specifically to work with OpenHAB (open source home automation software) and an MQTT broker such as Mosquitto (a messaging subscribe/publish type protocol that is lightweight and great for communication between devices) Don't let the names or acronyms scare you, they are really quite simple to use once you get to know how they work. I utilise OpenHAB on a NTC C.H.I.P however many people use a Raspberry Pi or similar. This tutorial assumes you have implemented OpenHAB (if you need help setting up OpenHAB, follow this excellent article from the makeuseof web site) You also need to download and install Mosquitto (MQTT broker) You can install this elsewhere on your network, however most people just install this on the same machine as OpenHAB to keep it simple.

To install Mosquitto, following this link then select the type of device you are using and follow the instructions. Because C.H.I.P runs Debian (Jessie), you can follow the Raspberry Pi instructions if you are using C.H.I.P. for your Home Automation device (also note, its best to reconfigure CHIP to boot from the CLI. There are instructions for this here)

Once you have OpenHAB and Mosquitto running, you need to prepare the Arduino IDE for the code below. First, you need to add the PubSubClient library. In the Arduino IDE, from the menu's go to Sketch, Include Library, Manage Libraries In the filter search box, type PubSubClient then highlight the search result and click to Install (at time of writing, the latest version is 2.6.0) You will also need the Ethernet library which covers extensions for the Ethernet shield.

Parts Needed / Assembly

The Garage Door opener does 2 things. First, it provides a button in OpenHAB to activate the door to open or close. Secondly it provides feedback to OpenHAB on the status of the door (either open, closed or partially open)

You will need the following parts:

  • 1 x Arduino Uno Rev3 Board
  • 1 x Ethernet Shield
  • 1 x 1K Ohm Resistor
  • 1 x 2N2222 Transistor (or 2N3704, BC548)
  • 1 x 1N4001 Diode (or 1N4004, 1N4007)
  • 2 x Reed Switches
  • 1 x 5V Relay

  • The resistor, transistor and diode help create the circuit to the relay, which is triggered by a button in OpenHAB to open/close the door. The reed switches provide feedback to OpenHAB as to the garage doors status. These parts can be sourced cheaply from any electronics shop. Using these components is also the recommended way to configure the Arduino with a relay as per the Arduino site (note: the LED on the breadboard below is simply for test purposes, the red/black wires will usually go to the garage door opener)

    Below is a sample image of what the Garage Door opener section looks like in the OpenHAB interface (PC web browser, iPhone/Android app etc)

    So, to spell it out in laymens terms... with the garage door in an 'open' state as shown below, clicking the 'Close' button will trigger OpenHAB to send an MQTT message to the Arduino, the Arduino will then open the relay for 1 second, causing the door to close. The (door open) reed switch will become open which the Arduino will pick up and send a new door status back to OpenHAB which will then display the updated status 'Partially Open' until the door fully closes, at which stage the (door closed) reed switch will close, this will be detected by the Arduino which will then send a new message to the MQTT broker and status will then update to 'Closed'

    Arduino Uno R3:


    Click the link to download the Arduino Garage Door code:


    Open in the Arduino IDE (from the menu's, you may have to go to Sketch, Include Library, Manage Libraries and ensure that the following are installed: Ethernet Built-In, PubSubClient and DHT Sensor library), Click the Verify button to ensure it compiles ok. Alter the following IP address in the code and change it to point to the IP address of the device that is running your MQTT broker.
    // MQTT Broker IPAddress MQTT_SERVER(192, 168, 222, 254);

    Also, if you do not have a DHCP server that can issue your Arduino an IP address, you will have to set a fixed IP address for the Arduino. Remove the // from the start of the following line and enter an IP address:
    // Remove remarks and alter if you want a fixed IP for the Arduino // IPAddress ip(192, 168, 222, 100);

    For testing the door activation, open Serial Monitor in Arduino then click the 'Open' or 'Close' button in OpenHAB to open/close the Garage Door - you should see an inbound "ACTIVATE" or "DEACTIVATE" messages coming in on the Arduino serial interface and you should also here the relay 'click'

    If you need to troubleshoot incoming MQTT messages for the door status, then on the OpenHAB/Mosquitto server, run the command:
    mosquitto_sub -d -t "openhab/garage/doorstatus"

    You should see inbound PUBLISH messages (numbers 0,1 or 2) coming in from the Arduino which indicate the door state (open, closed, partially open)

    Alter the "define" fields at the top of the code if you want to alter any of the pin's for data input.

    Remove any code referencing DHT if you do not want to have humidity or temperature sensing in your garage otherwise click here if you want basic instructions on how to connect the DHT22 sensor.