import time
import dht
import network
from machine import Pin
from umqtt.simple import MQTTClient
# Configuración del sensor DHT22
sensor = dht.DHT22(Pin(15)) # Pin del sensor DHT22
# Configuración del LED de alerta y el relé para el ventilador
led_alarma = Pin(16, Pin.OUT) # LED de alerta (pin 16)
rele_ventilador = Pin(32, Pin.OUT) # Relé para el ventilador (pin 32)
# Umbral de temperatura para activar la alarma y el ventilador
umbral_temperatura = 28 # Puedes ajustar este valor según tus necesidades
# Configuración Wi-Fi
ssid = 'Wokwi-GUEST'
wifipassword = ''
# Configuración MQTT
mqtt_server = 'io.adafruit.com'
port = 1883
user = 'Matvillan'
password = 'aio_ubsp92Kd9m823s6FH16njB5z3Z0o'
client_id = 'Proyecto'
topic_TEMPERATURA = 'Matvillan/feeds/SensorT'
topic_HUMEDAD = 'Matvillan/feeds/SensorH'
topic_ALERTA = 'Matvillan/feeds/Alarma'
# Conexión Wi-Fi
def conectar_wifi():
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, wifipassword)
print("Conectando")
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.5)
print("\nConexión Wi-Fi Exitosa")
print("IP:", sta_if.ifconfig()[0])
# Función para leer la temperatura y la humedad
def leer_temperatura_humedad():
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
return temp, hum
except OSError as e:
print("Error al leer del sensor DHT22:", e)
return None, None
# Conexión al servidor MQTT
def conectar_mqtt():
try:
client = MQTTClient(client_id, mqtt_server, user=user, password=password, port=port)
client.connect()
print("Conectado con Broker MQTT")
return client
except OSError as e:
print("Error al conectar con el servidor MQTT")
time.sleep(5)
machine.reset()
# Inicializar Wi-Fi y conexión MQTT
conectar_wifi()
client = conectar_mqtt()
# Bucle principal
while True:
temp, hum = leer_temperatura_humedad()
if temp is not None:
print(f"Temperatura: {temp}°C, Humedad: {hum}%")
# Publicar datos en Adafruit IO
client.publish(topic_TEMPERATURA, str(temp))
client.publish(topic_HUMEDAD, str(hum))
# Verificar si la temperatura supera el umbral
if temp > umbral_temperatura:
print("¡Alerta! Temperatura alta")
led_alarma.value(1) # Enciende el LED de alerta
rele_ventilador.value(1) # Activa el ventilador
client.publish(topic_ALERTA, "ON") # Publicar alerta en Adafruit IO
# Simulación de la reducción de la temperatura con el ventilador encendido
while temp > umbral_temperatura:
time.sleep(2) # Esperar un poco antes de leer nuevamente
temp -= 1 # Simulación de la reducción de la temperatura
print(f"Disminuyendo temperatura: {temp}°C")
client.publish(topic_TEMPERATURA, str(temp))
print("Temperatura bajo el umbral, apagando ventilador")
rele_ventilador.value(0) # Apaga el ventilador
led_alarma.value(0) # Apaga el LED de alerta
client.publish(topic_ALERTA, "OFF")
else:
# Si la temperatura está por debajo del umbral, asegúrate de que todo esté apagado
led_alarma.value(0)
rele_ventilador.value(0)
client.publish(topic_ALERTA, "OFF")
# Esperar antes de la próxima lectura
time.sleep(5)