M5mqtt

Use the API in the M5mqtt module to connect to the mqtt server and subscribe to publish message content.

-Connect to mqtt server


from m5mqtt import M5mqtt

//Create connection instance
m5mqtt = M5mqtt(
    client_id,
    server,
    port=0,
    user=None,
    password=None,
    keepalive=0,
    ssl=False,
    ssl_params=None
)

//Start connection
m5mqtt.start()

while True:

-Subscribe and publish news


//Subscribe news
def callback(topic_data):
    print(topic_data)

m5mqtt.subscribe(topic, callback)


//release the news
m5mqtt.publish(topic, data)

-Other configurations


//Configure the client will message
m5mqtt.set_last_will(topic, msg)

//Disconnect
m5mqtt.deinit()
On This Page