This post is the beginning of the 3 part series on MQTT with Python. And the parts are as follows:
- Part 1- Introduction to MQTT and its implementation using an open source implementation called Mosquitto.
- Part 2 - Introduction to paho-mqtt-python, a python interface for MQTT implementation.
- 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:
- Publisher: The one which publishes messages to the outer world.
- Subscriber: The one which receives messages that are intended for it.
- 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.
- 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.
- 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.
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’.