Advertisement
Guest User

Untitled

a guest
May 2nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. # import the libraries
  2. import os
  3. from picamera import PiCamera
  4. from time import sleep
  5. import RPi.GPIO as GPIO
  6. import base64
  7. import pyaudio
  8. import sys
  9. import wave
  10. import paho.mqtt.client as mqtt
  11. import zlib
  12.  
  13. #setup software side
  14. topic = "devices/SmartLockPi/messages/events"
  15. HostName = "Team12-IoTHub.azure-devices.net"
  16. DeviceId = "SmartLockPi"
  17. SharedAccessKey = "+MMtFeiGNDIQN1D6QV5oSYSRvZs/wpKsVPViNOTL14k="
  18.  
  19. mqtt_server = "m16.cloudmqtt.com"
  20. mqtt_username = "tdwisftd"
  21. mqtt_password = "GqEovBZL_0zD"
  22. mqtt_port = 12132
  23. ssl_port = "22132"
  24.  
  25. client = mqtt.Client("P1")
  26. client.username_pw_set("tdwisftd", "GqEovBZL_0zD")
  27. connect_feedback = client.connect(mqtt_server, mqtt_port)
  28. print("connect_feedback: " + str(connect_feedback))
  29. subscribe_feedback = client.subscribe("testing", qos = 0)
  30. print("subscribe_feedback: " + str(subscribe_feedback))
  31.  
  32. #setup the hardware
  33. camera = PiCamera()
  34. camera.resolution = (200, 200)
  35. GPIO.setmode(GPIO.BOARD)
  36. GPIO.setup(11, GPIO.OUT) #Lock Door
  37. GPIO.setup(12, GPIO.OUT) #Unlock Door
  38. GPIO.setup(15, GPIO.IN) #RFID Match
  39. GPIO.setup(16, GPIO.IN) #Face Match
  40. GPIO.setup(18, GPIO.IN) #Voice Match
  41. GPIO.setup(13, GPIO.IN) #Unlock In
  42. GPIO.setup(31, GPIO.IN) #Lock In
  43.  
  44. #Lock the door
  45. def lock_door():
  46. global door_locked
  47. GPIO.output(12, False)
  48. GPIO.output(11, True)
  49. door_locked = True
  50.  
  51. #Unlock the door
  52. def unlock_door():
  53. global door_locked
  54. GPIO.output(11, False)
  55. GPIO.output(12, True)
  56. door_locked = False
  57.  
  58. #analyze incoming messages
  59. def on_message(client, userdata, message):
  60. if(message.payload == "unlock"):
  61. unlock_door()
  62. if(message.payload == "lock"):
  63. lock_door()
  64.  
  65. client.on_message = on_message
  66.  
  67. #record messages
  68. def record():
  69. CHUNK = 2048
  70. FORMAT = pyaudio.paInt16
  71. CHANNELS = 2
  72. RATE = 11059
  73. RECORD_SECONDS = 3
  74. WAVE_OUTPUT_FILENAME = "output.wav"
  75.  
  76. p = pyaudio.PyAudio()
  77.  
  78. stream = p.open(format=FORMAT,
  79. channels=CHANNELS,
  80. rate=RATE,
  81. input=True,
  82. frames_per_buffer=CHUNK)
  83.  
  84. print("* recording")
  85.  
  86. frames = []
  87.  
  88. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  89. data = stream.read(CHUNK)
  90. frames.append(data)
  91.  
  92. print("* done recording")
  93.  
  94. stream.stop_stream()
  95. stream.close()
  96. p.terminate()
  97.  
  98. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  99. wf.setnchannels(CHANNELS)
  100. wf.setsampwidth(p.get_sample_size(FORMAT))
  101. wf.setframerate(RATE)
  102. wf.writeframes(b''.join(frames))
  103. wf.close()
  104.  
  105. #Set the door to be locked on startup
  106. door_locked = True
  107. lock_door()
  108.  
  109. #Actual Operation
  110. while(True):
  111. if(GPIO.input(15) == True): #Initiate RFID Operation
  112. print("rfid")
  113. sleep(1)
  114. print("done")
  115.  
  116. if(GPIO.input(16) == True): #Initiate Camera Operation
  117. print("door lock status: " + str(door_locked))
  118. payload_string_one = "{\"type\": \"face\", \"command\": \""
  119. if(door_locked == True):
  120. command = "auth"
  121. print("Going to take photo to attempt match")
  122. sleep(1)
  123. print("about to take sample picture...")
  124. GPIO.output(12, True)
  125. print("3...")
  126. sleep(1)
  127. print("2...")
  128. sleep(1)
  129. print("1...")
  130. sleep(1)
  131. camera.capture('/home/pi/Pictures/subject/image.jpg')
  132. print("subject image taken")
  133. GPIO.output(12, False)
  134. image_encoding = base64.b64encode(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
  135. print("Image Encoded")
  136. image_string = str(image_encoding)
  137. payload_string = payload_string_one + command + "\", \"payload\": \"" + image_string + "\"}"
  138. publish_feedback = client.publish(topic, payload_string)
  139. #print("payload string: " + payload_string)
  140. print("publish feedback: " + str(publish_feedback))
  141. if(door_locked == False):
  142. command = "add"
  143. print("Going to take 5 images to add to database")
  144. for i in range(5):
  145. sleep(1)
  146. print("about to take sample picture...")
  147. GPIO.output(12, True)
  148. print("3...")
  149. sleep(1)
  150. print("2...")
  151. sleep(1)
  152. print("1...")
  153. sleep(1)
  154. camera.capture('/home/pi/Pictures/subject/image.jpg')
  155. print("subject image taken")
  156. GPIO.output(12, False)
  157. image_encoding = base64.encodestring(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
  158. print("Image Encoded")
  159. image_string = str(image_encoding)
  160. image_string = str(image_encoding)
  161. payload_string = payload_string_one + command + "\", \"payload\": \"" + "image_string " + str(i) + "\"}"
  162. publish_feedback = client.publish(topic, payload_string)
  163. print("publish feedback: " + str(publish_feedback))
  164. #print("payload string: " + payload_string)
  165.  
  166. if(GPIO.input(18) == True): #Initiate Voice Operation
  167. print("Now recording voice clip")
  168. record()
  169. #encoded_audio = base64.b64encode(str(open('/home/pi/Documents/Owens_Measurements/output.wav').read(),'utf-8'))
  170. #print("Audio Encoded")
  171. #audio_string = str(encoded_audio)
  172. #print(audio_string)
  173.  
  174. if(GPIO.input(13)):
  175. if (door_locked == True):
  176. print("unlocking door...")
  177. unlock_door()
  178. print("door unlocked")
  179.  
  180. if(GPIO.input(31)):
  181. if(door_locked == False):
  182. print("locking door...")
  183. lock_door()
  184. print("door locked")
  185.  
  186. #print("waiting for command")
  187. sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement