Skip to content

Commit

Permalink
Added the sketch and updated the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tIsGoud committed Aug 15, 2017
1 parent 67598c2 commit f0ce0fe
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
68 changes: 68 additions & 0 deletions Doorbell-via-Wemos-and-optocoupler.ino
Original file line number Diff line number Diff line change
@@ -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 <Homie.h>

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();
}

91 changes: 89 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:'
```
19 changes: 19 additions & 0 deletions data/homie/config.json
Original file line number Diff line number Diff line change
@@ -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
}
}
Binary file added data/homie/ui_bundle.gz
Binary file not shown.
Binary file added img/breadboard_v2.0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/from_button_to_HA_via_MQTT.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hardware_setup_v2.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/optocoupler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/standard_doorbell_installation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f0ce0fe

Please sign in to comment.