Skip to content

Instantly share code, notes, and snippets.

@victorchee
Last active August 29, 2015 14:13
Show Gist options
  • Save victorchee/654fc92bad3cdebe6eb1 to your computer and use it in GitHub Desktop.
Save victorchee/654fc92bad3cdebe6eb1 to your computer and use it in GitHub Desktop.
Raspberry Door
import RPi.GPIO as GPIO
import pycurl, json
from StringIO import StringIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(25, GPIO.OUT)
appID = "54bc5b49a4c48a3d5bf22c4c"
appSecret = "a55bc3eb578bcd5d9065d24f83fa3219"
pushEvent = "RaspberryDoor"
pushMessage = "Door Opened!"
# use StringIO to capture the response from out push API call
buffer = StringIO()
curl = pycurl.Curl()
curl.setopt(curl.URL, 'https://api.instapush.im/post')
curl.setopt(curl.HTTPHEADER, ['x-instapush-appid:'+appID,
'x-instapush-appsecret:'+appSecret,
'Content-Type: application/json'])
json_fields = {}
json_fields['event'] = pushEvent
json_fields['trackers'] = {}
json_fields['trackers']['message'] = pushMessage
postfields = json.dumps(json_fields)
curl.setopt(curl.POSTFIELDS, postfields)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
while True:
GPIO.wait_for_edge(23, GPIO.RISING)
print("Door Opened!")
GPIO.output(25, GPIO.HIGH)
# send the push request
curl.perform()
# capture the response from the server
body = buffer.getvalue()
print(body)
# reset the buffer
buffer.truncate(0)
buffer.seek(0)
GPIO.wait_for_edge(23, GPIO.FALLING)
print("Door Closed!")
GPIO.output(25, GPIO.LOW)
else:
curl.close()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment