Advertisement
Guest User

Untitled

a guest
May 29th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. enter code here
  2. import time
  3. import pycurl, json
  4. import time
  5. from datetime import datetime
  6. from StringIO import StringIO
  7. import RPi.GPIO as GPIO ## Import GPIO library
  8. GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
  9. n = 1
  10. further = 0
  11. now = datetime.now()
  12. m_hour = now.hour
  13. success_1 = 0
  14. success_2 = 0
  15. failure_1 = 0
  16. failure_2 = 0
  17. door_open = 0
  18. log_list = [success_1, success_2, failure_1, failure_2, door_open]
  19.  
  20. if m_hour > 12:
  21. m_hour = m_hour - 12
  22.  
  23. if m_hour == 0:
  24. m_hour = 12
  25.  
  26. def restart():
  27.  
  28. further = raw_input("Enter Passcode")
  29. if further == str(3161):
  30. success_1 = (" At %s:%s.%s your door was successfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
  31. GPIO.setup(7, GPIO.OUT)## Setup GPIO Pin 7 to OUT
  32. GPIO.setup(15, GPIO.OUT)
  33. for i in range(1):
  34. GPIO.output(7,True)
  35. GPIO.output(15, True)
  36. time.sleep(.3)
  37. GPIO.output(15, False)
  38. time.sleep(.3)
  39. GPIO.output(15, True)
  40. time.sleep(.3)
  41. GPIO.output(15, False)
  42. time.sleep(.3)
  43. GPIO.output(15, True)
  44. time.sleep(.3)
  45. GPIO.output(7,False)
  46. GPIO.output(15, False)
  47. time.sleep(.3)
  48.  
  49. else:
  50. failure_1 = (" At %s:%s.%s your door was unsuccessfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
  51. GPIO.setup(11, GPIO.OUT)
  52. GPIO.setup(15, GPIO.OUT)
  53. for i in range(1):
  54. GPIO.output(11,True)
  55. GPIO.output(15,True)
  56. time.sleep(1)
  57. GPIO.output(11, False)
  58. GPIO.output(15, False)
  59. further = raw_input("Enter Passcode")
  60. if further == str(3161):
  61. success_2 = (" At %s:%s.%s your door was successfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
  62. GPIO.setup(7, GPIO.OUT)
  63. GPIO.setup(15, GPIO.OUT)
  64. for i in range(1):
  65. GPIO.output(7,True)
  66. GPIO.output(15, True)
  67. time.sleep(.3)
  68. GPIO.output(15, False)
  69. time.sleep(.3)
  70. GPIO.output(15, True)
  71. time.sleep(.3)
  72. GPIO.output(15, False)
  73. time.sleep(.3)
  74. GPIO.output(15, True)
  75. time.sleep(.3)
  76. GPIO.output(7,False)
  77. GPIO.output(15, False)
  78. time.sleep(.3)
  79. else:
  80. failure_2 = (" At %s:%s.%s your door was unsuccessfully unlocked using the passcode " + str(further)) %(m_hour, now.minute, now.second)
  81. GPIO.setup(11, GPIO.OUT)
  82. GPIO.setup(15, GPIO.OUT)
  83. for i in range(1):
  84. GPIO.output(11,True)
  85. GPIO.output(15,True)
  86. time.sleep(1)
  87. GPIO.output(11, False)
  88. GPIO.output(15, False)
  89.  
  90. # set to pull-up (normally closed position) and make pin 23 bcm/ 16 board input
  91. GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  92.  
  93. #setup InstaPush variables
  94.  
  95. appID = "ID not shown for security purposes" # add your Instapush Application ID
  96.  
  97.  
  98. appSecret = "Not shown for security purposes"
  99. pushEvent = "DoorAlert"
  100. pushMessage = "Door Opened!" # add your Instapush Application Secret
  101.  
  102.  
  103. buffer = StringIO() # use this to capture the response from our push API call
  104.  
  105.  
  106. c = pycurl.Curl() # use Curl to post to the Instapush API
  107.  
  108.  
  109. c.setopt(c.URL, 'https://api.instapush.im/v1/post') # set API URL
  110.  
  111.  
  112. c.setopt(c.HTTPHEADER, ['x-instapush-appid: ' + appID,
  113. 'x-instapush-appsecret: ' + appSecret,
  114. 'Content-Type: application/json']) #setup custom headers for authentication variables and content type
  115.  
  116.  
  117.  
  118. json_fields = {} # create a dict structure for the JSON data to post
  119.  
  120.  
  121. json_fields['event']=pushEvent
  122. json_fields['trackers'] = {}
  123. json_fields['trackers']['message']=pushMessage # setup JSON values
  124.  
  125. postfields = json.dumps(json_fields) #print(json_fields)
  126.  
  127.  
  128. c.setopt(c.POSTFIELDS, postfields) # make sure to send the JSON with post
  129.  
  130.  
  131. c.setopt(c.WRITEFUNCTION, buffer.write) # set this so we can capture the resposne in our buffer
  132.  
  133. # uncomment to see the post sent
  134. #c.setopt(c.VERBOSE, True)
  135.  
  136.  
  137. door_open = (" At %s:%s.%s your door was opened without authorization " + str(further)) %(m_hour, now.minute, now.second)
  138. for i in range(2) : # setup an indefinite loop that looks for the door to be opened / closed
  139.  
  140. GPIO.wait_for_edge(16, GPIO.RISING)
  141. print("Door Opened!n")
  142.  
  143.  
  144. c.perform() # in the door is opened, send the push request
  145.  
  146.  
  147. body= buffer.getvalue() # capture the response from the server
  148.  
  149.  
  150. print(body) # print the response
  151.  
  152.  
  153. buffer.truncate(0)
  154. buffer.seek(0) # reset the buffer
  155.  
  156.  
  157. GPIO.wait_for_edge(16, GPIO.RISING)
  158. print("Door Closed!n") # print when the door in closed
  159.  
  160.  
  161. GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  162. prev_input = 0
  163.  
  164. def recheck():
  165. n = 1
  166. while n > 0:
  167. prev_input = 0
  168. input = GPIO.input(16)
  169. if (not input):
  170. n = n
  171. else:
  172. def logging(log_list):
  173. import logging
  174. if __name__ == "__main__":
  175.  
  176. logging.basicConfig(level=logging.DEBUG, filename="Log File for Door", filemode="a+",
  177. format="%(asctime)-15s %(levelname)-8s %(message)s")
  178. logging.info(log_list)
  179.  
  180. restart()
  181. log_list = [success_1, success_2, failure_1, failure_2, door_open]
  182. logging(log_list)
  183. prev_input = input
  184. prompt = raw_input("Enter reset code")
  185. if prompt == str(1802):
  186. n = n-1
  187.  
  188. else:
  189. n = n
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. recheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement