Home

Awesome

micropython-mqtt

Async MQTT client library with auto reconnect for MicroPython devices such as the ESP32 or Pycom devices

Installation

Features

The MQTTClient class is a simple lightweight MQTT client for basic MQTT pub / sub functionality. It has the following features:

Usage

See the basic-example.py file in the examples folder for a simple example of using the MQTTClient class

from libs.mqtt import MQTTClient

Import the MQTTClient class

client = MQTTClient('192.168.1.1', port=1883)

Construct a new instance of the MQTTClient class

set_connected_callback(cb)

Set the callback method which is fired when the connected status changes. This callback is optional

set_message_callback(cb)

Set the callback method which is fired when a new MQTT message arrives. This callback is optional

set_puback_callback(cb)

Set the callback method which is fired when a publish command succeeds. This callback is optional

set_suback_callback(cb)

Set the callback method which is fired when a subscribe command succeeds. This callback is optional

set_unsuback_callback(cb)

Set the callback method which is fired when an ubsubscribe command succeeds. This callback is optional

connect(client_id, user=None, password=None, clean_session=True, will_topic=None, will_qos=0, will_retain=False, will_payload=None)

Connect the MQTT client to the broker. This method is non-blocking and returns before the connection has completed.

disconnect()

Disconnect the MQTT client from the broker

isconnected()

Get the status of the connection between the MQTT client and broker

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

Publish an MQTT message to the broker

subscribe(topic, qos=0)

Subscribe to an MQTT topic

unsubscribe(topic)

Unsubscribe from an MQTT topic