Doorbell (Python) script in wiki

Python and python framework

Moderator: leecollings

Post Reply
marmachine
Posts: 128
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Doorbell (Python) script in wiki

Post by marmachine »

Hi Folks,

I'm looking for the basics to connect my doorbell to GPIO of my RPI3.
In the WIKI i found this script, but as far as my understanding of Python goes, it seems that there is some part missing in the script!?

I'd like to work on this project and see if i can get it to work.
I see that "domoticzurl" is defined, but not used...

Code: Select all

# Settings for the domoticz server
domoticzserver="ipadres:8080"
domoticzusername = ""
domoticzpassword = ""
 
domoticzurl = 'http://'+domoticzserver+'/json.htm?type=command&param=switchlight&idx=170&switchcmd=On'

GPIO.setmode(GPIO.BCM)
 
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
while True:
	input_state = GPIO.input(18)
	if input_state == False:
		print('Button Pressed')
		time.sleep(5.0)
What appears to be missing is the part where the "domoticzurl" is called when GPIO18 goes low.

So, i guess that domoticzurl should be called somewhere in the if statement, before time.sleep(5.0) ofcourse, in order to trigger a switch in Domoticz. My question is, what will be the proper way to do this? (I'm no Python developer)

Wouldn't that be as simple as the below example?

Code: Select all

url = "http://maps.googleapis.com/maps/api/geocode/json?address=google"
response = urllib.urlopen(url)

#
# if you want to do something with Domoticz response... add below lines, otherwise leave them out or comment out with a #
#
data = json.loads(response.read())
print data
Guess that we don't really need import urllib.request and import json unless we want to do something with the response, right?

Thanks!
Marco
RayAmsterdam
Posts: 115
Joined: Sunday 11 January 2015 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Doorbell (Python) script in wiki

Post by RayAmsterdam »

I have done it this way:

Code: Select all

import RPi.GPIO as GPIO
import time
import os
import urllib


GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)

button = 24

GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
   GPIO.wait_for_edge(button, GPIO.FALLING)
   start = time.time()
   time.sleep(0.05)

   while GPIO.input(button) == GPIO.LOW:
      time.sleep(0.02)

   length = time.time() - start

   if length > 0.1:
      urllib.urlopen("http://[ip]/json.htm?type=command&param=switchlight&idx=[idx]&switchcmd=On").read()
      time.sleep(2.5)
Change button into the right GPIO (BCM) input
Change [ip] into your ip and portnumber
Change [idx] into the right IDX.
marmachine
Posts: 128
Joined: Saturday 26 March 2016 10:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Doorbell (Python) script in wiki

Post by marmachine »

RayAmsterdam wrote: Wednesday 10 May 2017 19:04 I have done it this way:

Code: Select all


...
GPIO.setmode(GPIO.BCM)
#GPIO.setwarnings(False)

button = 24

GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
   GPIO.wait_for_edge(button, GPIO.FALLING)
   start = time.time()
   time.sleep(0.05)

   while GPIO.input(button) == GPIO.LOW:
      time.sleep(0.02)

   length = time.time() - start

   if length > 0.1:
      urllib.urlopen("http://[ip]/json.htm?type=command&param=switchlight&idx=[idx]&switchcmd=On").read()
      time.sleep(2.5)
Thanks for sharing!
Are you having false alarms?

I wasn't using your script (yet) though, but going to try it!
I did set the pull-up, but that's probably not good enough.
So before changing GPIO-18 to GPIO-24, i wonder if your function resolves the false notifications i am receiving.
(could it be power dip's causing this effect?)

Marco
djdacore
Posts: 13
Joined: Monday 17 April 2017 14:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11670
Location: Appingedam
Contact:

Re: Doorbell (Python) script in wiki

Post by djdacore »

In my case it was the length of the cable from the doorbell button to the GPIO input. All I've done is putting a capacitor between the two poles.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest