I have a simple Python script that is observing two GPIO pins, I've set the bouncetime parameter to 500ms, but I get multiple (almost always 2) calls to my callback, usually 3 or 4 ms apart.
This is my first Python script, so I may be missing some subtlety other than the GPIO handling.
#!/usr/bin/env python2.7
import os, datetime, atexit, time, urllib2,sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.setup(27, GPIO.IN)
def gate_moving_callback(channel):
if GPIO.input(channel):
print(str(datetime.datetime.now()), " Gate closing")
urllib2.urlopen("http://<redacted>/prog/GateClose").read()
sys.stdout.flush()
else:
print(str(datetime.datetime.now()), " Gate opening")
urllib2.urlopen("http://<redacted>/prog/GateOpen").read()
os.system('./gatepic')
sys.stdout.flush()
def shutdown_request(channel):
print "change in GPIO27: ", channel
print GPIO.input(channel)
if GPIO.input(channel):
print str(datetime.datetime.now()), " Shutdown request"
sys.stdout.flush()
os.system('/sbin/shutdown -h now')
def cleanup():
#print("Cleaning up")
GPIO.cleanup()
atexit.register(cleanup)
GPIO.add_event_detect(17, GPIO.BOTH, callback=gate_moving_callback, bouncetime=500)
GPIO.add_event_detect(27, GPIO.BOTH, callback=shutdown_request, bouncetime=500)
# Wait indefinitely...
print str(datetime.datetime.now()), " Ready!"
sys.stdout.flush()
while True:
time.sleep(1);
Here's a fairly typical example from the log:
('2014-03-02 12:26:42.196143', ' Gate opening')
('2014-03-02 12:26:42.200036', ' Gate opening')
('2014-03-02 12:27:49.946151', ' Gate closing')
Note that the second gate opening message is only 4ms after the first, even though I've specified bouncetime=500.
It always reports two opens and one close.