MQTT Doorbell

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