import time
import dht
from machine import PWM, Pin
import neopixel
# --- Configuración de Hardware ---
# Sensor de temperatura y humedad DHT22 en el pin 4
sensor_temp = dht.DHT22(Pin(4))
# Matriz NeoPixel WS2812 (por defecto en Wokwi: 8x8 = 64 LEDs) en el pin 18
NUM_LEDS = 64
np = neopixel.NeoPixel(Pin(18), NUM_LEDS)
# Buzzer pasivo en el pin 15 mediante PWM
buzzer = PWM(Pin(15))
buzzer.duty(0)
# Umbrales de temperatura (°C) para el proceso
TEMP_BAJA = 25.0
TEMP_ALTA = 35.0
def limpiar_matriz():
for i in range(NUM_LEDS):
np[i] = (0, 0, 0)
np.write()
def actualizar_indicadores(temp_c):
"""Dibuja una barra térmica en los primeros 16 LEDs y un indicador de estado en el LED 63."""
limpiar_matriz()
# Calcula cuántos LEDs encender en la barra (entre 1 y 16) según temp de 15 °C a 45 °C
proporcion = (temp_c - 15.0) / (45.0 - 15.0)
if proporcion < 0:
proporcion = 0.0
elif proporcion > 1:
proporcion = 1.0
leds_barra = int(proporcion * 16)
if leds_barra < 1:
leds_barra = 1
# Define colores según zona térmica
if temp_c < TEMP_BAJA:
color_barra = (0, 0, 35) # Azul suave
color_estado = (0, 35, 0) # Verde
alarma_activa = False
elif temp_c <= TEMP_ALTA:
color_barra = (0, 35, 10) # Verde-cian
color_estado = (0, 35, 0) # Verde
alarma_activa = False
else:
color_barra = (40, 0, 0) # Rojo
color_estado = (40, 0, 0) # Rojo
alarma_activa = True
for i in range(leds_barra):
np[i] = color_barra
# LED de estado general en la última posición (63)
np[NUM_LEDS - 1] = color_estado
np.write()
return alarma_activa
# --- Secuencia Automatizada de Inicio ---
print("Iniciando prueba automatizada de matriz NeoPixel y alarma...")
for idx in [0, 1, 2, 3, NUM_LEDS - 1]:
np[idx] = (0, 25, 25)
np.write()
buzzer.freq(900)
buzzer.duty(350)
time.sleep(0.25)
buzzer.duty(0)
limpiar_matriz()
print("Sistema listo. Ajusta la temperatura en el DHT22 durante la simulación.")
# --- Bucle Principal de Monitoreo ---
while True:
try:
sensor_temp.measure()
temp = sensor_temp.temperature()
hum = sensor_temp.humidity()
print("Temperatura: {:.1f} °C | Humedad: {:.1f} %".format(temp, hum))
alarma = actualizar_indicadores(temp)
if alarma:
# Alarma acústica por sobretemperatura
buzzer.freq(1500)
buzzer.duty(512)
time.sleep(0.2)
buzzer.duty(0)
time.sleep(0.2)
else:
buzzer.duty(0)
time.sleep(1.5)
except OSError as e:
print("Error al leer el sensor DHT22:", e)
buzzer.duty(0)
time.sleep(1.0)
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4