Facebooktwittergoogle_plusredditpinterestlinkedinmailFacebooktwittergoogle_plusredditpinterestlinkedinmail

I decided I wanted to be able to control my garage door using my phone. After some research, I decided to use an ESP8266 based Wemos D1 Mini and Home Assistant.

Here are the parts that will be needed:

Software Requirements:

I’m going to assume that Home Assistant, MQTT, and Arduino IDE are all setup. There are plenty of guides out there on how to set those up. If anyone reading this does need help with any of that, feel free to post a comment.

    1. The first thing we will need to do is wire up the Wemos. The schematic below shows how I wired mine. If for any reason, you don’t use the same pins as I did on the Wemos, keep in mind that the chip will send some pins to high on startup. That would trigger the relay, and unexpectedly open your garage door. Be mindful of which pin you use.
      Garage Door Opener Wemos Schematic
      Garage Door Opener Wemos Schematic
      My Wemos and relay.
    2. Next, we need to upload the code below to the Wemos. You will need to edit the custom parameters section as needed. The first time you upload the firmware, you will need to do it via serial using a USB cable. After the firmware is on there, you can do the updates Over-The-Air (OTA). The code below uses WifiManager, which starts the Wemos as an AP if it can’t connect to a Wifi network. You can then connect to the AP to setup Wifi on the Wemos. The settings will be saved across reboots.

      For OTA, in Arduino IDE, click Tools -> Flash Size -> 4M (1M SPIFFS). If you don’t do this, the firmware will not work on the Wemos. Next, compile it in Arduino IDE using CTRL+ALT+S. That will compile a *.bin file in the same directory as your *.ino. You can use your web browser to go to http://<Wemos IP>/WebFirmwareUpgrade and upload the *.bin file. It will then reboot in about 30 seconds with the new code.

  1. Now to connect it to the garage door opener. The wired piece of the reed switch will go onto the wall, right next to the garage door. The other part of the reed switch will go on the door itself. You want to be very careful here to make sure that piece is in a place where it won’t get knocked off by something when the garage door moves up.
    The reed switch (The wired piece) has to be within about 1/4″ from the magnetic piece when the door is closed.
    Note the red wire in the first terminal, and the black wire on the second terminal. These are going into Common and NC on the relay.

     

    On the garage door opener itself, you need to find the terminals where the wires for the wall button are located. You will most likely have 4 or more terminals. Two of those terminals are for the wall button. If you can’t figure out which ones they are, then you can try just taking a wire and shorting 2 terminals at a time until the door opens. Or you can disconnect one wire at a time until the button stops working.Once you have found those 2 terminals, connect a wire to each one. The other end of one of the wires(it doesn’t matter which one) goes to the Common terminal on the relay. The other wire connects to the NO (normally open) terminal. Now you can put it in the case and power it up.

  2. The last thing that is left to do is configure Home Assistant. To do that, simply add in the following code into your *.yaml file. This configuration sends the appropriate payload via MQTT to open/close the door, and looks for the payload based on the sensor to determine if the door is closed or not:

    The garage door should show up in Home Assistant as a Cover. You can tell the state of the door based on which arrow is grayed out.

57 Replies to “Wifi Garage Door Controller using a Wemos D1 Mini

    1. The purpose of that resistor is to ‘pull down’ pin D2 to ground by default. This helps prevent things like interference from making D2 appear to be high when it is not. It’s not absolutely required, but you might get some false positives where the ESP thinks the door is open when it is actually not.

      I don’t really recommend not using a resistor in your final project, but you should be fine for testing. Resistors don’t cost very much, so it’s better to be safe.

      Let me know if you need any more help with it.

          1. I dont see a resistor in the picture and i am a noob to this electrical stuff. would you mind showing me how you wired this resistor for learning purposes?

          2. The resistor is there in the schematic. You can basically just connect it directly from D2 to Ground. Let me know if you still need help with it.

      1. The Esp8266 has an internal pullup. Just use this to set the pin mode:

        pinMode(DOOR_PIN, INPUT_PULLUP);

        Then check the state of the door by switching 1 and 0 in the original code

        if (digitalRead(DOOR_PIN) == 1) // get new state of door

        door_state = “OPENED”;

        else if (digitalRead(DOOR_PIN) == 0)

        door_state = “CLOSED”;

        1. Thanks for this John. I had read a little about this a while ago. I just did some more reading on it and it makes sense to me now. But, from what I’m seeing, the Pull-Up is only on pins 0 and 2, right? And I think 2 can’t be used on the wemos because it’s the built-in LED. And it looks like pin 15 has a pull down. Or am I wrong about this? Here’s the URL of what I read: http://escapequotes.net/esp8266-wemos-d1-mini-pins-and-diagram/

          1. Oh interesting, yeah I should look closer. I’m using a NodeMCU v1.0 because it’s what I had on hand.

          2. Trying to track down the source of truth on this. Seems like all the docs I’m finding from Espressif that mention the pull-ups say there are configurable pullups on every pin on the ESP8266 except for GPIO16 (D0 on the Wemos D1 Mini).

            http://bbs.espressif.com/download/file.php?id=893&mode=view

            https://cdn-shop.adafruit.com/product-files/2471/0A-ESP8266__Datasheet__EN_v4.3.pdf

            Super weird though that the official Wemos D1 Mini documentation (https://wiki.wemos.cc/products:d1:d1_mini) only mentions pull-ups on those specific pins. But since the Espressif docs say otherwise, I think the D1 Mini docs are incorrect.

            Let me know if you find info otherwise.

          3. That is pretty much what I am seeing. I would think the expressif is more accurate.
            If I had to guess, the expressif is more accurate in that the pull up/down is configurable. Maybe on the Wemos, it’s set and you can’t change it. I don’t see how a dev board can add a resistor and make it change via code.

      2. Simit, Noob here. is it possible to put up a quick picture of how the resistor is connected? i am still learning and would like to know how this all works. Great project!

  1. I cant figure out how to get code  when I hit ctl alt  s  my computer brings up a hp screen please any help would be great

     

      1. ok thank you where do I past I mean this my screen

        void setup() {
        // put your setup code here, to run once:

        }    do I put it here

        void loop() {
        // put your main code here, to run repeatedly:

        or here or booth

        sorry to bug you but this project is awesome thanks

         

        1. You’re not bugging me. Sorry for taking so long to respond. But you can just delete that code that is there and paste my code in.

  2. Awesome project.  I did something very similar just using a basic HTTP server on the ESP8266 and Tasker + AutoRemote on my Android phone to receive notifications of the door changing status and also send a command to open / close.  Haven’t explored Home Assistant yet, but I’ll have to check it out sometime!

    How does the front-end work?  Is it a website hosted by Home Assistant that you can access on any device or is it an app that you have to configure?

    Thanks!

    1. Thanks. Your best bet is to just check out the home assistant website. HA can run on a Linux or Windows box or even just a raspberry pi. You install it and configure it like I have shown to add sensors. You could probably even add your garage door without Any modification of the ESP.

      Home assistant has a nice quick Web interface. Of you make it public facing you can access it from anywhere but I wouldn’t recommend that because of the security risks.

      Let me know if you have any more questions.

  3. Thanks for a great article.  I am just getting started in ESP8266 and home automation and I really enjoyed your blog.

    However ( at least it is not a but…) I would recommend adding a flyback diode (usually use 1N4407 ) across the VCC and GND pins of the relay.

    See this link for an interesting experiment as to why one is required.  https://electronics.stackexchange.com/questions/146499/relays-flyback-diode-is-it-necessary/146514

     

     

    1. I just checked that out and I’m not sure if I need that. It looks like it might only be the case if a transistor is used. What do you think?

    1. I never had any plans to add one. I think someone on one of the posts on the Home Assistant forum might have mentioned they wanted to do that. I’m not sure they ever did that.

  4. What type of wire should I be buying to lead into the opener? Is there a particular gauge you recommend? I am looking to buy long lengths of it because my power outlet is a ways away from the opener. Thanks for any help!

    1. Sarah, THIS should work for you. Those are each 33 feet long. I’m using about a 25 run myself. Considering the fact that it’s not really pushing a whole lot of current, pretty much any wire should work. Let me know if you need any more help with it.

      1. Simit – thanks very much for posting this project, it’s exactly what I’m after.  For the hookup wire, old telephone cords work well for this sort of thing. Just cut the ends off.

  5. parts of code i had to modify for it to work for me. otherwise relay would not work

    void setup() {

    //Set Relay(output) and Door(input) pins

    pinMode(RELAY_PIN, OUTPUT);

    digitalWrite(RELAY_PIN, LOW);

    pinMode(DOOR_PIN, INPUT);

     

     

    and

     

    //’click’ the relay

    Serial.println(“ON”);

    digitalWrite(RELAY_PIN, HIGH);

    delay(600);

    digitalWrite(RELAY_PIN, LOW);

     

      1. I recall when I started this project I looked for the difference between using pinMode and digitalWrite and I couldn’t find a good answer. I still don’t know what the difference is.

  6. Hey!

    I am trying to use the default Mqtt server built in home assistant(https://home-assistant.io/docs/mqtt/broker/#embedded-broker), but I get the failed to connect to Mqtt server message. in arduino.

    What am I missing here?

     

    #define mqtt_server “192.168.1.31:1883”

    #define door_topic “garage/door”

    #define button_topic “garage/button”

    const char* mqtt_user = “homeassistant”;

    const char* mqtt_pass = “My pass”;

     

    In configuration.yaml file I have your code above and mqtt:

    1. I recommend you switch to something like Mosquitto for your mqtt broker. It’s been a while, but I remember having a lot of issues when I tried to use the HA broker along with a username and password. I switched to Mosquitto and everything just worked. Setup only took a few minutes and I haven’t had to touch it since then.

      1. I tried that next, but I am having issues with the Mosquito broker. Any chance you could show what you have in configuration.yaml and in the Mosquito options part?

        1. Here’s my mosquitto.conf:

          pid_file /var/run/mosquitto.pid
          listener 1883 0.0.0.0
          persistence true
          persistence_location /var/lib/mosquitto/
          persistence_file mosquitto.db
          log_dest syslog
          log_dest stdout
          log_dest topic
          log_type error
          log_type warning
          log_type notice
          log_type information
          connection_messages true
          log_timestamp true
          allow_anonymous false
          password_file /etc/mosquitto/pwfile

          And here’s my HA config:

          mqtt:
          broker: 127.0.0.1
          port: 1883
          username: mqtt_user
          password: mqtt_pass
          discovery: true

          1. Big thanks! I got it working now, I had the Broker adress wrongly set. It was set to the default value, when it needs to be the Pi’s ip adress.

            I am using your code to control an IR led connected to NodeMcu. All is working now, just have to figure out a nice housing for it. 🙂 Your code has been a great help.

          2. Awesome. Glad to hear you got it working. I think I have a link for a good housing in at least a few of my blog posts. You may also consider using those rectangular plastic baby food container if aesthetics isn’t an issue.

    1. I have no idea about Domotica. And I don’t know if it will work directly with Homebridge or not, but I know that if you use it with HomeAssistant, it will definitely work with the HomeBridge-HomeAssistant plugin. If you are wanting to use it with HomeBridge directly, there might be a way to do it with the HomeBridge-MQTT plugin.

  7. John Boiles, I was able to check out your github but i see you are using a d1 mini. I was wondering if you had the nodemcu 1.0 version. I am currently using that board but running into compiling issues on the debug.
    src\main.cpp: In function ‘void loop()’:
    src\main.cpp:166:3: error: ‘Debug’ was not declared in this scope
    Debug.handle();
    ^
    *** [.pioenvs\nodemcuv2\src\main.o] Error 1

    1. I think there was an earlier comment stating this. I’m not sure why, but it works for me. I might change it in the code posted here.

      1. Probably has to do with the type of wemos he is using. code worked fine for my wemos d1 mini. Looked exactly like the one pictured above.

  8. Simit, i bought a few reed switches from amazon and the come with COM, NO, NC any idea how i would wire that in with your diagram? i cannot get the device to monitor the door state properly.

  9. Wire the C to 5 and NO to 16.  When the reed is activated closed, it will close the normally open contact and tell the wemos the door is closed.

  10. @Simit

     

    So I was thinking, I believe the wifi manager stores your wifi credentials in non volatile memory.  If your network goes out, wont it go into WAP mode, allowing you to connect and set a new host network to join?

    I know it’s an edge case, but also one rather easy to solve…either disabling that feature or just setting static wifi credentials.

Leave a Reply

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