import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
# MQTT SERVER
SERVIDOR = "broker.hivemq.com"
print("Conectando-se ao WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Conectado!")
print("Conectando-se ao servidor MQTT... ", end="")
client = MQTTClient("ESP32Fulano", SERVIDOR, 1883)
client.connect()
print("Conectado!")
# led setup
led = Pin(2, Pin.OUT)
LED_TOPICO = "LigaLedJ"
def blink_led(topic, msg):
if msg.decode() == '1':
led.value(1)
if msg.decode() == '2':
led.value(0)
# mqtt subscription
client.set_callback(blink_led)
client.subscribe(LED_TOPICO)
while True:
client.check_msg()
time.sleep(0.5)