This implementation of
mgtt is written completely in C and runs (when client is created) in
background (as separate freeRTOS) task.
Multiple clients can be created, connected to the different mqtt servers (brokers). Secure (ssl) and unsecure connection is supported.
To create the new mqtt client, only two parameters are mandatory and can be given as positional or kw parameters:
mqtt = network.mqtt("description", "server")
or
mqtt = network.mqtt(name="description", server="server")
The
name parameter is used to identfy the client if multiple clients uses the same callback function.
The rest of the parameters can be given on creation, or by calling
mqtt.config() method.
user = "user_name" user name, default: ""
password = "password" password, default: ""
port = 1234 server port, integer, default: 1883 for nonsecure or 8883 for secure connection
autoreconnect = 60 timeout to reconnect if connection lost in seconds, if set to 0, does not reconnect, default: 0
clientid = "my_mqtt_ID" client ID, default: "mpy_mqtt_client"
cleansession = True clean session, default: False
keepalive = 60 keepalive interval in seconds, default: 120
qos = 1 QoS, default 0
retain = 1 retain, default: 0
secure = True use secure (SSL) connection, default: False
connected_cb = my_function callback function executed when connected to server, default: None
disconnected_cb = my_function callback function executed when disconnected from server, default: None
subscribed_cb = my_function callback function executed when the topic is succesfully subscribed, default: None
published_cb = my_function callback function executed when succesfully published to the topic, default: None
data_cb = my_function callback function executed when data received, default: None
connected,
disconnected and
subscribed callback functions have one input parameter, the client name.
published callback function has tuple input parameter with client name and publish type
data callback function has tuple input parameter with client name, topic's name and data received)
Example demonstrating all mqtt methods and callback functions:
Code: Select all
def conncb(task):
print("[{}] Connected".format(task))
def disconncb(task):
print("[{}] Disconnected".format(task))
def subscb(task):
print("[{}] Subscribed".format(task))
def pubcb(pub):
print("[{}] Published: {}".format(pub[0], pub[1]))
def datacb(msg):
print("[{}] Data arrived from topic: {}, Message:\n".format(msg[0], msg[1]), msg[2])
mqtts = network.mqtt("eclipse", "iot.eclipse.org", secure=True)
>>> I (2492310) [Mqtt client]: Starting Mqtt task
I (2492311) [Mqtt client]: Resolve dns for domain: iot.eclipse.org
I (2492326) [Mqtt client]: Connecting to server 198.41.30.241:8883,45858
I (2492455) [Mqtt client]: Creating SSL object...
I (2492457) [Mqtt client]: Start SSL connect..
I (2493728) [Mqtt client]: Connected!
I (2493729) [Mqtt client]: Connected to server iot.eclipse.org:8883
I (2493729) [Mqtt client]: Sending MQTT CONNECT message, type: 1, id: 0000
I (2493738) [Mqtt client]: Reading MQTT CONNECT response message
I (2493877) [Mqtt client]: Connected
I (2493877) [Mqtt client]: Connected to MQTT broker, creating sending thread before calling connected callback
I (2493881) [Mqtt client]: Sending task started
I (2493886) [Mqtt client]: mqtt_start_receive_schedule
>>> mqtts
Mqtt[eclipse](Server: iot.eclipse.org:8883, Status: Connected
Client ID: mpy_mqtt_client, Clean session=False, Keepalive=120 sec, QoS=0, Retain=False, Secure=True
Used stack: 2840/10240 + 1576/2048
)
>>> mqtts.config(subscribed_cb=subscb, published_cb=pubcb, data_cb=datacb)
>>> mqtts.subscribe("mipy_topic")
I (3341733) [Mqtt client]: Queue subscribe, topic"mipy_topic", id: 1
>>> I (3341919) [Mqtt client]: Subscribe successful
[eclipse] Subscribed
>>> mqtts.publish("other_topic","test from mpy")
I (3409081) [Mqtt client]: Published 45 bytes
I (3409084) [Mqtt client]: Queuing publish, length: 28, queue size(0/1024)
True
>>> [eclipse] Published: Sent
>>> mqtts.publish("mipy_topic","test from mpy, I'm subscribed to this topic")
I (3482472) [Mqtt client]: Published 102 bytes
I (3482475) [Mqtt client]: Queuing publish, length: 57, queue size(0/1024)
True
>>> [eclipse] Published: Sent
I (3482609) [Mqtt client]: deliver_publish
[eclipse] Data arrived from topic: mipy_topic, Message:
test from mpy, I'm subscribed to this topic
>>> mqtts.unsubscribe("mipy_topic")
I (3559931) [Mqtt client]: Queue unsubscribe, topic"mipy_topic", id: 2
>>>
>>> mqtts.status()
(1, 'Connected')
>>> mqtts.stop()
>>> I (3621157) [Mqtt client]: Closing client socket
I (3621162) [Mqtt client]: Client freed
>>> mqtts.status()
(4, 'Stopped')
>>> mqtts.free()
True
>>> mqtts.status()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: Mqtt client destroyed
>>> mqtts
Mqtt[eclipse]( Destroyed )
No debug messages are printed if you set lower CONFIG_MQTT_LOG_LEVEL using
menuconfig, the dafault is Error, here it is set to Info.
@pythoncoder
I hope this answers your question.