from machine import Pin
from dht import DHT22
from time import sleep
dht = DHT22(Pin(13))
# Temperature leds
temp_yellow_led = Pin(17, Pin.OUT)
temp_green_led = Pin(18, Pin.OUT)
temp_red_led = Pin(19, Pin.OUT)
temp_led_on = ""
# Humidty leds
humi_violet_led = Pin(25, Pin.OUT)
humi_blue_led = Pin(26, Pin.OUT)
humi_orange_led = Pin(27, Pin.OUT)
humi_led_on = ""
def celsius_to_fahrenheit(celsius):
return celsius * 1.8 + 32
while True:
dht.measure() # Start the read of temperature
temperature_celsius = dht.temperature()
humidity_percent = dht.humidity()
if temperature_celsius > 50:
temp_green_led.off()
temp_yellow_led.off()
temp_red_led.on()
temp_led_on = "Vermelho"
elif temperature_celsius <= 50 and temperature_celsius >= 30:
temp_green_led.on()
temp_yellow_led.off()
temp_red_led.off()
temp_led_on = "Verde"
elif temperature_celsius < 30:
temp_green_led.off()
temp_yellow_led.on()
temp_red_led.off()
temp_led_on = "Amarelo"
if humidity_percent > 80:
humi_violet_led.off()
humi_blue_led.off()
humi_orange_led.on()
humi_led_on = "Laranja"
elif humidity_percent <= 80 and humidity_percent >= 40:
humi_violet_led.off()
humi_blue_led.on()
humi_orange_led.off()
humi_led_on = "Azul"
elif humidity_percent < 40:
humi_violet_led.on()
humi_blue_led.off()
humi_orange_led.off()
humi_led_on = "Violeta"
print("-"*60)
print(f"Temperatura: {temperature_celsius}ºC | {celsius_to_fahrenheit(temperature_celsius)}ºF")
print(f"LED de temperatura aceso: {temp_led_on}\n")
print(f"Umidade: {humidity_percent}%")
print(f"LED de umidade aceso: {humi_led_on}")
print("-"*60)
sleep(0.2)