from umqtt.simple import MQTTClient # Connected to MQTT broker client = MQTTClient("publisher", "broker_hostname") client.connect() # Publish a message to a topic client.publish(b"testtopic", b"Hello world") # Disconnect the client client.disconnect()
from umqtt.simple import MQTTClient # Callback function for when a message is received def callback(topic, msg): print("Received message: ", msg) # Connect to the MQTT broker client = MQTTClient("subscriber", "broker_hostname") client.connect() # Subscribe to a topic client.set_callback(callback) client.subscribe(b"testtopic") # Wait for a message to be received while True: client.wait_msg()This library is part of the MicroPython umqtt package library.