MQTT Integrated Doorbell

Software

My requirements for this project were that it must:

  1. Integrate with my current doorbell with minimal changes
  2. Allow me to disable the bell when I choose
  3. Notify subscribers whenever the doorbell button was pressed

The hardware portion of this project already covered how I accomplished #1. Because the voltage path to the doorbell solenoid runs through a microcontroller-controlled relay, #2 is easily accomplished. The code subscribes itself to a MQTT topic to allow it to read messages to enable or disable the relay control. This message is published to the queue as a persistent message so it will survive restarts of the device. The code also publishes non-persistent messages to a MQTT queue when the button is pressed.

Embedded code


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

// Update these with values suitable for your network.
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";
const char* mqtt_user = "";
const char* mqtt_password = "";

const int SWITCH_INPUT = D1;
const int BELL_OUTPUT = D2;

const char* IDENTIFIER = "doorbell";
const char* STATUS_TOPIC = "doorbell/status";
const char* COMMAND_TOPIC = "doorbell/command";
const char* RING_TOPIC = "doorbell/ring";

const char* DISABLE_COMMAND = "DISABLE";
const char* ENABLE_COMMAND = "ENABLE";
const char* RING_COMMAND = "RING";

int debounceTimeout = 2000;
bool disableRing = false;
long lastTrigger;

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received: ");
  Serial.println((char *)payload);
  Serial.print("Command with length: ");
  Serial.print(length);
  Serial.print(" from MQTT broker is : [");
  Serial.print(topic);
  Serial.print("] value: ");
  String value = "";
  for(int i=0;i<length;i++) {
    value = value +  (char)payload[i];    
  }
  Serial.println(value);
  if (value != "") {
    if (strcmp(topic, COMMAND_TOPIC) == 0) {
      Serial.println(value);
      if (value == DISABLE_COMMAND) {
        Serial.println("They wanna disable");
        disableRing = true;
        client.publish(STATUS_TOPIC, DISABLE_COMMAND, true);
      } else if (value == ENABLE_COMMAND) {
        Serial.println("They wanna enable");
        disableRing = false;
        client.publish(STATUS_TOPIC, ENABLE_COMMAND, true);
      } else if (value == RING_COMMAND) {
        Serial.println("They wanna ring");
        youCanRingMyBeeeeeeeell(false);
      }
    }
  }
}//end callback

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Doorbell-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe(COMMAND_TOPIC);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()

void setup() {
  Serial.begin(115200);
  lastTrigger = millis();
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(SWITCH_INPUT, INPUT);
  pinMode(BELL_OUTPUT, OUTPUT);
  digitalWrite(BELL_OUTPUT, LOW);
}

void youCanRingMyBeeeeeeeell(bool fromButton) {
  if (fromButton) {
      client.publish(RING_TOPIC, "RING", false);
    }
  if (!disableRing) {
    digitalWrite(BELL_OUTPUT,HIGH);
    delay(400);
    digitalWrite(BELL_OUTPUT,LOW);
  }
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  int pinVal = digitalRead(SWITCH_INPUT);
  if (pinVal == HIGH) {
    if (millis() - lastTrigger >= debounceTimeout) {
      lastTrigger = millis();
      youCanRingMyBeeeeeeeell(true);
    }
    
  }
}

Home Assistant Configuration

Home Assistant needs to be configured so it can enable and disable the doorbell ring.


- platform: mqtt
  name: "Doorbell Ring"
  command_topic: "doorbell/command"
  payload_on: "ENABLE"
  payload_off: "DISABLE"
  state_topic: "doorbell/status"
  optimistic: false
  retain: false

After configuration and a restart you should see the switch in your dashboard now.

It can also be used to set up automations when the doorbell rings


alias: Doorbell Rung
initial_state: True
trigger:
  platform: mqtt
  topic: doorbell/ring
condition:
  condition: state
  entity_id: switch.doorbell_ring
  state: 'off'
action:
  service: tts.google_say
  data_template:
    entity_id: media_player.downstairs_home
    message: "There's someone at the door!"