from umqtt.simple import MQTTClient # connect to MQTT broker client = MQTTClient('client_id', 'mqtt.example.com') client.connect() # publish a message to a topic topic = 'my/topic' message = 'Hello, World!' client.publish(topic, message) # disconnect from MQTT broker client.disconnect()
from umqtt.simple import MQTTClient # connect to MQTT broker client = MQTTClient('client_id', 'mqtt.example.com') client.connect() # publish a message to a topic with QoS level 1 topic = 'my/topic' message = 'Hello, World!' client.publish(topic, message, qos=1) # disconnect from MQTT broker client.disconnect()In this example, we connect to the MQTT broker, publish a "Hello, World!" message to the "my/topic" topic, and specify a QoS level of 1. This means that the broker will acknowledge receipt of the message and will attempt to deliver it to the subscriber even if the subscriber is not currently online. Package Library: The umqtt.simple module is part of the MicroPython library and is included in the micropython-lib package for PyPI.