diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/Doorbell-via-Wemos-and-optocoupler.ino b/Doorbell-via-Wemos-and-optocoupler.ino new file mode 100644 index 0000000..8d1cbe1 --- /dev/null +++ b/Doorbell-via-Wemos-and-optocoupler.ino @@ -0,0 +1,68 @@ +/* + * Doorbell project to determine if the doorbell was pressed. + * + * The "signal" to the physical doorbell is intercepted and send to a WEMOS D1 mini on pin D3 (GPIO0). + * + * In this version an optocoupler is used to decouple the Wemos from the doorbell. + * This idea was originally published in the German c't magazine [17/2017]. + * + * The Wemos publishes a message to an MQTT instance. + * + * Wi-Fi and MQTT configuration are set in the Homie config file (data/homie/config.json). + * + * Used libraries: + * - Homie 2.0.0 + * - ArduinoJSon 5.11.1 + * + * Parameters: + * - readDelay (milliseconds), sets the time between reading a value + * - buttonPressDelay (milliseconds) , is used to prevent multiple reads (very pragmatic but works fine) + * + * Author: Albert W. Alberts + * Date: 14-08-2017 + * + */ +#include + +int readDelay = 50; // 50 is the max, higher makes it unreliable +int buttonPressDelay = 3000; // 3000 = 3 seconds +int inputPin = D3; // D3 or 0 (for GPIO0) +int intputValue = 0; + +HomieNode doorbellNode("button", "button"); + +void loopHandler() { + // Read the input value on the input pin + inputValue = digitalRead(inputPin); + + // Turn on the internal led, internal led is active low + digitalWrite(BUILTIN_LED, inputValue); + + if (inputValue == 0) { + Serial.println("Doorbell pressed ..."); + doorbellNode.setProperty("pressed").send("true"); + delay(buttonPressDelay); + } + delay(readDelay); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + + // Pull-up to prevent "floating" input + pinMode(inputPin, INPUT_PULLUP); + pinMode(BUILTIN_LED, OUTPUT); + + Homie_setBrand("wemos"); + Homie_setFirmware("Doorbell-MQTT", "2.0.0"); + + Homie.setLoopFunction(loopHandler); + + Homie.setup(); +} + +void loop() { + Homie.loop(); +} + diff --git a/README.md b/README.md index d501992..936d424 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,89 @@ -# Doorbell-via-Wemos-and-optocoupler -Digital version of my "analog" doorbell project to determine if the doorbell was pressed. +# Doorbell via Wemos and optocoupler + +This is the digital version or v2.0 of my [initial doorbell project](https://github.com/tIsGoud/Doorbell-via-Wemos/). +The main goal is to determine if the doorbell was pressed and perform some actions based on it. + +I could have made a version 2.0 of the initial setup but that did not feel right. The hardware and the software are very different. + +The setup for this version was inspired by an article in the German c't magazine (2017/17). +In the German magazine an optocoupler is used to separate the doorbell from a Raspberry Pi. + +In version 1 I used the analog port(A0) on the Wemos to read out the voltage, this version receives a signal from the optocoupler that is read on a digital port. +A nice galvanic separation between the two circuits, thereby protecting the Wemos. + +As in the previous version the main requirements is that I do not want to mess with the existing doorbell installation or have a dependency on extra components. + +The standard Dutch doorbell installation: +![Dutch standard doorbell installation](img/standard_doorbell_installation.png) + +Once the doorbell-button is pressed, the "signal" is send to the Wemos. The Wemos is connected to MQTT via the local Wi-Fi. +Home Assistant picks up the signal from MQTT. From there it's up to your imagination what you want to do with the input. +For now I send a message to a Slack-bot to notify me. + +![From button press to HA via MQTT](img/from_button_to_HA_via_MQTT.png) + +## Hardware v2.0 + +For the initial setup [Rudi](http://www.rudiniemeijer.nl) a colleague helped me out with the design and some components he had lying around: +- Resistor, 330Ω +- Diode, 1N4148 +- Optocoupler, PC817 + +The resistor lowers the voltage, the diode "flattens" the signal and the optocoupler sends the signal to the Wemos on port(D3/GPIO0). +![Hardware setup v2.0](img/hardware_setup_v2.0.png) + +The value read on port D3 is by default 1, pressing the doorbell drops the signal to 0. Not what I initially expected but this is how it works. + +Another learning point was how to connect the optocoupler. On the surface there is a "dot", this indicates port 1, the anode that connects to the doorbell. The emitter and collector are connected to the Wemos. +![Optocoupler](img/optocoupler.png) + +On the breadbord the setup is nice and simple. +![Breadbord 2.0](img/breadbord_v2.0.jpg) + +## Software v2.0 + +On the Wemos I use [Homie for ESP8266](https://github.com/marvinroger/homie-esp8266) to: +- Connect and stay connected to the Wi-Fi +- Connect and stay connected to MQTT +- Send data to MQTT + +The [Wemos D1 mini BasicIO example](https://github.com/wemos/D1_mini_Examples/tree/master/examples/01.Basics/BasicIO) shows how to read the input pin (D3). +In the example the input pin is set to INPUT, this could lead to a "floating" signal. To prevent that we set it to INPUT_PULLUP. + +Pressing the doorbell button lights up the Wemos onboard led. A nice visual feedback, convenient during development. + +The input pin D3 is read in a loop with a delay defined in the variable "readDelay". Otherwise it would be polling constantly. The readDelay with a value of 50 works fine, with higher values and pressing the button multiple times or constantly somehow destabilizes the system. Annoying, although only temporary. + +To prevent multiple triggers when the button is either pressed very long or multiple times I use the "buttonPressDelay" variable. Setting "buttonPressDelay" to a value of 3 to 5 seconds works fine and results in just one trigger. + +The readDelay and buttonPressDelay values are set in milliseconds. + +## Home Assistant configuration + +I use [Home Assistant](https://home-assistant.io/) for home automation. +Below are the parts of the Home Assistent configuration that handle the MQTT message and send me a notification via Slack: + +``` +automation: +- alias: "Doorbell pressed" + hide_entity: True + trigger: + platform: mqtt + topic: wemos/doorbell/button/pressed + payload: 'true' + action: + service: notify.Slack + data: + message: "Someone at the door!" + title: "Doorbell says ..." + ``` + + ``` + notify: + - name: Slack + platform: slack + api_key: !secret Slack_API-key + username: !secret Slack_Username + default_channel: '#hass' + icon: ':house:' +``` diff --git a/data/homie/config.json b/data/homie/config.json new file mode 100644 index 0000000..1b783d5 --- /dev/null +++ b/data/homie/config.json @@ -0,0 +1,19 @@ +{ + "name": "WeMos Doorbell", + "device_id": "doorbell", + "wifi": { + "ssid": "[your Wi-Fi SSID]", + "password": "[your Wi-Fi password]" + }, + "mqtt": { + "host": "[your MQTT host IP-address]", + "port": 1883, + "base_topic": "wemos/", + "auth": true, + "username": "[your MQTT username]", + "password": "[your MQTT password]" + }, + "ota": { + "enabled": true + } +} diff --git a/data/homie/ui_bundle.gz b/data/homie/ui_bundle.gz new file mode 100644 index 0000000..04d7340 Binary files /dev/null and b/data/homie/ui_bundle.gz differ diff --git a/img/breadboard_v2.0.jpg b/img/breadboard_v2.0.jpg new file mode 100644 index 0000000..492cb04 Binary files /dev/null and b/img/breadboard_v2.0.jpg differ diff --git a/img/from_button_to_HA_via_MQTT.png b/img/from_button_to_HA_via_MQTT.png new file mode 100644 index 0000000..900f919 Binary files /dev/null and b/img/from_button_to_HA_via_MQTT.png differ diff --git a/img/hardware_setup_v2.0.png b/img/hardware_setup_v2.0.png new file mode 100644 index 0000000..c6c336e Binary files /dev/null and b/img/hardware_setup_v2.0.png differ diff --git a/img/optocoupler.png b/img/optocoupler.png new file mode 100644 index 0000000..664ecbb Binary files /dev/null and b/img/optocoupler.png differ diff --git a/img/standard_doorbell_installation.png b/img/standard_doorbell_installation.png new file mode 100644 index 0000000..11901c2 Binary files /dev/null and b/img/standard_doorbell_installation.png differ