栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树莓派人体感应警报(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()

运行结果


上云:
侵权即删

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号