MQTT Library for CircuitPython.
- Author(s): Brent Rubell
Software and Dependencies:
- Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases
MiniMQTT Exception class.
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.
Initializes and attaches a logger to the MQTTClient. :param str logger_name: Name of the logger instance
Initiates connection with the MQTT Broker. :param bool clean_session: Establishes a persistent session.
De-initializes the MQTT client and disconnects from the mqtt broker.
Disconnects the MiniMQTT client from the MQTT broker.
Returns MQTT client session status as True if connected, raises a MMQTTException if False.
Returns if the socket is 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.
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.
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.
Returns maximum MQTT payload and topic size.
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:: pythonmqtt_client.publish(‘topics/piVal’, 3)
Example of sending a float, 3.14, to the broker on topic ‘piVal’. .. code-block:: pythonmqtt_client.publish(‘topics/piVal’, 3.14)
Example of sending a string, ‘threepointonefour’, to the broker on topic piVal. .. code-block:: pythonmqtt_client.publish(‘topics/piVal’, ‘threepointonefour’)
Attempts to reconnect to the MQTT broker. :param bool resub_topics: Resubscribe to previously subscribed topics.
Re-establishes the socket’s connection with the MQTT broker.
Reconnects to WiFi Access Point and socket, if disconnected.
Sets the level of the logger, if defined during init. :param string log_level: Level of logging to output to the REPL.
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:: pythonmqtt_client.subscribe(‘topics/ledState’)
Example of subscribing to a topic and setting the qos level to 1. .. code-block:: pythonmqtt_client.subscribe(‘topics/ledState’, 1)
Example of subscribing to topic string and setting qos level to 1, as a tuple. .. code-block:: pythonmqtt_client.subscribe((‘topics/ledState’, 1))
Example of subscribing to multiple topics with different qos levels. .. code-block:: pythonmqtt_client.subscribe([(‘topics/ledState’, 1), (‘topics/servoAngle’, 0)])
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:: pythonmqtt_client.unsubscribe(‘topics/ledState’)
Example of unsubscribing from multiple topics. .. code-block:: pythonmqtt_client.unsubscribe([(‘topics/ledState’), (‘topics/servoAngle’)])