用树莓皮B模DHT22控制继电器

2024-10-01 04:52:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个基本上恒温器控制使用DHT22传感器。我想从传感器读取数据,并使用if语句将传感器值与设定的温度值进行比较。如果实现,If语句将使用常闭出口打开继电器。下面是我在Geany编程中编写的代码,以便大致了解我想做什么。你知道吗

from time import sleep

import RPi.GPIO as GPIO

import Adafruit_DHT as dht


GPIO.setmode(GPIO.BCM) 

AC_1_GPIO = 17                              #sets relay to GPIO pin 17 for AC unit control

DHT_1_GPIO = 3

GPIO.setup(AC_1_GPIO, GPIO.OUT)             #sets up GPIO to be on or off based on condition

#GPIO.setup(DHT_1_GPIO, GPIO.OUT)                           #

humdtemp = dht.read_retry(dht.DHT22, DHT_1_GPIO)

Temp_Max = 32                                           #this is temperature maximum value for tent

#Humd_Max = 60                                          #this is relative humidity maximum value for 
#Humd_Min = 30                                          #this is relative humidity mininum value for 

while True:                                           #creates continuous loop

    if humdtemp > Temp_Max:                                     #compares sensor value to maximum 
# temperature value

        GPIO.output(AC_1_GPIO, GPIO.LOW)                #turns on AC unit using controllable relay

    elif humdtemp == Temp_Max:

        print ("Achieving 72 degree temperature")     #we need to select a temperature setpoint value

    else:
        GPIO.output(AC_1_GPIO, GPIO.HIGH)               #turns 'on' normally off relay

Tags: toimportforgpiovalueon传感器relay