使用树莓派+传感器实现人体感应警报
使用模块:
树莓派cm4
蜂鸣器模块1个
HC-SR501红外人体感应模块 1个
面包板
子母杜邦线若干
引脚图
GPIO引脚插法
BCM编码
VCC 5V
GND GND
17 HC-SR501中的OUT
18 蜂鸣器i/o
函数:
import RPi.GPIO as GPIO
import time
‘’‘
import json
import paho.mqtt.client as mqtt
#发布客户端
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect('1.116.67.152', 1883, 60) # 60为keepalive的时间间隔
client.subscribe('Sub/100026', qos=0)
client.loop_start() # 保持连接
’‘’
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.setup(18,GPIO.OUT)
pass
def beep():
for i in range(1,6):
GPIO.output(18, GPIO.LOW)
time.sleep(0.5)
GPIO.output(18, GPIO.HIGH)
time.sleep(0.5)
print ("the Buzzer will make sound")
def detct():
for i in range(1, 31):
if GPIO.input(17) == True:
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" Someone is closing!")
c = {"man":1}
’‘’
client.publish('Pub/100026', payload=json.dumps(c), qos=0) #上传数据
beep()
‘’‘
else:
GPIO.output(18, GPIO.LOW)#无人时关闭蜂鸣器
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" Noanybody!")
‘’‘
c = {"man":0}
client.publish('Pub/100026', payload=json.dumps(c), qos=0) #上传数据
‘’‘
time.sleep(3) #每3秒检查一次
time.sleep(2)
init()
detct()
GPIO.cleanup()
运行结果
上云:
侵权即删
版权声明:本文为CSDN博主「浅蓝色£”的忧殇♛」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cc777777777/article/details/121798547
暂无评论