from machine import Pin
import time
import network
from umqtt.simple import MQTTClient
import ujson
import micropython
led = Pin(25, Pin.OUT)
last_msg = ""
state = ""
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect('Wokwi-GUEST', '')
while not wifi.isconnected():
print("connecting to wifi")
time.sleep(1)
print(wifi.ifconfig())
print(wifi.ifconfig())
client = "ee5c54b6-de3b-4fd1-a151-82d05da635e8"
broker = "broker.netpie.io"
token = "vwm35ppM1F8ggiBYZ9SHWQT3TSA8RgND"
secret = "vQdZzwDsMWuNDRrxNBacocxkLbTDiXyN"
topic = "@msg/operator"
def on_message(topic, msg):
global last_msg
global state
last_msg = msg.decode().upper()
if last_msg == "ON":
state = "on"
led.value(1)
elif last_msg == "OFF":
state = "off"
led.value(0)
netpie = MQTTClient(client, broker, user=token, password=secret)
netpie.set_callback(on_message)
netpie.connect()
netpie.subscribe(topic)
while True:
netpie.check_msg()
print("led mode:", state)
time.sleep(0.5)