import dht
from machine import Pin
from time import sleep
red = Pin(15, Pin.OUT)
yellow = Pin(2, Pin.OUT)
blue = Pin(0, Pin.OUT)
sensor = dht.DHT22(Pin(14))
while True:
try:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
print('Temperature is', t,'\u00B0C')
print('Humidity is', h)
if t < 25.0:
blue.on()
elif t < 38.0:
yellow.on()
else:
red.on()
except OSError as e:
print(e)
else:
pass
finally:
sleep(2) # 2 seconds
red.off()
yellow.off()
blue.off()