MQTT Doorbell

The way I did is to simply put an octocoupler in and connect it to a NodeMCU. Everytime the doorbell is pressed the pin of the NodeMCU turn to high and it sends a simple message to HASS over MQTT. Not home now, but if you want I can post a photo how it looks (its real silmple).

This is the code I use on the node:

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

//WIFI
const char* wifi_ssid = "SSID";
const char* wifi_password = "PASSWORD";

//MQTT
const char* mqtt_server = "MQTTIP";
const char* mqtt_user = "MQTTUSER";
const char* mqtt_password = "MQTTPASSWORD";
const char* clientID = "CLIENTNAME IE 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( 3000 );
  }
}

In HASS I’m sending it to pushbullet via an automation.

2 Likes

I know this is an old post, but one way you could achieve that without having to use a plug would be a relay operated by the same ESP, which it energises when you want to run the doorbell via HA changing over the button wires to the ESP, and leaves the wiring running through its NC contact to the existing doorbell system for use in its de-energised state. That way if HA is down, the ESP would not have energised the relay and your existing doorbell continues to function.

I am now using shielded Cables between wemos and the doorbell button.

I still get the false triggers sometimes when someone flicks the switch in the bathroom. Not sure how i can fix this? Anyone with any tips? =)

@armandjanssen I copied your setup and code and with some small issues it’s pretty well working.
Got some issues with false switchings, looks like that the input is floating, did you got the same issues or somebody else can assist?

What are the options?

  • Input High for minimum 1,5 / 2 seconds? And how can I add that easily to the code?
  • Pullup Resistor?

Thanks in advance

Had an ESP laying about and a cheap 433mhz door bell just gave this a go, i fitted the esp to the ringer box rather than the actual bell working well ! Now to solder :slight_smile:

1 Like

Anyone tried the reed switch in the bell? So that when the traditional bell rings it creates a magnetic field and closes the reed switch
 was planning on testing this
 got the bits just haven’t had time.

I want the original bell to operate as normal but make it “smart” in a non-intrusive way.

I removed the relay method a while ago because of false triggers. Haha! The family got very confused by the phantom door bell but it was quite entertaining for a couple of days!

Works as intended, No false or missed triggers so far :slight_smile:

Did some testning with a arcard button, notification with the phone works just perfect and sound.

Only thing left is the light in the button

Hi,

It loses sometimes the connection to my MQTT server. Due too the fact I’m rebooting HASS.IO etc etc.
But does not always reconnect it, what you already described in your code.

But is there some update or idea how to solve this?
I’am quite new to arduino so need to get a bit more experience how program it.

Is there a way to report the mqtt state to “off” after x amount of time. I only receive “on” message via mqtt when door bell is pressed, would be good to monitor the state of the door bell. Can this be done within Homeassistant ?

My setup sends an Off Status after 3 seconds when the button gets pushed. This also prevents the system from multiple commands per seconds if some kids have fun with your doorbell. Within those 3 seconds the system is on hold and wont send any other status.

Hope this helps

delay(3000);
Serial.print("Send message: ");
Serial.println(offMsg);
client.publish(outTopic, offMsg);
1 Like

perfect thanks :slight_smile:

In an attempt to get rid of the false triggers I added a 10k resistor between the button and the Wemos input port.
Sadly I am having issues to get this running


When I upload the code and have the wemos still connected to the computer it works like expected, When i press the button the wemos send an MQTT message.

BUT when i disconnect the computer and connect Wemos to the wall plug instead it connects to the MQTT server like before but when i push the button nothing happens. Not sure why
 Any thoughts?

I don’t have false switchings. It just works like a charm :slight_smile: Except that I build in a reboot every 12 hours, because it stopped working after some time. Probably the wifi or mqtt connection.

I got the same issue with that, could you please share your code with us? Thanks in advance!

Here is my code for resetting the wemos board:

long resetInMiliSeconds = 1000 * 60 * 60 * 12; // 24 hours

In start of loop():

 if (millis() > resetInMiliSeconds) {
    Serial.println("Restarting....");
    ESP.restart();
  }

The function ‘millis()’ returns the number of milliseconds since the Arduino board began running the current program (millis() - Arduino Reference). If it is running longer than 24 hours, I reset it.

I also had a lot of false triggering without a resistor. I just learned about the floating state and saw that I didn’t use the internal Pull up resistor “INPUT_PULLUP”. :thinking: Have you tried this? My cable the doorbell button is about 6m long. I tried a sonoff basic with the same result. I soldered a 10k resistor between the 3v3 and GPIO14 and the problem seems to be gone since yesterday.

I will try to change my code to use the pull up resistor on the wemos and will get back to you with the result if you’re interested.

How long is your cable? From what I read, a capacitor could help.

Hi!

Thanks for sharing your work. I just copied and pasted your code (and replaced fields for wifi, username, etc.) but I always get the “exit status 1” error, so I can’t compile it. Any ideas?
Thanks!

i am having the same problem with fantom doorbell, usually at night
 wife will kill me one of these days :slight_smile:

I am not a electronics guy any suggestions ??

Since I added a physical resistor to the setup instead of only using pullup resistor I have not experienced the fantom doorbells. I will report back in a couple of days to see if it still is so.

1 Like