import time
import network
from machine import Pin, PWM
from umqtt.simple import MQTTClient
MQTT_BROKER = "broker.hivemq.com"
MQTT_CLIENT_ID = "buzzer-12G-17-Plamena"
MQTT_TOPIC = "elsys/12G/17/Plamena"
# Button constants - Update according to schematics
BUTTON_PINS = [32,33,25,26]
# Connect to Wi-Fi
def connect_to_wifi():
print("Connecting to Wi-Fi", 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!")
def connect_to_mqbroker():
connect_to_wifi()
print("Connecting to MQ Hive", end="")
client.connect()
print("Connected to broker ", end="")
client.subscribe(MQTT_TOPIC)
print("Subscribed to {}".format(MQTT_TOPIC))
def on_message(topic, message):
print ("Recieving message on {} - activate buzzer {}".format(topic, message))
def push_button():
client = MQTTClient(MQTT_BROKER, MQTT_CLIENT_ID)
client.set_callback(on_message)
connect_to_mqbroker()
while True:
client.check_msg()