MQTT With PYTHON — Part 1

Ashiq KS
Ashiq KS
Dec 1, 2018 · 4 min read

A series on the lightweight protocol MQTT with the Python programming language.

This post is the beginning of the 3 part series on MQTT with Python. And the parts are as follows:

  1. Part 1- Introduction to MQTT and its implementation using an open source implementation called Mosquitto.
  2. Part 2 - Introduction to paho-mqtt-python, a python interface for MQTT implementation.
  3. Part 3 - A simple real world example with paho-mqtt utilizing the type class of Python

MQTT- The Messenger

MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. MQTT stands for Message Queuing Telemetry Transport.

It is a publish-subscribe-based messaging protocol. It works on top of the TCP/IP protocol.

The important terminologies in MQTT are as follows:

  1. Publisher: The one which publishes messages to the outer world.
  2. Subscriber: The one which receives messages that are intended for it.
  3. Client: A client can be either publisher or subscriber or both. That is a client publish a message and receive another message at the same time.
  4. Server/Broker: The one receives the messages published by the publisher first, even before the subscriber. Then the server publishes the messages to the subscribers after filtering the messages. Both the names server and broker mean the same entity.
  5. Topic: An UTF-8 string used by the clients and servers to send and receive messages. Eg: sensors/altimeter/1.

How does MQTT work?

First of all a subscriber subscribes to one or more topics. Then one or more publishers publish message to a server, a server can be anywhere or even be the local host, on a specific topic. Then the server publishes the message to the subscribers which have subscribed to the topic specified by the publishers.

As seen in the above image, the laptop and the mobile device are the subscribers and the temperature sensor is the publisher. The temperature sensor publishes it temperature value to the broker and the broker, then, publishes the temperature value to the laptop and mobile device.

Installing MQTT

We will use the open source implementation, called Mosquitto. It is a broker that implements the MQTT protocol. There are a lot of other brokers available, either open source and paid. And in this series we will use Ubuntu Linux 18.04 LTS from Google Cloud.

Type in the command prompt as follows:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

After that run the update command sudo apt-get update. Then install Mosquitto as follows:

sudo apt-get install mosquitto

Now, install the mosquitto clients.

sudo apt-get install mosquitto-clients

To check mosquitto is successfully installed, run the following command.

sudo service mosquitto service

And it will show similar to the following output if mosquitto is successfully installed.

Mosquitto Clients — Subscribers & Publishers

Now we will get into how to subscribe and publish messages using command line tools.

Enter in the command prompt as follows to subscribe to the topic ‘sensors/altimeter/1

mosquitto_sub -V mqttv311 -t sensors/altimeter/1 -d

-V determines the mqtt version we are using, here we use 3.1.1. Then the topic comes after the -t. -d means debug mode, so we can view what happens under the hood.

Here we have used our localhost. If we want to use any other server we can do it by me including -h followed by the IP address or name of the server.

The output is as follows:

Since we haven’t published any messages to this topic there are no messages shown yet. Now we will publish a message to the topic defined earlier. Let the message be ‘100 feet’. Open another command prompt and type in as follows:

mosquitto_pub -V mqttv311 -t sensors/altimeter/1 -m "100 feet" -d

The difference from subscribing is instead of mosquitto_sub we have used mosquitto_pub and we have an additional -m to denote the message.

The output will be as follows:

After sending the message, it got disconnected. Now we will check our subscriber has received the message or not. Take the subscriber and check out.

We can see our subscriber has successfully received the message sent by the publisher under the topic ‘sensors/altimeter/1’.

It is this easy to get started off with MQTT and in the Part 2 and Part 3 we will use Python to subscribe and publish messages. Please comment below your thoughts and suggestion.

Ashiq KS

Written by

Ashiq KS

Machine Learning & Cloud Technophile

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade