from machine import Pin
import dht
import time

# Configuración de pines
led_rojo = Pin(15, Pin.OUT)   # LED rojo en GP15
led_verde = Pin(16, Pin.OUT)  # LED verde en GP16
button = Pin(18, Pin.IN, Pin.PULL_DOWN)  # Botón en GP18 con pull-down
sensor_dht = dht.DHT22(Pin(17))  # DHT22 en GP17

while True:
    # Leer el estado del botón
    if button.value() == 1:  
        led_rojo.value(1)  # Enciende el LED rojo si el botón está presionado
    else:
        led_rojo.value(0)  # Apaga el LED rojo si no está presionado

    # Leer temperatura y humedad del DHT22
    try:
        sensor_dht.measure()  # Tomar una nueva lectura
        temp = sensor_dht.temperature()  # Temperatura en °C
        hum = sensor_dht.humidity()  # Humedad en %

        print(f"Temperatura: {temp:.1f}°C, Humedad: {hum:.1f}%")

        # Control del LED verde según la temperatura
        if temp > 25:
            led_verde.value(1)  # Enciende el LED verde si la temperatura supera 25°C
        else:
            led_verde.value(0)  # Apaga el LED verde si está por debajo de 25°C

    except Exception as e:
        print("Error al leer el DHT22:", e)

    time.sleep(2)  # Esperar 2 segundos antes de la siguiente lectura
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT