umqtt.simple — Simple MQTT client function

MQTT is a “lightweight” publish-subscribe based messaging protocol for use on top of TCP/IP. It provides a subscribe/publish mode, is more simple and lightweight, and easy to use. It targets constrained environments (low bandwidth, high network latency, unstable network communication), and can be simply summarized as built for the Internet of Things.

Hint

This module comes from MicroPython-lib : https://github.com/micropython/micropython-lib/tree/master/umqtt.simple

Construct the object

class umqtt.simple.MQTTClient(client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={})
  • client_id

  • server

  • port

  • user

  • password

  • keepalive

  • ssl

  • ssl_params

Methods

MQTTClient.set_callback(f)
  • f - f(topic, msg) is the callback function. The first parameter topic is the received topic; the second parameter msg is the message of that topic.

Set the callback for received subscription messages

MQTTClient.set_last_will(topic, msg, retain=False, qos=0)

topic and msg are of bytes type

Set the MQTT “last will” message. It should be called before connect().

MQTTClient.connect(clean_session=True)

Connect to the server. It returns True if this connection uses a persistent session stored on the server (returns False if the clean_session = True parameter is used, which is the default).

MQTTClient.disconnect()

Disconnect from the server and release resources.

MQTTClient.ping()

Ping the server (the response is handled automatically by wait_msg())

MQTTClient.publish(topic, msg, retain=False, qos=0)

topic and msg are of bytes type

Publish a message

MQTTClient.subscribe(topic, qos=0)

topic is of bytes type

Subscribe to a topic

MQTTClient.wait_msg()

Wait for a server message. Subscription messages will be passed to the callback set via set_callback(); any other messages will be handled internally.

MQTTClient.check_msg()

Check whether the server has pending messages. If so, handle them in the same way as wait_msg(); if not, return immediately.

Attention

  • wait_msg() and check_msg() are “main loop iteration” methods, the blocking and non-blocking versions. You should call wait_msg() periodically in a loop if you have no other foreground tasks to perform (i.e. your application only responds to subscribed MQTT messages), or check_msg() if you also handle other foreground tasks.

  • Note that if you only publish messages, you do not need to call wait_msg() / check_msg(), nor subscribe to messages.

  • Both publishing and subscribing support QoS 0 and 1. QoS 2 is not supported to keep the code size small. Apart from ClientID, currently only the “clean session” parameter is supported for connection.

  • All data related to MQTT messages is encoded as bytes. This includes the message content and topic names (even though the MQTT specification states that topic names are UTF-8 encoded). The reason is simple: what is received through the network socket is binary data (bytes).