{"id":162,"date":"2017-05-10T20:47:20","date_gmt":"2017-05-10T20:47:20","guid":{"rendered":"http:\/\/www.cribbstechnologies.com\/?page_id=162"},"modified":"2017-05-13T15:29:10","modified_gmt":"2017-05-13T15:29:10","slug":"doorbell-software","status":"publish","type":"page","link":"https:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/doorbell-software\/","title":{"rendered":"Doorbell &#8211; Software"},"content":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;Section&#8221; fullwidth=&#8221;on&#8221; specialty=&#8221;off&#8221; background_image=&#8221;http:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/05\/doorbellHeader.jpg&#8221; transparent_background=&#8221;off&#8221; allow_player_pause=&#8221;off&#8221; inner_shadow=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;on&#8221; make_fullwidth=&#8221;off&#8221; use_custom_width=&#8221;off&#8221; width_unit=&#8221;on&#8221; make_equal=&#8221;off&#8221; use_custom_gutter=&#8221;off&#8221;][et_pb_fullwidth_header admin_label=&#8221;Fullwidth Header&#8221; title=&#8221;MQTT Integrated Doorbell&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; header_fullscreen=&#8221;off&#8221; header_scroll_down=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;off&#8221; content_orientation=&#8221;center&#8221; image_orientation=&#8221;center&#8221; disabled=&#8221;off&#8221; disabled_on=&#8221;on||&#8221; custom_button_one=&#8221;off&#8221; button_one_letter_spacing=&#8221;0&#8243; button_one_use_icon=&#8221;default&#8221; button_one_icon_placement=&#8221;right&#8221; button_one_on_hover=&#8221;on&#8221; button_one_letter_spacing_hover=&#8221;0&#8243; custom_button_two=&#8221;off&#8221; button_two_letter_spacing=&#8221;0&#8243; button_two_use_icon=&#8221;default&#8221; button_two_icon_placement=&#8221;right&#8221; button_two_on_hover=&#8221;on&#8221; button_two_letter_spacing_hover=&#8221;0&#8243; custom_css_main_element=&#8221;height: 544px&#8221; custom_css_title=&#8221;margin-top: 95px;||font-size: 36px;&#8221; saved_tabs=&#8221;all&#8221; \/][\/et_pb_section][et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;3_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<h1>Software<\/h1>\n<p>My requirements for this project were that it must:<\/p>\n<ol>\n<li>Integrate with my current doorbell with minimal changes<\/li>\n<li>Allow me to disable the bell when I choose<\/li>\n<li>Notify subscribers whenever the doorbell button was pressed<\/li>\n<\/ol>\n<p>The <a href=\"http:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/doorbell-hardware\/\">hardware<\/a> 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.<\/p>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;Embedded code&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<h2>Embedded code<\/h2>\n<pre><code class=\"\" data-line=\"\">\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;PubSubClient.h&gt;\n\n\/\/ Update these with values suitable for your network.\nconst char* ssid = &quot;&quot;;\nconst char* password = &quot;&quot;;\nconst char* mqtt_server = &quot;&quot;;\nconst char* mqtt_user = &quot;&quot;;\nconst char* mqtt_password = &quot;&quot;;\n\nconst int SWITCH_INPUT = D1;\nconst int BELL_OUTPUT = D2;\n\nconst char* IDENTIFIER = &quot;doorbell&quot;;\nconst char* STATUS_TOPIC = &quot;doorbell\/status&quot;;\nconst char* COMMAND_TOPIC = &quot;doorbell\/command&quot;;\nconst char* RING_TOPIC = &quot;doorbell\/ring&quot;;\n\nconst char* DISABLE_COMMAND = &quot;DISABLE&quot;;\nconst char* ENABLE_COMMAND = &quot;ENABLE&quot;;\nconst char* RING_COMMAND = &quot;RING&quot;;\n\nint debounceTimeout = 2000;\nbool disableRing = false;\nlong lastTrigger;\n\nWiFiClient espClient;\nPubSubClient client(espClient);\n\nvoid setup_wifi() {\n   delay(100);\n  \/\/ We start by connecting to a WiFi network\n    Serial.print(&quot;Connecting to &quot;);\n    Serial.println(ssid);\n    WiFi.begin(ssid, password);\n    while (WiFi.status() != WL_CONNECTED) {\n      delay(500);\n      Serial.print(&quot;.&quot;);\n    }\n  randomSeed(micros());\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected&quot;);\n  Serial.println(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n}\n\nvoid callback(char* topic, byte* payload, unsigned int length) {\n  Serial.print(&quot;Received: &quot;);\n  Serial.println((char *)payload);\n  Serial.print(&quot;Command with length: &quot;);\n  Serial.print(length);\n  Serial.print(&quot; from MQTT broker is : [&quot;);\n  Serial.print(topic);\n  Serial.print(&quot;] value: &quot;);\n  String value = &quot;&quot;;\n  for(int i=0;i&lt;length;i++) {\n    value = value +  (char)payload[i];    \n  }\n  Serial.println(value);\n  if (value != &quot;&quot;) {\n    if (strcmp(topic, COMMAND_TOPIC) == 0) {\n      Serial.println(value);\n      if (value == DISABLE_COMMAND) {\n        Serial.println(&quot;They wanna disable&quot;);\n        disableRing = true;\n        client.publish(STATUS_TOPIC, DISABLE_COMMAND, true);\n      } else if (value == ENABLE_COMMAND) {\n        Serial.println(&quot;They wanna enable&quot;);\n        disableRing = false;\n        client.publish(STATUS_TOPIC, ENABLE_COMMAND, true);\n      } else if (value == RING_COMMAND) {\n        Serial.println(&quot;They wanna ring&quot;);\n        youCanRingMyBeeeeeeeell(false);\n      }\n    }\n  }\n}\/\/end callback\n\nvoid reconnect() {\n  \/\/ Loop until we&#039;re reconnected\n  while (!client.connected()) {\n    Serial.print(&quot;Attempting MQTT connection...&quot;);\n    \/\/ Create a random client ID\n    String clientId = &quot;ESP8266Doorbell-&quot;;\n    clientId += String(random(0xffff), HEX);\n    \/\/ Attempt to connect\n    if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {\n      Serial.println(&quot;connected&quot;);\n     \/\/once connected to MQTT broker, subscribe command if any\n      client.subscribe(COMMAND_TOPIC);\n    } else {\n      Serial.print(&quot;failed, rc=&quot;);\n      Serial.print(client.state());\n      Serial.println(&quot; try again in 5 seconds&quot;);\n      \/\/ Wait 6 seconds before retrying\n      delay(6000);\n    }\n  }\n} \/\/end reconnect()\n\nvoid setup() {\n  Serial.begin(115200);\n  lastTrigger = millis();\n  setup_wifi();\n  client.setServer(mqtt_server, 1883);\n  client.setCallback(callback);\n  pinMode(SWITCH_INPUT, INPUT);\n  pinMode(BELL_OUTPUT, OUTPUT);\n  digitalWrite(BELL_OUTPUT, LOW);\n}\n\nvoid youCanRingMyBeeeeeeeell(bool fromButton) {\n  if (fromButton) {\n      client.publish(RING_TOPIC, &quot;RING&quot;, false);\n    }\n  if (!disableRing) {\n    digitalWrite(BELL_OUTPUT,HIGH);\n    delay(400);\n    digitalWrite(BELL_OUTPUT,LOW);\n  }\n}\n\nvoid loop() {\n  if (!client.connected()) {\n    reconnect();\n  }\n  client.loop();\n  \n  int pinVal = digitalRead(SWITCH_INPUT);\n  if (pinVal == HIGH) {\n    if (millis() - lastTrigger &gt;= debounceTimeout) {\n      lastTrigger = millis();\n      youCanRingMyBeeeeeeeell(true);\n    }\n    \n  }\n}\n<\/code><\/pre>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;Text&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221;]<\/p>\n<h2>Home Assistant Configuration<\/h2>\n<p>Home Assistant needs to be configured so it can enable and disable the doorbell ring.<\/p>\n<pre><code class=\"\" data-line=\"\">\n- platform: mqtt\n  name: &quot;Doorbell Ring&quot;\n  command_topic: &quot;doorbell\/command&quot;\n  payload_on: &quot;ENABLE&quot;\n  payload_off: &quot;DISABLE&quot;\n  state_topic: &quot;doorbell\/status&quot;\n  optimistic: false\n  retain: false\n<\/code><\/pre>\n<p>After configuration and a restart you should see the switch in your dashboard now.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-175 size-full\" src=\"http:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/05\/doorbellCard.png\" alt=\"\" width=\"508\" height=\"125\" srcset=\"https:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/05\/doorbellCard.png 508w, https:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/05\/doorbellCard-300x74.png 300w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/p>\n<p>It can also be used to set up automations when the doorbell rings<\/p>\n<pre><code class=\"\" data-line=\"\">\nalias: Doorbell Rung\ninitial_state: True\ntrigger:\n  platform: mqtt\n  topic: doorbell\/ring\ncondition:\n  condition: state\n  entity_id: switch.doorbell_ring\n  state: &#039;off&#039;\naction:\n  service: tts.google_say\n  data_template:\n    entity_id: media_player.downstairs_home\n    message: &quot;There&#039;s someone at the door!&quot;\n<\/code><\/pre>\n<p>[\/et_pb_text][\/et_pb_column][et_pb_column type=&#8221;1_4&#8243;][et_pb_code admin_label=&#8221;Code&#8221;]<div id='doorbell' class='widgets_on_page wop_tiny1  wop_small1  wop_medium1  wop_large1  wop_wide1'>\n\t\t\t<ul><li id=\"nav_menu-6\" class=\"widget widget_nav_menu\"><div class=\"menu-doorbell-project-container\"><ul id=\"menu-doorbell-project\" class=\"menu\"><li id=\"menu-item-144\" class=\"menu-item menu-item-type-post_type menu-item-object-page menu-item-144\"><a href=\"https:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/\">Overview<\/a><\/li>\n<li id=\"menu-item-148\" class=\"menu-item menu-item-type-post_type menu-item-object-page menu-item-148\"><a href=\"https:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/doorbell-hardware\/\">Hardware<\/a><\/li>\n<li id=\"menu-item-174\" class=\"menu-item menu-item-type-post_type menu-item-object-page menu-item-174\"><a href=\"https:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/doorbell-software\/\">Software<\/a><\/li>\n<li id=\"menu-item-219\" class=\"menu-item menu-item-type-post_type menu-item-object-page menu-item-219\"><a href=\"https:\/\/www.cribbstechnologies.com\/index.php\/home-automation\/mqtt-integrated-doorbell\/doorbell-finishing-touches\/\">Finishing Touches<\/a><\/li>\n<\/ul><\/div><\/li><\/ul><\/div><!-- widgets_on_page -->[\/et_pb_code][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;Section&#8221; fullwidth=&#8221;on&#8221; specialty=&#8221;off&#8221; background_image=&#8221;http:\/\/www.cribbstechnologies.com\/wp-content\/uploads\/2017\/05\/doorbellHeader.jpg&#8221; transparent_background=&#8221;off&#8221; allow_player_pause=&#8221;off&#8221; inner_shadow=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;on&#8221; make_fullwidth=&#8221;off&#8221; use_custom_width=&#8221;off&#8221; width_unit=&#8221;on&#8221; make_equal=&#8221;off&#8221; use_custom_gutter=&#8221;off&#8221;][et_pb_fullwidth_header admin_label=&#8221;Fullwidth Header&#8221; title=&#8221;MQTT Integrated Doorbell&#8221; background_layout=&#8221;light&#8221; text_orientation=&#8221;left&#8221; header_fullscreen=&#8221;off&#8221; header_scroll_down=&#8221;off&#8221; parallax=&#8221;off&#8221; parallax_method=&#8221;off&#8221; content_orientation=&#8221;center&#8221; image_orientation=&#8221;center&#8221; disabled=&#8221;off&#8221; disabled_on=&#8221;on||&#8221; custom_button_one=&#8221;off&#8221; button_one_letter_spacing=&#8221;0&#8243; button_one_use_icon=&#8221;default&#8221; button_one_icon_placement=&#8221;right&#8221; button_one_on_hover=&#8221;on&#8221; button_one_letter_spacing_hover=&#8221;0&#8243; custom_button_two=&#8221;off&#8221; button_two_letter_spacing=&#8221;0&#8243; button_two_use_icon=&#8221;default&#8221; button_two_icon_placement=&#8221;right&#8221; button_two_on_hover=&#8221;on&#8221; button_two_letter_spacing_hover=&#8221;0&#8243; custom_css_main_element=&#8221;height: 544px&#8221; custom_css_title=&#8221;margin-top: 95px;||font-size: 36px;&#8221; saved_tabs=&#8221;all&#8221; \/][\/et_pb_section][et_pb_section bb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221;][et_pb_row admin_label=&#8221;row&#8221;][et_pb_column type=&#8221;3_4&#8243;][et_pb_text admin_label=&#8221;Text&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":142,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-162","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":6,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/162\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/162\/revisions\/185"}],"up":[{"embeddable":true,"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/pages\/142"}],"wp:attachment":[{"href":"https:\/\/www.cribbstechnologies.com\/index.php\/wp-json\/wp\/v2\/media?parent=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}