from machine import Pin
import dht
import time
# 1. Configuración de Pines
sensor_clima = dht.DHT22(Pin(15))
# Entradas con resistencia Pull-Up interna (activas en 0 / presionado)
switch_general = Pin(13, Pin.IN, Pin.PULL_UP)
boton_entrada = Pin(12, Pin.IN, Pin.PULL_UP)
boton_salida = Pin(14, Pin.IN, Pin.PULL_UP)
# Salidas (Actuadores)
led_sistema = Pin(26, Pin.OUT)
led_ventilador = Pin(27, Pin.OUT)
# 2. Variables de estado
aforo = 0
temp_limite = 28.0
ultimo_tiempo_clima = 0
print("Sistema iniciado. Usa el switch para encender o apagar.")
# 3. Bucle principal
while True:
# Verificar si el interruptor general está encendido (valor 0 por PULL_UP)
sistema_encendido = (switch_general.value() == 0)
if not sistema_encendido:
# Apagar todo si el switch está en OFF
led_sistema.value(0)
led_ventilador.value(0)
time.sleep(0.2)
continue
# Si el sistema está encendido, activar indicador principal
led_sistema.value(1)
# --- CONTROL DE ENTRADA ---
if boton_entrada.value() == 0:
aforo += 1
print(f"[ENTRADA] Persona ingresó. Aforo actual: {aforo}")
time.sleep(0.3) # Pausa antirebote
# --- CONTROL DE SALIDA ---
if boton_salida.value() == 0:
if aforo > 0:
aforo -= 1
print(f"[SALIDA] Persona salió. Aforo actual: {aforo}")
else:
print("[AVISO] El lugar ya está vacío (Aforo: 0)")
time.sleep(0.3) # Pausa antirebote
# --- LECTURA DE TEMPERATURA Y HUMEDAD (cada 2 segundos) ---
tiempo_actual = time.ticks_ms()
if time.ticks_diff(tiempo_actual, ultimo_tiempo_clima) >= 2000:
sensor_clima.measure()
temperatura = sensor_clima.temperature()
humedad = sensor_clima.humidity()
print(f"Clima -> Temp: {temperatura:.1f}°C | Humedad: {humedad:.1f}% | Personas: {aforo}")
# Automatización: encender ventilador si hace calor y hay gente adentro
if temperatura > temp_limite and aforo > 0:
led_ventilador.value(1)
print("-> Ventilador ENCENDIDO por alta temperatura y presencia.")
else:
led_ventilador.value(0)
ultimo_tiempo_clima = tiempo_actual
time.sleep(0.05)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4