Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for how to use Paho to connect to an MQTT server (such as Mosquitto).
#!/usr/bin/env python
import paho.mqtt.client as mqtt
def on_connect(client, obj, flags, rc):
print("rc: " + str(rc))
def on_message(client, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_subscribe(client, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.tls_set(
ca_certs='/opt/wott/certs/ca.crt',
certfile='/opt/wott/certs/client.crt',
keyfile='/opt/wott/certs/client.key'
)
# disables peer verification
# client.tls_insecure_set(True)
# Note that you need to map x.d.wott.local to the IP to the MQTT server
# in /etc/hosts as the hostname must match the CN in the certificate.
client.connect('x.d.wott.local', 8883, 60)
client.subscribe('wott/temperature', 0)
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.