• 大小: 6.79KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-05-05
  • 语言: Python
  • 标签:

资源简介

【实例简介】 以树莓派为基础,连接有毒气体传感器、温湿度传感器、Co2浓度传感器,如果传感器检测到的数据超过预设的值,将会触发灯光和蜂鸣器报警
【核心源码】 air_detection_alarm_system.py

资源截图

代码片段和文件信息

import time
import serial
import binascii
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11 GPIO.OUT) #蜂鸣器针脚
GPIO.setup(12GPIO.INpull_up_down=GPIO.PUD_DOWN) #有毒气体检测针脚
GPIO.setup(13GPIO.OUT) #有毒气体灯光针脚
GPIO.setup(15GPIO.OUT) #温度灯光针脚
GPIO.setup(18GPIO.OUT) #co2浓度灯光针脚


def buzzer(): #声音报警
    
    pu={
    “1“:261.36
    “2“:293.66
    “3“:329.63
    “4“:349.23
    “5“:392
    “6“:440
    “7“:439.88
    }

    p = GPIO.PWM(11 1)  
    p.start(50)
    try:
        xxx=“1155665-4433221“
        for i in xxx:
            
            if i == “-“:
                time.sleep(0.5)
                continue
            p.ChangeDutyCycle(50)
            p.ChangeFrequency(pu[i])
            time.sleep(0.5)
            p.ChangeDutyCycle(0)
    except:
        pass
    p.stop()
    
    

def Toxic_gas(a): #有毒气体检测
    ydu_bj=a
    try:
        GPIO.setup(12GPIO.INpull_up_down=GPIO.PUD_DOWN)
        
        status = GPIO.input(12)
        if status == True:
            ydu_bj=0
            print(“未发现有毒气体“)
            GPIO.output(13False) #有毒气体报警灯暗
        else :
            ydu_bj = ydu_bj +1
            if ydu_bj>1:
                print(“发现有毒气体“)
                GPIO.output(13True) #有毒气体报警灯亮
                buzzer()
                    
            else:
                print(“可能有有毒气体“)
        return ydu_bj
    except KeyboardInterrupt:
        pass
        return ydu_bj

def wendu(abc):
    try:
        
        wen=a
        shi=b
        wen_bj=c
        GPIO.setmode(GPIO.BOARD)
        
        channel = 16           #引脚号16  
        data = []           #温湿度值
        j = 0               #计数器         
        GPIO.setup(channel GPIO.OUT) 
        time.sleep(1)
        GPIO.output(channel GPIO.LOW)  
        time.sleep(0.024)        #给信号提示传感器开始工作
        GPIO.output(channel GPIO.HIGH) 
        GPIO.setup(channel GPIO.IN) 
        while GPIO.input(channel) == GPIO.LOW: 
            continue
        while GPIO.input(channel) == GPIO.HIGH: 
            continue 
        while j < 40:  
            k = 0
            while GPIO.input(channel) == GPIO.LOW:  
                continue
            while GPIO.input(channel) == GPIO.HIGH:  
                k += 1
                if k > 100:  
                    break
            if k < 6:  
                data.append(0)  
            else:  
                data.append(1)  
            j += 1  
           
        humidity_bit = data[0:8]        #分组  
        humidity_point_bit = data[8:16]  
        temperature_bit = data[16:24]  
        temperature_point_bit = data[24:32]
        check_bit = data[32:40]  
        humidity = 0  
        humidity_point = 0  
        temperature = 0  
        temperature_point = 0  
        check = 0  
        for i in range(8):  
            humidity += humidity_bit[i] * 2 ** (7 - i)              #转换成十进制数据  
            humidity_point += humidity_point_bit[i] * 2 ** (7 - i)  
            temperature += temperature_bit[i] * 2 ** (7 - i)  
      

评论

共有 条评论

相关资源