Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import the libraries
- import os
- from picamera import PiCamera
- from time import sleep
- import RPi.GPIO as GPIO
- import base64
- import pyaudio
- import sys
- import wave
- import paho.mqtt.client as mqtt
- import zlib
- #setup software side
- topic = "devices/SmartLockPi/messages/events"
- HostName = "Team12-IoTHub.azure-devices.net"
- DeviceId = "SmartLockPi"
- SharedAccessKey = "+MMtFeiGNDIQN1D6QV5oSYSRvZs/wpKsVPViNOTL14k="
- mqtt_server = "m16.cloudmqtt.com"
- mqtt_username = "tdwisftd"
- mqtt_password = "GqEovBZL_0zD"
- mqtt_port = 12132
- ssl_port = "22132"
- client = mqtt.Client("P1")
- client.username_pw_set("tdwisftd", "GqEovBZL_0zD")
- connect_feedback = client.connect(mqtt_server, mqtt_port)
- print("connect_feedback: " + str(connect_feedback))
- subscribe_feedback = client.subscribe("testing", qos = 0)
- print("subscribe_feedback: " + str(subscribe_feedback))
- #setup the hardware
- camera = PiCamera()
- camera.resolution = (200, 200)
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(11, GPIO.OUT) #Lock Door
- GPIO.setup(12, GPIO.OUT) #Unlock Door
- GPIO.setup(15, GPIO.IN) #RFID Match
- GPIO.setup(16, GPIO.IN) #Face Match
- GPIO.setup(18, GPIO.IN) #Voice Match
- GPIO.setup(13, GPIO.IN) #Unlock In
- GPIO.setup(31, GPIO.IN) #Lock In
- #Lock the door
- def lock_door():
- global door_locked
- GPIO.output(12, False)
- GPIO.output(11, True)
- door_locked = True
- #Unlock the door
- def unlock_door():
- global door_locked
- GPIO.output(11, False)
- GPIO.output(12, True)
- door_locked = False
- #analyze incoming messages
- def on_message(client, userdata, message):
- if(message.payload == "unlock"):
- unlock_door()
- if(message.payload == "lock"):
- lock_door()
- client.on_message = on_message
- #record messages
- def record():
- CHUNK = 2048
- FORMAT = pyaudio.paInt16
- CHANNELS = 2
- RATE = 11059
- RECORD_SECONDS = 3
- WAVE_OUTPUT_FILENAME = "output.wav"
- p = pyaudio.PyAudio()
- stream = p.open(format=FORMAT,
- channels=CHANNELS,
- rate=RATE,
- input=True,
- frames_per_buffer=CHUNK)
- print("* recording")
- frames = []
- for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
- data = stream.read(CHUNK)
- frames.append(data)
- print("* done recording")
- stream.stop_stream()
- stream.close()
- p.terminate()
- wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
- wf.setnchannels(CHANNELS)
- wf.setsampwidth(p.get_sample_size(FORMAT))
- wf.setframerate(RATE)
- wf.writeframes(b''.join(frames))
- wf.close()
- #Set the door to be locked on startup
- door_locked = True
- lock_door()
- #Actual Operation
- while(True):
- if(GPIO.input(15) == True): #Initiate RFID Operation
- print("rfid")
- sleep(1)
- print("done")
- if(GPIO.input(16) == True): #Initiate Camera Operation
- print("door lock status: " + str(door_locked))
- payload_string_one = "{\"type\": \"face\", \"command\": \""
- if(door_locked == True):
- command = "auth"
- print("Going to take photo to attempt match")
- sleep(1)
- print("about to take sample picture...")
- GPIO.output(12, True)
- print("3...")
- sleep(1)
- print("2...")
- sleep(1)
- print("1...")
- sleep(1)
- camera.capture('/home/pi/Pictures/subject/image.jpg')
- print("subject image taken")
- GPIO.output(12, False)
- image_encoding = base64.b64encode(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
- print("Image Encoded")
- image_string = str(image_encoding)
- payload_string = payload_string_one + command + "\", \"payload\": \"" + image_string + "\"}"
- publish_feedback = client.publish(topic, payload_string)
- #print("payload string: " + payload_string)
- print("publish feedback: " + str(publish_feedback))
- if(door_locked == False):
- command = "add"
- print("Going to take 5 images to add to database")
- for i in range(5):
- sleep(1)
- print("about to take sample picture...")
- GPIO.output(12, True)
- print("3...")
- sleep(1)
- print("2...")
- sleep(1)
- print("1...")
- sleep(1)
- camera.capture('/home/pi/Pictures/subject/image.jpg')
- print("subject image taken")
- GPIO.output(12, False)
- image_encoding = base64.encodestring(open('/home/pi/Pictures/subject/image.jpg', "rb").read())
- print("Image Encoded")
- image_string = str(image_encoding)
- image_string = str(image_encoding)
- payload_string = payload_string_one + command + "\", \"payload\": \"" + "image_string " + str(i) + "\"}"
- publish_feedback = client.publish(topic, payload_string)
- print("publish feedback: " + str(publish_feedback))
- #print("payload string: " + payload_string)
- if(GPIO.input(18) == True): #Initiate Voice Operation
- print("Now recording voice clip")
- record()
- #encoded_audio = base64.b64encode(str(open('/home/pi/Documents/Owens_Measurements/output.wav').read(),'utf-8'))
- #print("Audio Encoded")
- #audio_string = str(encoded_audio)
- #print(audio_string)
- if(GPIO.input(13)):
- if (door_locked == True):
- print("unlocking door...")
- unlock_door()
- print("door unlocked")
- if(GPIO.input(31)):
- if(door_locked == False):
- print("locking door...")
- lock_door()
- print("door locked")
- #print("waiting for command")
- sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement