Page 1 of 1

Doorbell on GPIO script

Posted: Thursday 20 July 2017 15:54
by marmachine
Hi Folks,

I've been working on a doorbell script, this should trigger when the specific GPIO pin goes low.

Code: Select all

#!/usr/bin/python

# Doorbell py with read contact uitlezen,
# GPIO 18 to Ground when button is pushed
# Release 1
# Author J. Jeurissen / M. van Wijngaarden
# Copyright (c) 2015  J. Jeurissen  / (c) 2017 M. van Wijngaarden
# Python 3

import urllib
#import urllib.request
import json
import RPi.GPIO as GPIO
import time

# Settings for the domoticz server
domoticzserver="192.168.2.100:8080"
domoticzusername = ""
domoticzpassword = ""

domoticzurl = 'http://'+domoticzserver+'/json.htm?type=command&param=switchlight&idx=189&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')
                response = urllib.urlopen(domoticzurl)
                data = json.loads(response.read())
                print data
                time.sleep(5.0)
It seems to work when i start the script manually, however it won't start from CRONTAB.
Here's my crontab:

Code: Select all

pi@raspberrypi:~$ crontab -e

@reboot /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py >/home/pi/domoticz/scripts/python/logs/cronlog 2>&1
EDIT: found the solution, using the following crontab entry, now works for me after reboot:

Code: Select all

@reboot /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py

Here's how i start the script manually and the result when the doorbell (button pressed) rings.
Though, i have to say that i don't understand the line below "button pressed"... does that indicate an error?

Code: Select all

pi@raspberrypi:~$ python /home/pi/domoticz/scripts/python/doorbellgpio.py
Button Pressed
{u'status': u'ERROR', u'message': u'WRONG CODE', u'title': u'SwitchLight'}
As you can see i am no expert, but maybe you can help me figure out what's wrong here?
Thanks in advance.

Marco

Re: Doorbell on GPIO script

Posted: Thursday 20 July 2017 16:20
by D'rMorris
It seems that your Domoticz URL is not complete. If you look at the wiki, the syntax to switch a light / switch on is:

/json.htm?type=command&param=switchlight&idx=xxx&switchcmd=On

You only have:
/json.htm?type=command&param=switchligh$.
If you update your URL it should work.

Re: Doorbell on GPIO script

Posted: Monday 24 July 2017 12:17
by marmachine
D'rMorris wrote:It seems that your Domoticz URL is not complete. If you look at the wiki, the syntax to switch a light / switch on is:

/json.htm?type=command&param=switchlight&idx=xxx&switchcmd=On

You only have:
/json.htm?type=command&param=switchligh$.
If you update your URL it should work.
Thanks for your reply!
Unfortunately that is not entirely correct, it actually appeared to be correct on my Pi so i guess this was a typo (error with copy-paste).
I've updated my original post (script).

The strange thing is that it works when i manually start the script.
So i wonder why it won't start on reboot.

Re: Doorbell on GPIO script

Posted: Monday 24 July 2017 13:24
by D'rMorris
How do you run it when you run it manually? Do you start it with user pi (or any other user which is not root) logged in? As far as I know, but I might be wrong, is that GPIO needs root. Now, you run it as root when you reboot:

@reboot sudo /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py >$

but perhaps you could try to add the line above in the crontab for user root? first do a su root when logged in as user pi and then crontab -e and add the line above. That way, you can at least check if it's a authorization issue or not.

Re: Doorbell on GPIO script

Posted: Tuesday 25 July 2017 9:24
by marmachine
D'rMorris wrote:How do you run it when you run it manually? Do you start it with user pi (or any other user which is not root) logged in? As far as I know, but I might be wrong, is that GPIO needs root. Now, you run it as root when you reboot:

@reboot sudo /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py >$

but perhaps you could try to add the line above in the crontab for user root? first do a su root when logged in as user pi and then crontab -e and add the line above. That way, you can at least check if it's a authorization issue or not.
I run it manually, logged in as 'pi', issuing the following command:
python /home/pi/domoticz/scripts/python/doorbellgpio.py

This results in a triggered virtual switch which is representing the doorbell. So i assume it should work, even without root.
So, i just figured maybe try an entry without logging, so i've altered my crontab entry to the below (still logged in as 'pi'):

@reboot /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py

Guess what, after reboot, the above is working!
For some reason the previous crontab entry wasn't correct!? Well, i don't care for logging in this particular case, so i'm happy!

Thanks all!

Re: Doorbell on GPIO script

Posted: Tuesday 25 July 2017 9:47
by D'rMorris
I don't know logging with >$. In my own cron I use >/dev/null 2>&1. Does the same as using no logging I guess, because the log is directed to /dev/null.

Re: Doorbell on GPIO script

Posted: Tuesday 25 July 2017 11:02
by marmachine
D'rMorris wrote:I don't know logging with >$. In my own cron I use >/dev/null 2>&1. Does the same as using no logging I guess, because the log is directed to /dev/null.
Some part of my original code was lost there somehow... below is my original entry (which doesn't work!):
@reboot /usr/bin/python /home/pi/domoticz/scripts/python/doorbellgpio.py >/home/pi/domoticz/scripts/python/logs/cronlog 2>&1