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.

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.

Flash it

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.

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.

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

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.

Discussions

Become a member

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.

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.