DIY Wireless Smart Doorbell: Program ESP8266 NodeMCU with Arduino or ESPHomeYAML

MQTT Doorbell and Door Sensor

Today on the hookup we’re going to modify a cheap wireless doorbell from amazon to make an MQTT connected doorbell that interfaces with home assistant to send notifications and images.

The ring doorbell is pretty cool, but a few things about my smart home setup made me decide against buying it.  First, I do not have a wired doorbell on my house, so I would have had to use the battery operated version, which I’ve not heard great things about.  Second, I already have a camera pointed at my front door, so adding another camera on the doorbell seemed like overkill.  And third, I’ve tried to craft my smart home to function primarily on my local network and avoid cloud services whenever possible so internet outages don’t render the system useless and this allows me to keep with that theme.

I figured there was no reason to reinvent the wheel here, so I headed over to amazon to get a super cheap wireless doorbell.  11 dollars and two days later I had this doorbell by the company 1 by 1,  now I actually built this project over a year ago, so I asked 1 by 1 to send me a free one to take apart for this video, but they never responded up so I went ahead and just bought another one, you guys are worth it right?

We’re going to combine this thing with an ESP8266 nodeMCU board to give it wifi capabilities.  I specifically selected the battery version of this doorbell because I knew it ran off 2 double AA batteries, or the equivalent of 3 volts, which happens to be a voltage my NodeMCU can supply.  They make a version of this with an AC wall plug that includes the transformer from 120V AC to 3v DC, but I didn’t want to mess with mains voltage for this project, so I just went with the battery powered one.

Out of the box the doorbell has 3 modes: LED only, LED plus sound, and sound only.  Our modified version is going to use the LED output to determine whether the doorbell has been pressed to send notifications.  We’re also going to add an MQTT switch to silence the doorbell chime, and optionally a reed switch to detect the state of the front door.  This plastic container will make a great housing for our project, but if you want to use a different enclosure it’s a good idea to label the buttons on the board so you don’t forget what they do… don’t ask me how I know that.

Lets get this thing wired up.  Here’s the schematic for this creation.  We’re going to hook up the LED output to pin D0 because this pin on the ESP8266 has a built in pull-down resistor, which means we don’t need to add one ourselves.  We’ll connect D4 to the base of our transistor to control whether the chime is active, and we’ll also hook up our reed switch to both D3 and Ground.  We can power the whole thing using a USB micro cable plugged into a 5V wall brick.  The small doorbell PCB is powered the 3v3 pin and the ground pin on the NodeMCU.

The actual code is pretty simple.  We use a NodeMCU as an MQTT client and monitor one of the GPIO pins for a high signal that corresponds to the LED.  If that pin goes to HIGH we’ll send an MQTT message to Doorbell with the message Ding.  You can obviously change this to any message you want to fit in with your MQTT schema. Since the LED flashes on and off twice and we only want to be alerted one time by each doorbell ring there’s a little bit of code here that limits the number of messages sent to 1 every 6 seconds.

The other functionality of this code is to silence the doorbell.  If you send the message “silent doorbell” to the topic “doorbell/commands” it will turn off the speaker on the device and will only trigger the notification.  If you send “audio doorbell” to the same topic it will activate that switch and the doorbell will play through the speaker and notify you.

The last functionality I’ve included is a reed switch that can monitor your front door.  This is a wired reed switch, so you’ll need to put the doorbell unit somewhere close to the door where you can also run a wire to the door. If you do choose to use it it will send the MQTT message Open to the topic doors/front if the door opens and Closed to door/front when it closes.  Each of these messages uses a retain flag so if your home assistant instance restarts it the status of the door will be resent once home assistant subscribes to the door/Front MQTT feed.   The rest of the doorbell will still work perfectly fine even if you don’t wire up this reed switch, so if you don’t want to  use it you can skip it.

To add this into home assistant we’re going to add a few things to our configuration.yaml file.  We’re going to make an MQTT sensor for the doorbell with an expire after value of 10 seconds, and an MQTT switch to silence the doobell with the commands and state topics specified in the code, last we’ve got that optional MQTT binary sensor for the front door, you’ll only need to put this entry in if you wired up the reed switch.

 

Now that home assistant knows when the doorbell rings, lets make a fancy automation to include an image from an existing home assistant camera in the message.  We’re going to use the binary sensor “doorbell” as a trigger, we’re going to call my ios notification as an action, and we’re going to utilize this super secret URL that contains a JPEG snapshot to get an image into that notification.  I’ll include a copy of this YAML in the description.  If you want to see how to do this and more in NodeRED make sure to check out my mastering node-red series.  All that’s left to do now is test it out.

First we’ll flip the chime switch on, then ring the doorbell.

Next lets turn the chime off and ring the doorbell again.

Lets do a couple opens and closes of the front door to test out our reed switch.

Perfect.

I wrote this Arduino code for you, and I really think writing Arduino is a great skill to have if you’re into tinkering with electronics so I REALLY want to encourage you to try and step through my code and figure out what’s going on.  However, if you aren’t interested in learning to code in Arduino I’m also going to show you how to set this up in hassio without having to mess with the Arduino IDE at all.

Start by heading into your hassio page and we’re going to add otto winter’s repository.   He has created a tool called esphomeyaml which will program our esp8266 and esp32 devices for us.  After installing the addon go ahead and click on start, and then open web ui.  If you use a dynamic DNS service you’ll need to either forward this port on your router (which I wouldn’t recommend), or go to the actual ip of your hassio device.  If you’ve used node-red you’ve had to deal with this before, same thing, different port.

We’re going to set up a new device and call it doorbell, note here that you need to use only lowercase letters or it will throw an error.  In the next step you need to specify the board you’re going to use, we are using a nodemcuv2, but the rest of the board definitions can be found by clicking on the links in this text box.  In the third step enter your wireless SSID and password.  Fourth you’ll put in your MQTT broker address login and password, for me my mqtt ip is the same as my hassio ip since I run mosquito as a hassio addon.  After clicking done you’ll now have a new file generated in your hassio config esphomeyaml folder.  Open that file up in your favorite text editor, I’m going to use notepad++.  When you open this file up it should look familiar, we’re going to program our ESP8266 in the same way we write yaml for our configuration file, which if you’re like me means you’re mostly going to copy and paste stuff.

We’re going to set up the same features as before.  A binary sensor for our doorbell, on D0 with an input pulldown, a binary sensor for the front door on pin D3 with an input pullup, and a switch on pin D4.  After we’ve added this stuff we can hit save and head back over to our hassio addon, plug in our nodemcu to our pi using a USB micro cable and restart the addon.  This step is necessary to allow the board to be recognized over USB.  Once the addon restarts open up your web ui and click validate, and if all is well click upload.  Go grab a drink, because this step takes a while.

While this thing is uploading we can add a few lines to our configuration.yaml file to enable MQTT discovery.  By doing this our switches and sensors that we just configured on our nodeMCU will add themselves automatically after a restart of home assistant.  Add discovery true, and discovery prefix home assistant under mqtt and you’re all set.

By now our nodemcu should be all programmed, go ahead and head back over to the esphomeyaml addon and check to make sure your sensors are working.  If you connect a wire to D0 and 3V3 it should trigger “on” and if you connect a wire between D3 and ground it should trigger “on”.  If all is good you can go ahead and restart home assistant and your sensors will be there.

In the Arduino code we were able to limit the amount of times the doorbell triggers, since esphomeyaml is more of a generic system we’re not going to be able to do that, but we can figure fix that issue in node red after the fact.  Again, if you want to get into DIY electronics stuff I highly recommend you learn Arduino, but esphomeyaml is a pretty neat tool if you just want to make a few sensors the quick and easy way.

Hopefully you were able to pick up a few useful things from this video.  The description has all of the parts you need to make this project as well and the Arduino code and the esphomeyaml code.  If you’re wondering where the backup video I mentioned a few weeks ago is, things got a little weird and I’m trying to figure some stuff out before I post the video.   If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.

I know everyone has a different home setup, so this project may not be directly applicable to yours, but I hope this gave you some ideas of how you can use a NodeMCU to repurpose dumb devices and connect them to your home assistant instance.  Amazon links to all the items I used are in the description.  If you enjoyed this video please consider subscribing, and as always, thanks for watching the hookup.

Leave a Reply

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