MQTT Doorbell

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 :wink:

But again thanks for sharing your initial code and making me enthusiastic to do this project as well

2 Likes

Hey Rune, great to hear you got it working.
Would like to see how you set it up, with the relay etc, as itā€™s the same I am intending to do.

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 :wink: )

Hi All,

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.

My Setup

My Arduino Code

Click to open arduino code
#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).

My Homeassistant setup

Click to open "binary sensor" setup
  • platform: mqtt
    state_topic: ā€œhomeassistant/house/doorbell/ā€
    name: Deurbel
Click to open automation
- 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 :slight_smile:

10 Likes

As a new user I could only add 1 image :frowning:

So here is the end result:

3 Likes

Hi
Can you tell me which hardware parts you used and send me links to bye them? Thanks :slight_smile:

Would love to see a clip of it in action :slight_smile:

Not a really exciting clip, but here it is :slight_smile:

4 Likes

I used:

*Arduino Wemos d1 Mini Pro: https://wiki.wemos.cc/products:d1:d1_mini_pro (https://www.tinytronics.nl/shop/nl/arduino/wemos/wemos-d1-mini-pro-esp8266-cp2104)

Good luck!

@armandjanssen I like your setup. Iā€™m new with Wemos D1 Mini and maybe you can show which pins to connect and how to upload the arduino code.

I would appreciate if you can make a tutorial vid how to set up step by step from start to end. .

There are enough resources online which describe this.

Then play around and try and find more videoā€™s and tutorials. :slight_smile:

Hereā€™s video of mine

Thereā€™s much more info in the doobly-doo of the video

6 Likes

Great! I like the way you have Google Home integrated with the doorbell.

I can also ring the door remotely. Not sure why Iā€™d wanna do that, but I can

@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).

Itā€™s an electro-mechanical relay so the relay would probably die. If you used a SSR, definitely

@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. :slight_smile:

1 Like

They donā€™t deal well with fast, repeated switching. The coil overheats and they either stick open or closed. A solid state relay is essentially a giant transistor. Hereā€™s a 5V Breakout with a SSR on it. Looks like itā€™s normally open so itā€™d work perfectly for this project.
https://www.amazon.com/Channel-G3MB-202P-Resistive-Atomic-Market/dp/B01DLDGGFU/ref=sr_1_1?ie=UTF8&qid=1496515342&sr=8-1&keywords=5V+SSR

1 Like

Little update.

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 :slight_smile:

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.

I used:

2 Likes

Nice! A very non-intrusive way to picking up state change :slight_smile:

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.