Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created April 13, 2015 06:04
Show Gist options
  • Save chaeplin/30686dbc4a0bd6a8a935 to your computer and use it in GitHub Desktop.
Save chaeplin/30686dbc4a0bd6a8a935 to your computer and use it in GitHub Desktop.
01.sensor.py
#!/usr/bin/env python2.7
import os, sys
import RPi.GPIO as g
from instapush.instapush import Instapush, App
import datetime
import time
#---------
insta = Instapush(user_token='token')
app = App(appid='appid', secret='secret')
#----------------
door = 27
bell = 17
#------------
g.setmode(g.BCM)
g.setwarnings(False)
g.setup(door, g.IN, pull_up_down = g.PUD_DOWN)
g.setup(bell, g.IN, pull_up_down = g.PUD_DOWN)
status = {}
status[27] = bool(g.input(door))
status[17] = bool(g.input(bell))
sensor = {}
sensor[27] = 'door'
sensor[17] = 'bell'
#-------------------
def checkpinstatus(channel):
cur_time = datetime.datetime.now()
status[channel] = not status[channel]
if channel == 17:
appmsg_str = 'ding'
if channel == 27:
if status[channel] == True:
appmsg_str = 'open'
else:
appmsg_str = 'closed'
appmsg = str(sensor[channel]) + ' : ' + appmsg_str
app.notify(event_name='Sensor', trackers={ 'sensor': appmsg })
g.add_event_detect(door, g.BOTH, callback=checkpinstatus, bouncetime=200)
g.add_event_detect(bell, g.RISING, callback=checkpinstatus, bouncetime=200)
try:
while True:
#pass
time.sleep(120)
status[27] = bool(g.input(door))
status[17] = bool(g.input(bell))
except KeyboardInterrupt:
g.cleanup()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment