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.
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.
@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?
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
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!
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.
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 Except that I build in a reboot every 12 hours, because it stopped working after some time. Probably the wifi or mqtt connection.
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â. 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.
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!
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.