Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/python
import urllib2
import time
import RPi.GPIO as GPIO
from time import sleep
PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
coupler = True
couplerLast = True
startTime = time.time()
elapsedTime = 0
sonosPath = urllib2.quote('/sayall/there is someone at the door')
sonosUrl = 'http://127.0.0.1:5005%s' % sonosPath
print sonosUrl
try:
while True:
if GPIO.input(PIN):
coupler = True
else:
coupler = False
sleep(0.1)
if coupler != couplerLast:
if coupler:
elapsedTime = time.time() - startTime
print "Elapsed time: %01.2f" % elapsedTime
else:
startTime = time.time()
print "DingDong! Sending to SONOS..."
req = urllib2.Request(sonosUrl)
try: urllib2.urlopen(req)
except urllib2.URLError as e:
print e.reason
couplerLast = coupler
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment