Last active
July 11, 2019 15:42
-
-
Save vpetersson/56df7c0302372db0fe5f745f77271ea7 to your computer and use it in GitHub Desktop.
Example code for how to use Paho to connect to an MQTT server (such as Mosquitto).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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