Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enter code here
- import time
- import pycurl, json
- import time
- from datetime import datetime
- from StringIO import StringIO
- import RPi.GPIO as GPIO ## Import GPIO library
- GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
- n = 1
- further = 0
- now = datetime.now()
- m_hour = now.hour
- success_1 = 0
- success_2 = 0
- failure_1 = 0
- failure_2 = 0
- door_open = 0
- log_list = [success_1, success_2, failure_1, failure_2, door_open]
- if m_hour > 12:
- m_hour = m_hour - 12
- if m_hour == 0:
- m_hour = 12
- def restart():
- further = raw_input("Enter Passcode")
- if further == str(3161):
- success_1 = (" At %s:%s.%s your door was successfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
- GPIO.setup(7, GPIO.OUT)## Setup GPIO Pin 7 to OUT
- GPIO.setup(15, GPIO.OUT)
- for i in range(1):
- GPIO.output(7,True)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(15, False)
- time.sleep(.3)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(15, False)
- time.sleep(.3)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(7,False)
- GPIO.output(15, False)
- time.sleep(.3)
- else:
- failure_1 = (" At %s:%s.%s your door was unsuccessfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
- GPIO.setup(11, GPIO.OUT)
- GPIO.setup(15, GPIO.OUT)
- for i in range(1):
- GPIO.output(11,True)
- GPIO.output(15,True)
- time.sleep(1)
- GPIO.output(11, False)
- GPIO.output(15, False)
- further = raw_input("Enter Passcode")
- if further == str(3161):
- success_2 = (" At %s:%s.%s your door was successfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
- GPIO.setup(7, GPIO.OUT)
- GPIO.setup(15, GPIO.OUT)
- for i in range(1):
- GPIO.output(7,True)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(15, False)
- time.sleep(.3)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(15, False)
- time.sleep(.3)
- GPIO.output(15, True)
- time.sleep(.3)
- GPIO.output(7,False)
- GPIO.output(15, False)
- time.sleep(.3)
- else:
- failure_2 = (" At %s:%s.%s your door was unsuccessfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
- GPIO.setup(11, GPIO.OUT)
- GPIO.setup(15, GPIO.OUT)
- for i in range(1):
- GPIO.output(11,True)
- GPIO.output(15,True)
- time.sleep(1)
- GPIO.output(11, False)
- GPIO.output(15, False)
- # set to pull-up (normally closed position) and make pin 23 bcm/ 16 board input
- GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- #setup InstaPush variables
- appID = "ID not shown for security purposes" # add your Instapush Application ID
- appSecret = "Not shown for security purposes"
- pushEvent = "DoorAlert"
- pushMessage = "Door Opened!" # add your Instapush Application Secret
- buffer = StringIO() # use this to capture the response from our push API call
- c = pycurl.Curl() # use Curl to post to the Instapush API
- c.setopt(c.URL, 'https://api.instapush.im/v1/post') # set API URL
- c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
- 'x-instapush-appsecret: ' + appSecret,
- 'Content-Type: application/json']) #setup custom headers for authentication variables and content type
- json_fields = {} # create a dict structure for the JSON data to post
- json_fields['event']=pushEvent
- json_fields['trackers'] = {}
- json_fields['trackers']['message']=pushMessage # setup JSON values
- postfields = json.dumps(json_fields) #print(json_fields)
- c.setopt(c.POSTFIELDS, postfields) # make sure to send the JSON with post
- c.setopt(c.WRITEFUNCTION, buffer.write) # set this so we can capture the resposne in our buffer
- # uncomment to see the post sent
- #c.setopt(c.VERBOSE, True)
- door_open = (" At %s:%s.%s your door was opened without authorization " + str(further)) %(m_hour, now.minute, now.second)
- for i in range(2) : # setup an indefinite loop that looks for the door to be opened / closed
- GPIO.wait_for_edge(16, GPIO.RISING)
- print("Door Opened!n")
- 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
- buffer.truncate(0)
- buffer.seek(0) # reset the buffer
- GPIO.wait_for_edge(16, GPIO.RISING)
- print("Door Closed!n") # print when the door in closed
- GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- prev_input = 0
- def recheck():
- n = 1
- while n > 0:
- prev_input = 0
- input = GPIO.input(16)
- if (not input):
- n = n
- else:
- def logging(log_list):
- import logging
- if __name__ == "__main__":
- logging.basicConfig(level=logging.DEBUG, filename="Log File for Door", filemode="a+",
- format="%(asctime)-15s %(levelname)-8s %(message)s")
- logging.info(log_list)
- restart()
- log_list = [success_1, success_2, failure_1, failure_2, door_open]
- logging(log_list)
- prev_input = input
- prompt = raw_input("Enter reset code")
- if prompt == str(1802):
- n = n-1
- else:
- n = n
- recheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement