##Libreria para configurar la conexión WiFi del dispositivo
import network
##Libreria para gestionar la conexión al servidor MQTT.
from umqtt.simple import MQTTClient
##Libreria para para manejar datos en formato JSON
import ujson
##Libreria para controlar hardware del ESP32 (pines, PWM, etc.).
import machine
from machine import Pin, PWM
##Libreria para manejar hilos, permitiendo que el código escuche mensajes MQTT mientras realiza otras tareas.
import _thread
##Libreria para para manejar retardos y sincronización de tiempo
import time
##Libreria para leer datos del sensor de temperatura y humedad DHT22.
import dht
# Configuro los leds
coldLed = Pin(13, machine.Pin.OUT)
normalLed = Pin(12, machine.Pin.OUT)
warmLed = Pin(14, machine.Pin.OUT)
control = Pin(27, machine.Pin.OUT)
servo_pin = Pin(18)
servo = machine.PWM(servo_pin, freq=50)
servo.duty(30)
# Apgo todos los led turn off/ enciendo solo el LED que pase como parametro
def turnOnLed(led):
coldLed.off()
normalLed.off()
warmLed.off()
control.off()
led.on()
# callback for the messages from mqtt broker
def on_mqtt_message(topic, msg):
print("Incoming message:", msg)
try:
msg = ujson.loads(msg)
led_control = msg.get("control")
if led_control == 1:
servo.duty(125) # Ángulo de 180 grados
turnOnLed(control)
elif led_control == 0:
servo.duty(30) # Ángulo de 0 grados
control.off()
except Exception as e:
print("Error:", e)
print("Connecting to WiFi...", end="")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
while not wifi.isconnected():
time.sleep(0.5)
print(".", end="")
print("Done")
print("Connecting to MQTT...")
client = MQTTClient("","mqtt-dashboard.com")
client.set_callback(on_mqtt_message)
client.connect()
client.subscribe("topics/esp32_00001")
print("ESP32 Connected!")
def wait_message_from_mqtt(name):
while True:
client.wait_msg() # enabling the listening mode for the messages..
_thread.start_new_thread(wait_message_from_mqtt, (1,))
sensor = dht.DHT22(Pin(15))
lastMessage_t = None
lastMessage_h = None
while True:
sensor.measure()
message = ujson.dumps({
"temp": sensor.temperature(),
"humidity": sensor.humidity(),
})
if (lastMessage_t != sensor.temperature() or lastMessage_h != sensor.humidity()):
lastMessage_t = sensor.temperature()
lastMessage_h = sensor.humidity()
client.publish("TOPIC/mobile_00001/RQ", message)
print(message)
if(sensor.temperature() > 41):
turnOnLed(warmLed)
elif(sensor.temperature() > 15 and sensor.temperature() < 40):
turnOnLed(normalLed)
elif(sensor.temperature() < 15):
turnOnLed(coldLed)
time.sleep(1)