MQTT client#

Description#

For Python, we use the client library paho-mqtt. It is pretty easy to use and provides several examples to start with on their github repo.

A basic example that subscribes to /in and publishes to /out:

import paho.mqtt.client as mqtt

# connection callback
def on_connect(client, userdata, flags, rc):
   print("Connected with result code " + str(rc))

# message received callback
def on_message(client, userdata, msg):
   print(msg.topic + " " + str(msg.payload))
   client.publish("/out", "received an input...")

# set up the client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mqtt.eclipse.org", 1883, 60) # address ip, port number, keep alive

# subscribe
client.subscribe("/in")

# process the MQTT business
client.loop_forever()

Ressources#

The documentation of the paho-mqtt library can be found here.

The repo with our own clients can be used as a base to start.

Python MQTT