Doorbell GPIO

Post Reply
Posts: 97
Joined: Thursday 18 May 2017 8:08
Target OS: Raspberry Pi
Domoticz version: Left

Doorbell GPIO

Post by elgringo » Wednesday 05 July 2017 8:31

So in the past I created a dummy device, and a puython script whcih wait for interrupt, does some filtering and send it to domoticz if needed.

Python script:
Spoiler: show
# settings
GPIO_doorbell = 26 # BCM Pin number
domoticzidx = 48 # ID of doorbell
domoticzserver="" # IP / port domoticz
domoticzusername = "pi" # username
domoticzpassword = "pi" # password

mintimebetweenrings = 1 # in seconds (means bell is 2\X seconds blind after a press)
logrings = True # logging to stdout
minbuttonpressed = 10 # 0 = do no check, other time in milliseconds
maxbuttonpressed = 5000 # time (ms)to wait until button press is over (only used if minbuttonpressed > 0)

import RPi.GPIO as GPIO
import time
import urllib.request
import json
import traceback
import sys
from base64 import b64encode

# Setup IO
GPIO.setmode(GPIO.BCM) # BOARD does not work for pin 29
GPIO.setup(GPIO_doorbell, GPIO.IN, pull_up_down=GPIO.PUD_UP)

inlog ='%s:%s' % (domoticzusername, domoticzpassword)
base64string = b64encode(inlog.encode('utf-8')).decode('utf-8')

def microTime():
return int(round(time.time() * 1000))

def domoticzrequest (url):
request = urllib.request.Request(url)
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib.request.urlopen(request)

def microtimeToString(microtime):
return time.strftime("%d-%m-%Y %H:%M:%S", time.localtime(microtime/1000))

def reportBell():
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + str(domoticzidx) + "&switchcmd=On")
domoticzrequest("http://" + domoticzserver + "/json.htm?type=command&param=switchlight&idx=" + str(domoticzidx) + "&switchcmd=Off")

while True:
# Doorbell is active low, so a falling edge means the door has been pressed
GPIO.wait_for_edge(GPIO_doorbell, GPIO.FALLING)
timePressed = microTime()

# doorbell is pressed
if (minbuttonpressed > 0):
result = GPIO.wait_for_edge(GPIO_doorbell, GPIO.RISING, timeout=maxbuttonpressed)
if result is None:
if logrings:
print ("Doorbell pressed at "+ microtimeToString(timePressed)+" but not released after "+str(maxbuttonpressed)+" milliseconds, ignored.")
timeLoose = microTime()
pressedtime = timeLoose - timePressed

if (pressedtime > minbuttonpressed):
if logrings:
print ("Doorbell pressed at "+ microtimeToString(timePressed)+" for "+str(pressedtime)+ " milliseconds, notified Domoticz.")

if logrings:
print ("Doorbell pressed at "+ microtimeToString(timePressed)+" for "+str(pressedtime)+ " milliseconds, minimal of "+ str(minbuttonpressed) +" is required, ignored.")
if logrings:
print ("Doorbell pressed at "+ microtimeToString(timePressed)+", notified Domoticz")
except Exception as e:
print ("Error occured: "+ traceback.format_exc())
Nevert thought of it that Domoticz might support this out of the box :)
I run a v3.8015.

Is it possible to make Domoticz does the same as my script (with the filtering etc)? And interrupt based?
If so whow do i do this?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest