#!/usr/bin/env python2.7 # Detects motion, triggers Buzzer, LED and Relay, takes picture from RPi Camera, sends as attachment via Gmail # http://www.dexterindustries.com/GrovePi/projects-for-the-raspberry-pi/whos-at-the-door/ # GrovePi + Ultrasonic Ranger + Buzzer + Switch + Relay + LED + RPi Camera # http://www.seeedstudio.com/wiki/Grove_-_Ultrasonic_Ranger # http://www.seeedstudio.com/wiki/Grove_-_Switch(P) # http://www.seeedstudio.com/wiki/Grove_-_Solid_State_Relay import grovepi import time import pycurl, json from StringIO import StringIO appID = "544d09cca4cxxxxxx40c" # add your Instapush Application Secret appSecret = "05ab5ddfb628214b34bxxxxxxxxa32" pushEvent = "Acercamiento_maximo" pushMessage = time.strftime("%d %b %Y %H:%M:%S")+"\n Una persona acaba de entrar!" print "\n\nSYSTEM WORKING\n" switch = 4 led_status = 3 relay = 2 buzzer = 5 ultraso = 7 # use this to capture the response from our push API call buffer = StringIO() # use Curl to post to the Instapush API c = pycurl.Curl() # set API URL c.setopt(c.URL, 'https://api.instapush.im/v1/post') #setup custom headers for authentication variables and content type c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID, 'x-instapush-appsecret: ' + appSecret, 'Content-Type: application/json']) # create a dict structure for the JSON data to post json_fields = {} # setup JSON values json_fields['event']=pushEvent json_fields['trackers'] = {} json_fields['trackers']['mensaje']=pushMessage #print(json_fields) postfields = json.dumps(json_fields) # make sure to send the JSON with post c.setopt(c.POSTFIELDS, postfields) # set this so we can capture the response in our buffer c.setopt(c.WRITEFUNCTION, buffer.write) # uncomment to see the post sent #c.setopt(c.VERBOSE, True) # setup an indefinite loop that looks for the door to be opened / closed while True: # in case of IO error, restart try: grovepi.pinMode(switch,"INPUT") buffer.truncate(0) # reset the buffer buffer.seek(0) grovepi.digitalWrite(led_status,0) # Turn off the LED grovepi.digitalWrite(relay,0) # Turn off the Relay while True: if grovepi.digitalRead(switch) == 1: # If the system is ON if grovepi.ultrasonicRead(ultraso) < 100: # If a person walks through the door grovepi.digitalWrite(led_status,1) # Turn on the status LED to indicate that someone has arrived grovepi.digitalWrite(relay,1) # turn on the Relay to activate an electrical device print( "\n>>> "+time.strftime("%d %b %Y %H:%M:%S")+ " <<<\n Acaba de entrar una persona!\n") time.sleep(.1) c.perform() # in the door is opened, send the push request body= buffer.getvalue() # capture the response from the server print(body) # print the response print buffer.truncate(0) # reset the buffer buffer.seek(0) grovepi.digitalWrite(led_status,0) # Turn off the LED grovepi.digitalWrite(relay,0) # Turn off the Relay except IOError: print "... Error ..." c.close()