import time
from time import sleep
from machine import Pin
from umqtt.simple import MQTTClient
import ujson
import network
# MQTT Server Parameters
MQTT_CLIENT_ID = "Wokwi-GUEST"
MQTT_BROKER = "broker.emqx.io"
MQTT_USER = "Wokwi-GUEST"
MQTT_PASSWORD = ""
MQTT_TOPIC = "exams/Asoukidis/speed/"
# WiFi connection
print("Connecting to 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(" Connected!")
# MQTT server connection
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
led = Pin(18, Pin.OUT)
print("PROGRAM START")
while True:
def led_speed(topic, msg):
if msg == b'1':
led.on()
sleep(0.5)
led.off()
sleep(0.5)
client.check_msg()
elif msg == b'2':
led.on()
sleep(2)
led.off()
sleep(2)
client.check_msg()
client.set_callback(led_speed)
client.subscribe(MQTT_TOPIC)