Today I made the integration with Wemos, MQTT, Sonoff and Home-assistant. Thanks to the code above. I will try to make an entry soon where I explain what I did and with my experiences. Perhaps I will have some bugs in the system by then
But again thanks for sharing your initial code and making me enthusiastic to do this project as well
Hey, I got it working and even included a Dallas temperature sensor. I still have some false ringing, but think it is related to reconnect to somethingā¦ Either MQTT or wifi. When in silent mode there are no false alarmsā¦
If you want the Arduino code I would like to clean it up a bit first and maybe I can get rid of false alarms (this is my first attempt ever at something like this )
At the same time and in cooperation with @adwolters I created a smarter doorbell and like to share my results here for my first arduino project using a WEMOS D1 Mini Pro. Difference with Ad is that he uses a sonoff and I an relay.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//WIFI
const char* wifi_ssid = "your_ssid";
const char* wifi_password = "your_wifi_password";
//MQTT
const char* mqtt_server = "your_mqqt_server_ip_adress";
const int mqtt_port = 1883;
const char* mqtt_user = "";
const char* mqtt_password = "";
const char* clientID = "Doorbell";
//VARS
const char* doorbell_topic = "homeassistant/house/doorbell/";
const int doorbellPin = D2;
const int relayPin = D1;
int doorbellState = 0;
// when mqtt is offline, skip, so bell still works
int mqttRetryCount = 0;
int maxMqttRetry = 3;
boolean connectToMqtt = true;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
pinMode(doorbellPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
// DEBUG blink, duur 1 sec
void blink_now(){
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
}
// 2de DEBUG blink, duur 500 msec
void quickblink_now(){
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
}
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() {
mqttRetryCount = 0;
// Loop until we're reconnected
while (mqttRetryCount < maxMqttRetry && !client.connected()) {
Serial.print("Attempting MQTT connection...");
quickblink_now();
quickblink_now();
// Attempt to connect
if (client.connect(clientID)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(doorbell_topic, "Doorbell connected to MQTT");
connectToMqtt = true;
// set to hundred to jump out of loop
mqttRetryCount = 100;
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.print(" try again in 5 seconds - retry count: ");
Serial.println(mqttRetryCount);
mqttRetryCount++;
connectToMqtt = false;
// Wait 3 seconds before retrying
delay(3000);
}
}
}
void loop() {
if (connectToMqtt && !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!");
digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
delay( 1000 );
digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
// publish to mqtt
client.publish(doorbell_topic, "ON", true);
blink_now();
delay( 1000 );
client.publish(doorbell_topic, "OFF", true);
// block bell for 4 seconds
delay( 4000 );
}
}
In the arduino code I build a small āroutineā to skip the mqtt connection, after a # number of retries (not the best code, but it is simple and fast, I think). So the bell will still function when mosquito goes down. I havenāt fount a solution yet, for a failing wifi (havenāt tested yet).
- alias: "There is someone at the door"
trigger:
- platform: state
entity_id: binary_sensor.deurbel
state: 'on'
action:
- service: notify.pushbullet_notify
data:
title: "There is someone at the door!"
message: "Doorbell!!!"
After a lot of leeching on this site and forum it was time give something back in this first post
@quadmasta U could use that functionality to use the bell as an alarm signal.
In my solution, the bell would subscribe to an mqtt topic and then enter a loop which rings te bell each second for 2 minutes (or something like that).
@quadmasta Do you mean a relay like this? If yes, please elaborate why it would / could die?!
I donāt know that much about this stuff. Just copying, stealing and learning on the way.
I had some issues with interference from cables that run along the doorbell cables in the wall. Whenever someone turned on the light in the downstairs bathroom it caused the Wemos to trigger and the doorbell to go off.
I suspect that the wires are close to one and another causing some electric field which in turn was detected by the Wemos as if the doorbell was pressed. Getting the wiring redone was not an option so I had to check for different ways.
Inspired by the following blog http://brentsaltzman.com/make-your-doorbell-smart/ I order a reed relais. Basically this relais makes contact when a magnet is present. The bell operates with a magnetic field. So when the old doorbell is activated it creates the magnetic field and the reed relais makes contact and then all the magic happens
The test with a magnet was working and sofar it seems to work when it is all in place as well.
My layout is something like this. Although I need to tidy things up like, but I wait with this until it all proofs to be reliable.
Nice! A very non-intrusive way to picking up state change
I tried something similar. I used a Vision Z-wave door sensor. But the bell solenoid did not make a strong enough magnetic field to trigger it. A small āArduinoā reed switch might work better.