Skip to content

Instantly share code, notes, and snippets.

@tdlm
Created March 29, 2016 23:49
Show Gist options
  • Save tdlm/c1221b1ab3228636e5fc764ed65a3422 to your computer and use it in GitHub Desktop.
Save tdlm/c1221b1ab3228636e5fc764ed65a3422 to your computer and use it in GitHub Desktop.
#!/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