Advertisement
Guest User

Untitled

a guest
May 2nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 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. GPIO.output(12, False)
  47. GPIO.output(11, True)
  48. door_locked = True
  49.  
  50. #Unlock the door
  51. def unlock_door():
  52. GPIO.output(11, False)
  53. GPIO.output(12, True)
  54. door_locked = False
  55.  
  56. #analyze incoming messages
  57. def on_message(client, userdata, message):
  58. if(message.payload == "unlock"):
  59. unlock_door()
  60. if(message.payload == "lock"):
  61. lock_door()
  62.  
  63. client.on_message = on_message
  64.  
  65. #record messages
  66. def record():
  67. CHUNK = 2048
  68. FORMAT = pyaudio.paInt16
  69. CHANNELS = 2
  70. RATE = 11059
  71. RECORD_SECONDS = 3
  72. WAVE_OUTPUT_FILENAME = "output.wav"
  73.  
  74. p = pyaudio.PyAudio()
  75.  
  76. stream = p.open(format=FORMAT,
  77. channels=CHANNELS,
  78. rate=RATE,
  79. input=True,
  80. frames_per_buffer=CHUNK)
  81.  
  82. print("* recording")
  83.  
  84. frames = []
  85.  
  86. for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
  87. data = stream.read(CHUNK)
  88. frames.append(data)
  89.  
  90. print("* done recording")
  91.  
  92. stream.stop_stream()
  93. stream.close()
  94. p.terminate()
  95.  
  96. wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  97. wf.setnchannels(CHANNELS)
  98. wf.setsampwidth(p.get_sample_size(FORMAT))
  99. wf.setframerate(RATE)
  100. wf.writeframes(b''.join(frames))
  101. wf.close()
  102.  
  103. #Set the door to be locked on startup
  104. door_locked = True
  105. lock_door()
  106.  
  107. #Actual Operation
  108. while(True):
  109. if(GPIO.input(15) == True): #Initiate RFID Operation
  110. print("rfid")
  111. sleep(1)
  112. print("done")
  113.  
  114. if(GPIO.input(16) == True): #Initiate Camera Operation
  115. print("door lock status: " + str(door_locked))
  116. payload_string_one = "{\"type\": \"face\", \"command\": \""
  117. if(door_locked == True):
  118. command = "auth"
  119. print("Going to take photo to attempt match")
  120. sleep(1)
  121. print("about to take sample picture...")
  122. GPIO.output(12, True)
  123. print("3...")
  124. sleep(1)
  125. print("2...")
  126. sleep(1)
  127. print("1...")
  128. sleep(1)
  129. camera.capture('/home/pi/Pictures/subject/image.jpg')
  130. print("subject image taken")
  131. GPIO.output(12, False)
  132. image_encoding = base64.b64encode(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
  133. print("Image Encoded")
  134. image_string = str(image_encoding)
  135. payload_string = payload_string_one + command + "\", \"payload\": \"" + image_string + "\"}"
  136. publish_feedback = client.publish(topic, payload_string)
  137. #print("payload string: " + payload_string)
  138. print("publish feedback: " + str(publish_feedback))
  139. if(door_locked == False):
  140. command = "add"
  141. print("Going to take 5 images to add to database")
  142. for i in range(5):
  143. sleep(1)
  144. print("about to take sample picture...")
  145. GPIO.output(12, True)
  146. print("3...")
  147. sleep(1)
  148. print("2...")
  149. sleep(1)
  150. print("1...")
  151. sleep(1)
  152. camera.capture('/home/pi/Pictures/subject/image.jpg')
  153. print("subject image taken")
  154. GPIO.output(12, False)
  155. image_encoding = base64.encodestring(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
  156. print("Image Encoded")
  157. image_string = str(image_encoding)
  158. image_string = str(image_encoding)
  159. payload_string = payload_string_one + command + "\", \"payload\": \"" + "image_string " + str(i) + "\"}"
  160. publish_feedback = client.publish(topic, payload_string)
  161. print("publish feedback: " + str(publish_feedback))
  162. #print("payload string: " + payload_string)
  163.  
  164. if(GPIO.input(18) == True): #Initiate Voice Operation
  165. print("Now recording voice clip")
  166. record()
  167. #encoded_audio = base64.b64encode(str(open('/home/pi/Documents/Owens_Measurements/output.wav').read(),'utf-8'))
  168. #print("Audio Encoded")
  169. #audio_string = str(encoded_audio)
  170. #print(audio_string)
  171.  
  172. if(GPIO.input(13)):
  173. if (door_locked == True):
  174. print("unlocking door...")
  175. unlock_door()
  176. print("door unlocked")
  177.  
  178. if(GPIO.input(31)):
  179. if(door_locked == False):
  180. print("locking door...")
  181. lock_door()
  182. print("door locked")
  183.  
  184. #print("waiting for command")
  185. sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement