Blynk and MQTT with Garage Door Controller

So I built a wifi garage door controller using an ESP8266 board (D1 Mini). The hardware just uses relays to control the doors and reed switches to sense whether or not the doors are open or closed. I did the project in phases.

Phase 1 was to build the hardware and just have it working with Blynk. This worked great and I could control open/close and see the garage door status on my phone. I did this with just the basic Blynk.begin(auth, ssid, pass) statement in the setup and in the loop. This worked great and I had it running for several months just like this while I worked on the Phase 2 portion.

Phase 2 was to keep this functionality but also add in MQTT control so that I could integrate it into my Home Assistant installation that is running on my raspberry pi (running mosquitto MQTT server). I was able to get all of the control working correctly and have it both be controlled by Blynk or HA. To do this, I kept the normal statement in the loop, but changed the setup to be Blynk.config(auth) instead as I had already connected to wifi with the other standard WiFi.begin(ssid, password) statements seen in many examples. I then added the logic to look at the reed switches and send MQTT messages for status and also look for MQTT messages to open/close the garage door.

Initially, everything seemed to be working correctly. The problem was that I would lose wifi connection every 1-2 days and the ESP8266 would not reconnect to the network without a power cycle. I tried to include statements in the loop to reconnect to wifi if it disconnects, but nevertheless I would lose connection after 1-2 days and be dead in the water again until a power cycle. I rolled back the code to just the Blynk functionality and the connection is steady as a rock again.

So I guess my question is, is there something magical about the Blynk library to be able to maintain a steady wifi connection? And if so, is it possible to have Blynk directly send the MQTT messages to my home automation platform? I’ve tried to read into this a bit, but it seems the MQTT functionality with Blynk isn’t well documented. There seemed to be other workarounds like using node red in the middle as a translator between HA and Blynk, but I’m not sure if this is the right way to go or not.

I can post the full code if that would help, but seeing as how I’m a hardware engineer by trade, it’s not the prettiest.

Any help would be appreciated, thanks!

I use Blynk, MQTT, Alexa etc to control my devices.
I use Node-Red, and the code running on my ESPs has no Blynk code at all. They simply connect to Wi-Fi and MQTT and subscribe/publish appropriate MQTT messages and act on the MQTTmessages that they receive.

This works really well for me, and has the advantage that you don’t have to worry about restricting what’s running in your void loop (provided you don’t miss inbound MQTT messages of course).

I really like Node-Red. It’s not the visual/graphical ‘no code’ programming environment that attracts me - because in reality you write functions anyway - but the ease of integration with Blynk, Alexa, Ikea TRÅDFRI etc is what I love.


I had a similar issue to yours with my garage door setup. Although my hardware is different (arduino Nano with esp-01 shield) You can use the Blynk.connected() function to detect when it becomes disconnected and try to reconnect. This helped, but there were still times it wouldn’t reconnect until I pressed the reset button. Ultimately I had to enable the watchdog timer on my arduino as well and have it automatically restart when it became disconnected.

For me it seemed like it was disconnecting more when the garage door was open. Don’t know if your seeing similar behavior. I suspect the door being open was affecting the WiFi signal somehow.

Oh I hadn’t thought to use it that way. So do you have to have a local blynk server set up to interface with node-red? I’m currently only using the cloud server, but I’ve been thinking about setting up the local server anyway.

That was a method I was thinking about as well. However, when my esp resets it activates the garage doors as the relays are wired active low. Maybe not the best design choice in retrospect.

No, I use the Blynk cloud server. It works really well that way.


You should change that, whichever way you go forward. Try using a different pin to activate your relays, or pull the pin you’re using high immediately after reboot. You could also use pull-up resistors, but I’ve always managed to steer clear of those in the past.


I have my garage doors set up to turn on a led on my blink panel as well as send me an email when they are opened. I did this before I know what MQTT was. I had the
reset on power up problem also but changed the pins to different ones. It also reports the temp and humidity in my basement via a DHT22. I am using a Wemos D1 mini as my controller. Everything has worked great so far and I love it.