MQTT Doorbell

Hi there,
My first ESP8266 project became an MQTT doorbell.
When the doorbell is pressed my Wemos D1 Mini Pro send a MQTT message to my MQTT broker which Home Assistant is subscribing to.

In my Home Assistant automation I play a doorbell sound in my Sonos Play: 1 speaker then send a push notice using Pushbullet with an attached snapshot from my Ubiquiti G3 Camera mounted close to the door.

A bonus with this project is that after the kids are asleep at 7pm the volume on the doorbell sound is much lower. In case someone decides to ring the doorbell late at night.

A video showing the doorbell (Sorry about the quality):

24 Likes

Well done! So if I understand you correctly, you entirely bypassed the ā€œtraditional doorbell setupā€? Iā€™ve been thinking about a setup that can shut down the existing doorbell and switches to a solution like you propose for the specific situation when kids are asleep. But I think thatā€™s just because I donā€™t want the doorbell to be entirely reliant on HA and also want it to work when the system is down.

But while Iā€™m typing this another solution comes to mind, one where I attach a nice plug to the wires of the button that I can easily switch from the ESP to the existing wiring. That way I would have your setup, but if I canā€™t/wonā€™t use HA for a while I can easily plug the switch back to the old doorbell. Hmm, letā€™s give this a little more thought. But thanks for sharing!

Iā€™m having an issue with my doorbell.

When i restart Home Assistant hass start subscribing to MQTT again which in turn triggers the Doorbell automation.

So every time i restart Home Assistant my doorbell rings. How can I fix this?

1 Like

It sound to me as if the topic has been published with the ā€˜retainā€™ flag set. This causes anything that subscribes to that topic to receive the last value whenever it starts subscribing.

To fix the problem, clear the retain flag for the topic by sending a message to the topic with the retain flag set and with a null payload. Using mosquitto_pub you do

$ mosquitto_pub -t your_topic -r -n

Then fix the door bell by not setting the retain flag when the message is sent.

3 Likes

How did u manage to send a pic via push bullet? Wanna make something similar with xmpp

There are different ways of doing it using their API.
I am using the Perl script mentioned in this thread.

Much much easier to do with Telegram imho:

  action:
    - service: notify.telegram
      data:
        title: Doorbell
        message: 'Ringklockan ringer'
        data:
          photo:
          - url: !secret doorcam_url
            username: visitor
            password: !secret doorcam_pw
2 Likes

Yes I have tried Telegram aswell but I believe there was something that annoyed me.
I think it was something trivial like it lacked the possibility to change the icon in the notification or something :joy:

I am new to this whole ESP8266 thing and I am curious as I am looking into a similar solution for our doorbell.
Where did you find the instructions to make this? Is it possible you share a little bit more details on how you created this?

I did like so.

Connect one cable from the doorbell to a pin on your ESP8266, I used D3 as you can see in the variable doorbellPin below.
Connect the other cable from the doorbell to GND on your ESP8266. (More info on Input Pullup [here] (https://www.arduino.cc/en/Tutorial/InputPullupSerial) )

Just ask if theres anything i can help you with.

My ESP8266 code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

//WIFI
const char* wifi_ssid = "wifi";
const char* wifi_password = "wifi-password";

//MQTT
const char* mqtt_server = "mqtt server ip";
const char* mqtt_user = "mqtt username";
const char* mqtt_password = "mqtt password";
const char* clientID = "Doorbell";

//VARS
const char* doorbell_topic = "home/outdoors/doorbell";
const int doorbellPin = D3;
int doorbellState = 0;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  pinMode(doorbellPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void blink_now(){
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
}

void setup_wifi() {
  //Turn off Access Point
  WiFi.mode(WIFI_STA);
  delay(10);
  
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    blink_now();
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    blink_now();
    // Attempt to connect
    if (client.connect(clientID, mqtt_user, mqtt_password)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(doorbell_topic, "Doorbell connected to MQTT");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  doorbellState = digitalRead(doorbellPin);

  if ( doorbellState == LOW ) {
    // Put your code here.  e.g. connect, send, disconnect.
    Serial.println("Doorbell is pressed!");
    client.publish(doorbell_topic, "on", true);
    blink_now();

    delay( 5000 );
  }
}
2 Likes

Thanks for sharing this. I will give it a go soon :slight_smile:

Hi. Im trying to something similar and got the idea from : https://forum.mysensors.org/topic/2064/how-to-doorbell-automation-hack

To get to know the ESP thingy. I tried your code and its working and HA is receiving it, but I cant get it to work. Could you show us the configuation.yaml part for your code?

If anyone would want to give me hand to translate the mysensors code to esp code I would be forever greatful :smiley:

My automation code:

#################################################################
## Doorbell automation
#################################################################

- alias: Doorbell ringing
  trigger:
    platform: mqtt
    topic: home/outdoors/doorbell
    payload: 'on'
  action:
    - service: script.turn_on
      entity_id: script.sonos_doorbell
      data:
        variables:
          where: 'kitchen'
    - service: shell_command.doorbell_notification

Thanks :slight_smile:. So no need to setup mqtt sensor?

(edit) Confirmed. No need for sensor. Working as intended. :smiley:

1 Like

Just a tip here on hard coding your config details: Iā€™m using the WifiManager library, which has a feature that lets you offer a config web page to users via the ESP8266ā€™s local access point. The config mode is triggered either by being unable to connect to the access point already configured (e.g. very first time), or if you press a hardware button to re-configure.

You get a callback if the user changed any of your custom parameters, which you can write to a JSON file. There are examples with the WifiManager library showing how to do this.

2 Likes

Hi guys. I gave up copying the mysensors code. But instead used this code to control the relay (based on blink). So now I could connect this to my doorbell and the only thing I would be missing is a way to silence the bell (inhibit relay), I also fixed the retain so it doesnt go of at every restart :slight_smile:
I tested the ESP with a simple MQTT switch code so I know its just a matter of making a proper function.
Thanks again @christian.ek

I have been searching the internet a long time to find a solution like this, so thanks! I got the ESP and automations working, so now I need to install it into the system.

But maybe somebody can help me woth this: my doorbellsystem works on 8v, but the NodeMCU can only handle max 3.3V (if im not mistaken. It doesnt handle 8V, I found out the hard way :blush:). I am a bit of a electronics noob, so for me itā€™s not clear how to tackle this problem. Cause of the wiring I need to attach the node before the bell. Two questions:

  1. How can I tackle the 8v problem?
  2. How can I attach the node before the bell and still use it? Maybe with some kind of passthrough?

My apologies if these questions are stupid, buy I really have no idea. Thanks!

Hi. Take a look in the Mysensors link I pasted. A relay is the safest way to go and it looks like a good solution. Im a newbie in regards to esp and arduino ide, but are trying to make a sketch based on the Mysensors idea. :slight_smile: If it is working as intended I can share it when its done. For now its working, but without a way to silence the bell.

you have a tutorial?

Writing up a doorbell project, and comments/feedback welcome! https://www.hackster.io/robin-cole/pi-camera-doorbell-with-notifications-408d3d

2 Likes