#Lab25_Sensor_DHT22.py
from machine import Pin
import time
from dht import DHT11, DHT22
dht22 = DHT22(Pin(23))
led_red = Pin(2,Pin.OUT)
led_yellow = Pin(4,Pin.OUT)
led_green = Pin(5,Pin.OUT)
led_red.value(0)
led_yellow.value(0)
led_green.value(0)
while True:
try:
time.sleep(0.5)
dht22.measure()
temperature_C = dht22.temperature()
temperature_C = round(temperature_C, 2)
humidity = dht22.humidity()
humidity = round(humidity, 2)
print("Temperature: ", temperature_C, 'C')
print("Humidity: ", humidity, "%")
time.sleep(2)
if temperature_C > 45:
led_red.on()
led_yellow.off()
led_green.off()
elif temperature_C > 35:
led_red.off()
led_yellow.on()
led_green.off()
else:
led_red.off()
led_yellow.off()
led_green.on()
except OSError as e:
print('Failed to read sensor.')