DIY Smart Doorbell with Android notifications

Hmmm…when I use a simple Python script I took from a tutorial to detect the button being pressed it works.

But mqtt-gpio does nothing when I press the button. Nothing happens in MQTTBox or on the shell…only the initial settings are sent upon startup. So the MQTT connection itself should be allright.

I found out that it is very simple to write a small python program publishing a key press on mqtt. Will give it a try tomorrow as I am stuck with pi-mqtt-gpio.

Very strange. I have very good experience with pi-mqtt-gpio once I figured out pull_up/pull_down.

Don’t forget to make sure that your program starts automatically after a reboot if you should experience a power failure.

I tried everything in the config with pullup, pulldown and the payloads. No reaction at all.

I will try it like described here:

https://www.raspberrypi.org/forums/viewtopic.php?t=172748

However the next days I will not be able to work on it…either I have success today or I will have a hard time waiting until I can continue :smiley:

Ok my button is connected to Pin10 and 3,3V now.

I installed paho-mqtt and wrote (that means: stole together :smiley:) a little script that publishes my buttonpress to mqtt topic doorbell:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt

def button_callback(channel):
    publish.single("doorbell/", payload=None, qos=0, retain=False, hostname="<mqtt host>", port=1883, client_id="", keepalive=60, will=None, auth={'username':"<mqttuser>",'password':"<password>"}, tls=None, protocol=mqtt.MQTTv311, t$
    print("Button was pushed!")

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)

GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge

message = input("Press enter to quit\n\n") # Run until someone presses enter

GPIO.cleanup() # Clean up

Script parts taken from here https://raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio/ and here https://pypi.org/project/paho-mqtt/ and here https://www.raspberrypi.org/forums/viewtopic.php?t=172748

Still have to check on payload etc of course… :slight_smile:

This is how far I got:

Home Assistant notices the button press and input_boolean.doorbel is turned on by the automation. On the doorpi card I get the option to accept and reject the call, and when I click Accept that changes to end call.

However I don’t get sound in any direction.

It is working on https://myhost:9090/stream/webrtc but not in Home Assistant. What URL do you enter in the doorpi card? I entered https://myhost:9090 without any path - is that correct?

And where should I put the signalling.js and main.js? Also into /www/doorpi? And should I add them under resources: in the ui-lovelace.yaml? (edit: from checking the doorpi-card.js I assume they just have to reside in the same directory as doorpi-card.js)

And is it ok to remove the configuration for the server on port 8888?

So many questions, sorry :smiley:

edit:

I FINALLY GOT IT WORKING!!!

So stupid! It was not working as I had no SSL active on my Home Assistant and therefore were not able to allow access to the audio devices. Would be helpful if Chrome or Firefox had told me about blocking the access :smiley: Quickly created SSL certificates and now it is working.

Have to finetune my buttonpress script now :slight_smile:

edit:

Instead of putting more work in my button press mqtt script, I used the one I found here, with slight adjustments:

:slight_smile:

My version:
(added possibility to enter mqtt credentials and changed journal handler to work with custom systemd installed through “pip3 install systemd”. Changed GPIO mode to BOARD.

#!/usr/bin/python3

import RPi.GPIO as GPIO
import logging
import paho.mqtt.publish as publish
from time import sleep
from systemd import journal

log = logging.getLogger('mqtt_alarm')
log.addHandler(journal.JournaldLogHandler())
log.setLevel(logging.INFO)

MQTT_HOST = "<mqtthost>"
MQTT_TOPIC_PREFIX = "doorbell/input/"
MQTT_USER = "<username>"
MQTT_PASS = "<password>"
MQTT_CLIENT_ID = ""
MQTT_PAYLOADS = {
  0: "Off",
  1: "On",
}

# Dictionary of GPIO PIN to mqtt topic
PIN_MAP = {
  10: "button_1",
}

GPIO.setmode(GPIO.BOARD)

def state_change_hadler(channel):
  state = GPIO.input(channel)

  if state:
    log.info("Rising edge detected on {}".format(channel))
  else:
    log.info("Falling edge detected on {}".format(channel))

  publish_event(channel, state)

def publish_event(pin, state):
  topic = MQTT_TOPIC_PREFIX + PIN_MAP[pin]
  payload = MQTT_PAYLOADS[state]

  publish.single(topic, payload, hostname=MQTT_HOST, auth={'username':MQTT_USER,'password':MQTT_PASS}, retain=True, qos=2)
  log.info("Published event, topic={}, payload={}, hostname={}".format(topic, payload, MQTT_HOST))

for pin, name in PIN_MAP.items():
  GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.add_event_detect(pin, GPIO.BOTH, callback=state_change_hadler, bouncetime=100)

  state = GPIO.input(pin)

  log.info("Mapped pin {:0>2d} to {}".format(pin, name))
  log.info("... state {}".format(state))

  publish_event(pin, state)
try:
  while True:
    sleep(60)
except KeyboardInterrupt:
  log.info("Stopping...")
finally:
  GPIO.cleanup()
2 Likes

Glad you got it working!!! I hope you can enjoy the calling now…

I’ll put an extra warning in the instructable about https…

Cheers,
R

1 Like

Yeah.

Now I have to put some power to the door for the Pi but I found an unused power cable there already that I might be able to put back to life :slight_smile:

When you use Traefik to make that all available over the Internet, that means you are forwarding a Pi Port through the router , right?

Thats a thing a totally want to avoid - to much stress tracking security for device and software… because IF somebody breaks in that network, he might even be able to open my door (didn’t implement something for that yet and still have to walk there on my own, but that might change, too).

I think I will stay with VPN for that.

Would be great of course if there was a way to make it available through nabu casa somehow… maybe through the ingress feature? Maybe by creating an own small hass.io addon? Will do some research if that might be possible…

Yes. But it runs on a Docker on my NAS.

I see. Naaa…since my QNAP got infected with a Malware and regarding the extremely poor information politic for that issue from QNAP I will never again expose my NAS to the Internet :wink:

Hi Ronald,
I am sorry to trouble you as this a HA issue, but I am trying to build this project and can’t get SSL working for UV4l. I have tried everything I can think of. My config works fine until I try to secure it. I have generated a self certificate, but when I make the following changes I can’t access my site

HTTPS options:

server-option = --use-ssl=yes
server-option = --ssl-private-key-file=/etc/ssl/private/selfsign.key
server-option = --ssl-certificate-file=/etc/ssl/private/selfsign.crt
my key is as below
pi@raspberrypi:/etc/uv4l $ ls
privkey.key selfsign.key uv4l-raspicam.conf.save uv4l-uvc.conf
selfsign.crt uv4l-raspicam.conf uv4l-raspidisp.conf

I get the following in chrome.
image
any help or suggestions wood be appreciated.

sorry realsied I posted the wrong settings. should be
image

Hi Mark,

I use traefik for all ssl handling. This way, I can also see from all over the world who is at my front door.

P.s.: it’s not only a HA issue. If you want everything accessible from the outside, your HA instane and doorbell must be available using https.

Cheers,
R

Is uv4l starting correct? What is

tail -f /var/log/syslog

saying? Which raspian release are you running?

Hi ,I think this is an amazing project. I have completed step 1 of the instructables and have video and audio working. I am a little lost at step 3. Do I put the 3 files into the library www/doorpi/, being doorpi-card.js, signalling.js and doorpi-camera-view.js?
How do I use the doorpi.yaml? Do I copy it to my Configuration.yaml? Do I need to create a LoveLace card, or does the broswer mode do that. Do I need to change any names in the above files?
Sorry for all the dumb questions, but as you can see I am not very good at this stuff.
Thanks
Mark

I finally got this working saw your other post in instructables this is what you are after mabye

type: 'custom:doorpi-card'
camera_entity: camera.doorbell_camera
doorpi:
  icon: 'mdi:door'       
  title: DoorPi            
  url: 'https://youraddress:9090'          

Hi @Mark53,

sorry for the late reply - I’ve missed your question.

  - cards:
      - camera_entity: camera.doorbell_camera
        doorpi:
          url: 'your url'
        title: null
        type: 'custom:doorpi-card'
    icon: 'mdi:door'
    title: DoorPi

If you want to have the doorpi available when you are not home, you will need to make sure that ‘your url’ is available external (so not a 192.169.***) url.

Cheers!

Thank you both so much for your help on this amazing project. Now onto the next step.

Hi Ronald
Thanks for your hard work on this love it.I have everything up and running on a Pi 2 only issue i have is with certificates eg NET::ERR_CERT_AUTHORITY_INVALID
How did you get around this?. Do you use your Nas to reverse proxy and lets encrypt?
also can you use this from your phone IOS or will it only work through chome on a computer
thanks

Indeed, I use traefik on my bas to make it available remote. It also works on my Android mobile phone.

Only thing: the HTML 5 notifications come in a bit too late…