Security System using MQTT Cloud and Ultasonic Sensor
from Adafruit_IO import Client
aio = Client('Your Adafruit IO Key')
import RPi.GPIO as GPIO
import time
import signal
import sys
# use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM)
# set GPIO Pins
pinTrigger = 18
pinEcho = 24
def close(signal, frame):
print("\n ultrasonic distance detection...\n")
GPIO.cleanup()
sys.exit(0)
signal.signal(signal.SIGINT, close)
# set GPIO input and output channels
GPIO.setup(pinTrigger, GPIO.OUT)
GPIO.setup(pinEcho, GPIO.IN)
while True:
# set Trigger to HIGH
GPIO.output(pinTrigger, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(pinTrigger, False)
startTime = time.time()
stopTime = time.time()
# save start time
while 0 == GPIO.input(pinEcho):
startTime = time.time()
# save time of arrival
while 1 == GPIO.input(pinEcho):
stopTime = time.time()
# time difference between start and arrival
TimeElapsed = stopTime - startTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
print ("Distance: %.1f cm" % distance)
# if distance <= 20:
aio.send('Gauge', distance)
# aio.send('MSG', "Detected")
time.sleep(1)
# else:
aio.send('Gauge', distance)
# aio.send('MSG', "Not Detected")
#
#
data = aio.receive('Gauge')
print('Received value: {0}'.format(data.value))
0 comments:
Post a Comment
if you have any doubt please let me know