树莓派人体感应警报(python)HC-SR501红外人体感应

使用树莓派+传感器实现人体感应警报

使用模块
树莓派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()

运行结果

在这里插入图片描述
上云
在这里插入图片描述请添加图片描述侵权即删

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>