from wifi_lib import conecta
from umqttsimple import MQTTClient
import time
from machine import Pin
mqtt_server = "broker.mqttdashboard.com"
mqtt_port = 1883
mqtt_user = ""
mqtt_password = ""
mqtt_client_id = "clientId-2mT0Ny4w7l"
LED = Pin(5, Pin.OUT)
def recebi(topico, msg):
if msg.decode() == "LIGA":
LED.on()
else:
LED.off()
print("Conectando...")
station = conecta("Wokwi-GUEST", "")
if not station.isconnected():
print("Falha na conexão")
else:
print("Conectado")
print("Conectando Broker MQTT")
client = MQTTClient(mqtt_client_id,
mqtt_server,
mqtt_port,
mqtt_user,
mqtt_password)
client.set_callback(recebi) # Callback deve ser configurado antes de conectar
client.connect()
client.subscribe("pucpr/iotmc/msg_esp32")
try:
while True:
client.check_msg() # Checa por novas mensagens
time.sleep(1) # Aguarda um pouco antes de checar novamente
except KeyboardInterrupt:
print("Desconectando...")
client.disconnect()
station.disconnect()