adafruit_minimqtt

MQTT Library for CircuitPython.

  • Author(s): Brent Rubell

Implementation Notes

Software and Dependencies:

exception adafruit_minimqtt.MMQTTException

MiniMQTT Exception class.

class adafruit_minimqtt.MQTT(socket, broker, port=None, username=None, password=None, network_manager=None, client_id=None, is_ssl=True, log=False, keep_alive=60)

MQTT Client for CircuitPython :param socket: Socket object for provided network interface :param str broker: MQTT Broker URL or IP Address. :param int port: Optional port definition, defaults to 8883. :param str username: Username for broker authentication. :param str password: Password for broker authentication. :param network_manager: NetworkManager object, such as WiFiManager from ESPSPI_WiFiManager. :param str client_id: Optional client identifier, defaults to a unique, generated string. :param bool is_ssl: Sets a secure or insecure connection with the broker. :param bool log: Attaches a logger to the MQTT client, defaults to logging level INFO. :param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.

attach_logger(logger_name='log')

Initializes and attaches a logger to the MQTTClient. :param str logger_name: Name of the logger instance

connect(clean_session=True)

Initiates connection with the MQTT Broker. :param bool clean_session: Establishes a persistent session.

deinit()

De-initializes the MQTT client and disconnects from the mqtt broker.

disconnect()

Disconnects the MiniMQTT client from the MQTT broker.

is_connected()

Returns MQTT client session status as True if connected, raises a MMQTTException if False.

is_sock_connected

Returns if the socket is connected.

is_wifi_connected

Returns if the ESP module is connected to an access point, resets module if False

last_will(topic=None, message=None, qos=0, retain=False)

Sets the last will and testament properties. MUST be called before connect(). :param str topic: MQTT Broker topic. :param str message: Last will disconnection message. :param int qos: Quality of Service level. :param bool retain: Specifies if the message is to be retained when it is published.

loop()

Non-blocking message loop. Use this method to check incoming subscription messages.

This method does NOT handle networking or network hardware management, use loop_forever or handle in code instead.

loop_forever()

Starts a blocking message loop. Use this method if you want to run a program forever. Code below a call to this method will NOT execute. Network reconnection is handled within this call.

mqtt_msg

Returns maximum MQTT payload and topic size.

ping()

Pings the MQTT Broker to confirm if the broker is alive or if there is an active network connection.

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

Publishes a message to a topic provided. :param str topic: Unique topic identifier. :param str msg: Data to send to the broker. :param int msg: Data to send to the broker. :param float msg: Data to send to the broker. :param bool retain: Whether the message is saved by the broker. :param int qos: Quality of Service level for the message.

Example of sending an integer, 3, to the broker on topic ‘piVal’. .. code-block:: python

mqtt_client.publish(‘topics/piVal’, 3)

Example of sending a float, 3.14, to the broker on topic ‘piVal’. .. code-block:: python

mqtt_client.publish(‘topics/piVal’, 3.14)

Example of sending a string, ‘threepointonefour’, to the broker on topic piVal. .. code-block:: python

mqtt_client.publish(‘topics/piVal’, ‘threepointonefour’)
reconnect(resub_topics=True)

Attempts to reconnect to the MQTT broker. :param bool resub_topics: Resubscribe to previously subscribed topics.

reconnect_socket()

Re-establishes the socket’s connection with the MQTT broker.

reconnect_wifi()

Reconnects to WiFi Access Point and socket, if disconnected.

set_logger_level(log_level)

Sets the level of the logger, if defined during init. :param string log_level: Level of logging to output to the REPL.

subscribe(topic, qos=0)

Subscribes to a topic on the MQTT Broker. This method can subscribe to one topics or multiple topics. :param str topic: Unique MQTT topic identifier. :param int qos: Quality of Service level for the topic, defaults to zero. :param tuple topic: Tuple containing topic identifier strings and qos level integers. :param list topic: List of tuples containing topic identifier strings and qos.

Example of subscribing a topic string. .. code-block:: python

mqtt_client.subscribe(‘topics/ledState’)

Example of subscribing to a topic and setting the qos level to 1. .. code-block:: python

mqtt_client.subscribe(‘topics/ledState’, 1)

Example of subscribing to topic string and setting qos level to 1, as a tuple. .. code-block:: python

mqtt_client.subscribe((‘topics/ledState’, 1))

Example of subscribing to multiple topics with different qos levels. .. code-block:: python

mqtt_client.subscribe([(‘topics/ledState’, 1), (‘topics/servoAngle’, 0)])
unsubscribe(topic)

Unsubscribes from a MQTT topic. :param str topic: Unique MQTT topic identifier. :param list topic: List of tuples containing topic identifier strings.

Example of unsubscribing from a topic string. .. code-block:: python

mqtt_client.unsubscribe(‘topics/ledState’)

Example of unsubscribing from multiple topics. .. code-block:: python

mqtt_client.unsubscribe([(‘topics/ledState’), (‘topics/servoAngle’)])