MQTT and Python For Beginners -Tutorials

paho-mqtt-python-clientThis is a collection of tutorials forming a course  for complete beginners starting to use the Paho Python MQTT client in their projects.

The course consists of a series of tutorials, videos and examples that take you through the basics of using the Paho Python MQTT client.

At the end of the course you should have a very good understanding of the Python MQTT client and be in a position to create your own scripts using it.

Many of the demo scripts are available to download for many of the examples.

Important Note feb 2024: The MQTT Python client has been upgraded to version 2.The code examples were written in version 1.5 . The change appears only to affect the callbacks which now have a properties field. This is for MQTTv5 compliance. You may need to add this to the callbacks in the examples. Tks matt.

Paho MQTT Python Client –

This client is provided by the Eclipse foundation and it the one used in this course.

Tutorial 1Introduction to the Paho Python MQTT Client

This introductory tutorial covers the Install, core class methods and we end the tutorial with a simple publish and subscribe script.

Tutorial 2Introduction to the Client Class

In the next tutorial we look deeper into the Client class and look at client names (ids), auxiliary functions and settings like will_set().

We also look at some of the class modifications I make and also at sub classing.

Tutorial 3Connecting to a Broker – MQTT uses the TCP/IP protocol which requires a connection between the end-points. In this case between the MQTT client and the MQTT broker or server.

We look at the connection method in details including detecting both a successful connection and a failed one.

Tutorial 4Publishing Using The Paho Python MQTT Client -In this tutorial we take a more detailed look at the publish class and publish acknowledgements.

We also look at publishing using SSL and Websockets.

Tutorial 5Subscribing using The Paho Python Client – A more detailed look at the subscribe method and how to subscribe to multiple topics.

We also look at QOS levels on subscribe, processing acknowledgements, clean sessions and retained messages.

Tutorial 6 Receiving Messages with the Paho MQTT Python Client A more detailed look at the on_message callback and how to move messages to the main loop.

Tutorial 7Understanding The Loop – The loop is essential for processing callbacks. The MQTT client has several loop methods and we look at all these methods as well as working with multiple loops.

Tutorial 8 Understanding Callbacks -Callbacks are functions that are called in response to an event. They are used for detecting acknowledgements as well as receiving messages.

They are an essential part of the Client and probably the most least understood.

Tutorial 9Handling Multiple Client Connections

It is common to require several connections either to a single broker or to several brokers.

There a several ways of achieving this and we will examine two ways of doing it.


MQTT Python Kindle Book

If you prefer all of my MQTT pythons tutorials all in one place then you might be interested in my Kindle Book.
Working with the Paho Python MQTT Client

mqtt-python-book

Comments and Questions

I would be very grateful for any feedback and please don’t forget to rate the course.

Please rate? And use Comments to let me know more

11 comments

  1. hi, if i have to subscribe the json format data from mqtt server and push it to elastic search as new document, may i get the help with the code

  2. Hey Steve,
    For some reason I can’t connect to my local mosquitto server from another pc in my house. I’ve done some basic troubleshooting and opened port 1883. Can you think of what else might need to be done? Am I missing some configuration?

    Thanks
    Andy

    1. If it is mosquitto v2 then it need a basic config file containing
      listener 1883
      allow_anonymous true

      Rgds
      Steve

  3. Hey Steve, thanks for the information. Here are a couple of python scripts I made to do screen sharing via mosquitto. These additional packages are required and some may need to be added to the path env variable.
    python -m pip install –upgrade opencv-contrib-python
    python -m pip install –upgrade –user mss
    python -m pip install –upgrade numpy

    #server
    import paho.mqtt.client as mqtt
    import time
    import uuid
    import cv2
    import mss
    from mss.tools import zlib
    import numpy
    import base64
    import io
    import pickle

    quit = False
    capture = False

    def on_connect(client, userdata, flags, rc):
    print(“Connected flags ” + str(flags) + ” ,result code=” + str(rc))

    def on_disconnect(client, userdata, flags, rc):
    print(“Disconnected flags ” + str(flags) + ” ,result code=” + str(rc))

    def on_message(client, userdata, message):
    global quit
    global capture
    global last_image

    if message.topic == “server/size”:
    with mss.mss() as sct:
    sct_img = sct.grab(sct.monitors[0])
    size = sct_img.size
    client.publish(“client/size”, str(size.width) + “|” + str(size.height))

    if message.topic == “server/update/first”:
    with mss.mss() as sct:
    sct_img = sct.grab(sct.monitors[0])
    image = numpy.array(sct_img)
    b64img = base64.b64encode(zlib.compress(pickle.dumps(image), 9))
    client.publish(“client/update/first”, b64img)
    print(“Source Image Size=” + str(len(sct_img.rgb)))
    print(“Compressed Image Size=” + str(len(b64img)) + ” bytes”)
    last_image = image

    if message.topic == “server/update/next”:
    with mss.mss() as sct:
    sct_img = sct.grab(sct.monitors[0])
    image = numpy.array(sct_img)
    xor_image = image ^ last_image
    b64img = base64.b64encode(zlib.compress(pickle.dumps(xor_image), 9))
    client.publish(“client/update/next”, b64img)
    print(“Compressed Image Size=” + str(len(b64img)) + ” bytes”)
    last_image = image

    myid = str(uuid.uuid4()) + str(time.time())
    print(“Client Id = ” + myid)
    client = mqtt.Client(myid, False)
    client.on_connect = on_connect
    client.on_disconnect = on_disconnect
    client.on_message = on_message
    try:
    client.connect(“127.0.0.1”)
    client.loop_start()
    client.subscribe(“server/size”)
    client.subscribe(“server/update/first”)
    client.subscribe(“server/update/next”)
    while not quit:
    time.sleep(5)
    continue
    client.loop_stop()
    client.disconnect()
    except:
    print(“Could not connect to the Mosquito server”)

    #client
    import paho.mqtt.client as mqtt
    import time
    import uuid
    import cv2
    import mss
    from mss.tools import zlib
    import numpy
    import base64
    import io
    import pickle

    quit = False
    size = False
    capture = False
    width = 0
    height = 0
    last_image = None

    def on_connect(client, userdata, flags, rc):
    print(“Connected flags ” + str(flags) + ” ,result code=” + str(rc))

    def on_message(client, userdata, message):
    global quit
    global size
    global capture
    global width
    global height
    global last_image

    if message.topic == “client/size”:
    if width == 0 and height == 0:
    strsize = message.payload.decode(“utf-8”)
    strlist = strsize.split(“|”)
    width = int(strlist[0])
    height = int(strlist[1])
    size = True

    if message.topic == “client/update/first”:
    image = pickle.loads(zlib.decompress(base64.b64decode(message.payload.decode(“utf-8”)), 15, 65535))
    cv2.imshow(“Server”, image)
    if cv2.waitKeyEx(25) == 113:
    quit = True
    capture = False
    last_image = image;

    if message.topic == “client/update/next”:
    xor_image = pickle.loads(zlib.decompress(base64.b64decode(message.payload.decode(“utf-8”)), 15, 65535))
    image = last_image ^ xor_image
    cv2.imshow(“Server”, image)
    if cv2.waitKeyEx(25) == 113:
    quit = True
    capture = False
    last_image = image

    myid = str(uuid.uuid4()) + str(time.time())
    print(“Client Id = ” + myid)
    client = mqtt.Client(myid,False)
    client.on_connect = on_connect
    client.on_message = on_message
    try:
    client.connect(“127.0.0.1”)
    client.loop_start()
    client.subscribe(“client/size”)
    client.subscribe(“client/update/first”)
    client.subscribe(“client/update/next”)
    while not size:
    client.publish(“server/size”, None)
    time.sleep(1)

    first_image = True
    while not quit:
    if capture == False:
    capture = True
    if first_image:
    client.publish(“server/update/first”)
    first_image = False
    else:
    client.publish(“server/update/next”)
    time.sleep(.1)

    cv2.destroyAllWindows()
    client.loop_stop()
    client.disconnect()
    except:
    print(“Could not connect to the Mosquito server”)

  4. hi, im new to paho mqtt and i have a project to do. i need to use mqtt to publish and subscribe to an IoT device. the data will be displayed on Python-Django web dashboard. Am i able to use Arduino IDE for my codes and then connect it to the dashboard using mqtt? How does it work?

  5. Hello,

    With paho-mqtt, is there a way to use two Linux SBCs to communicate online via a broker? Also, just to make sure I am not “barking up the wrong tree,” I want to use my two Linux SBCs to communicate to one another while I send commands to both of them at separate intervals via some protocol.

    Seth

    1. Don’t see any reason why not providing you can run Python or nodejs on the machines is you are using the python or nodjs clients. For C client you will nee to compile from source.
      Rgds
      Steve

Leave a Reply

Your email address will not be published. Required fields are marked *