Working MyQttHub.com Python Paho examples


#1

Hello.

Here are some examples to connect with https://myqtthub.com using Python Paho MQTT library. We will be expanding them to cover more cases.

Connect and PUBLISH without using TLS

Though not recommended (because your credentials and information will travel insecure through the network), here is how to connect and PUBLISH using python paho mqtt client.

  1. First, be sure you have paho-client library installed. See https://pypi.org/project/paho-mqtt/ We installed it with:

pip install paho-mqtt

  1. Then, create one or several credentials using (https://support.asplhosting.com/t/how-to-create-and-manage-your-mqtt-devices-with-myqtthub-com ) and taking into account (https://support.asplhosting.com/t/connecting-several-devices-with-same-credentials-how-to-avoid-mqtt-connection-replace).

Then, use the following working example:

#!/usr/bin/python

import paho.mqtt.client as mqtt
import time
import ssl

host          = "node02.myqtthub.com"
port          = 1883
clean_session = True
client_id     = "<device-client-id>"
user_name     = "<device-user-name>"
password      = "<device-password>"

def on_connect (client, userdata, flags, rc):
    """ Callback called when connection/reconnection is detected """
    print ("Connect %s result is: %s" % (host, rc))
    
    # With Paho, always subscribe at on_connect (if you want to
    # subscribe) to ensure you resubscribe if connection is
    # lost.
    # client.subscribe("some/topic")

    if rc == 0:
        client.connected_flag = True
        print ("connected OK")
        return
    
    print ("Failed to connect to %s, error was, rc=%s" % rc)
    # handle error here
    sys.exit (-1)


def on_message(client, userdata, msg):
    """ Callback called for every PUBLISH received """
    print ("%s => %s" % (msg.topi, str(msg.payload)))

# Define clientId, host, user and password
client = mqtt.Client (client_id = client_id, clean_session = clean_session)
client.username_pw_set (user_name, password)

client.on_connect = on_connect
client.on_message = on_message

# configure TLS connection
# client.tls_set (cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2)
# client.tls_insecure_set (False)
# port = 8883

# connect using standard unsecure MQTT with keepalive to 60
client.connect (host, port, keepalive = 60)
client.connected_flag = False
while not client.connected_flag:           #wait in loop
    client.loop()
    time.sleep (1)

# publish message (optionally configuring qos=1, qos=2 and retain=True/False)
ret = client.publish ("some/message/to/publish", "{'status' : 'on'}")
client.loop ()

print ("Publish operation finished with ret=%s" % ret)

# close connection
client.disconnect ()

This code will connect and setup an authenticated session by using:

# Define clientId, host, user and password
client = mqtt.Client (client_id = client_id, clean_session = clean_session)
client.username_pw_set (user_name, password)

At the same time, we use the following code to keep on waiting until connect ACK is received:

# connect using standard unsecure MQTT with keepalive to 60
client.connect (host, port, keepalive = 60)
client.connected_flag = False
while not client.connected_flag:           #wait in loop
    client.loop()
    time.sleep (1)

Finally we PUBLISH and DISCONNECT using:

# publish message (optionally configuring qos=1, qos=2 and retain=True/False)
ret = client.publish ("some/message/to/publish", "{'status' : 'on'}")
client.loop ()

print ("Publish operation finished with ret=%s" % ret)

# close connection
client.disconnect ()

How to enable TLS/SSL protection for paho mqtt client and MyQttHub

Just uncomment the following lines from the example:

# configure TLS connection
client.tls_set (cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2)
client.tls_insecure_set (False)
port = 8883

They will configure client to require certificate, standard MQTT-TLS port and tls_insecure_set(False) to ensure certificate verification is done.