Reader small image

You're reading from  Hands-On MQTT Programming with Python

Product typeBook
Published inMay 2018
Reading LevelExpert
PublisherPackt
ISBN-139781789138542
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Gaston C. Hillar
Gaston C. Hillar
author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar

Right arrow

Using Command-Line and GUI Tools to Learn How MQTT Works

In this chapter, we will work with command-line and GUI tools to learn how MQTT 3.1.1 works in detail. We will learn MQTT basics, the specific vocabulary for MQTT, and its working modes. We will use different utilities and diagrams to understand the most important concepts related to MQTT. We will understand everything we need to know before writing Python code to work with the MQTT protocol. We will work with the different Quality of Service (QoS) levels and we will analyze and compare their overheads. We will gain an understanding of the following:

  • Subscribing to topics with a command-line tool
  • Subscribing to topics with a GUI tool
  • Publishing messages with a command-line tool
  • Publishing messages with a GUI tool
  • Unsubscribing from topics with a GUI tool
  • Learning best practices for topics
  • Understanding MQTT wildcards
  • Learning...

Subscribing to topics with a command-line tool

A drone is an IoT device that interacts with many sensors and actuators, including digital electronic speed controllers linked to engines, propellers, and servomotors. A drone is also known as an unmanned aerial vehicle (UAV), but we will definitely refer to it as a drone. Let's imagine that we have to monitor many drones. Specifically, we have to display their altitude and the speed for each of their servomotors. Not all of the drones have the same number of engines, propellers, and servomotors. We have to monitor the following types of drone:

Name Number of propellers
Quadcopter 4
Hexacopter 6
Octocopter 8

Each drone will publish its altitude every 2 seconds to the following topic: sensors/dronename/altitude, where dronename must be replaced by the name assigned to each drone. For example, the drone named octocopter01...

Subscribing to topics with a GUI tool

MQTT.fx is a GUI utility implemented with JavaFX that is available for Windows, Linux, and macOS. This tool allows us to connect with an MQTT server, subscribe to topic filters, see received messages, and publish messages to topics. You can download the appropriate version for your operating system from the downloads section of the main web page for this utility: http://www.mqttfx.org.

Now, we will use the MQTT.fx GUI utility to generate another MQTT client that subscribes to the same topic, sensors/octocopter01/altitude, and displays all the messages it receives. We will work with MQTT.fx version 1.6.0. Follow these steps:

  1. Launch MQTT.fx, select local mosquitto in the dropdown located at the upper-left corner, and click on the configuration icon at the right-hand side of this dropdown and at the left-hand side of the Connect button. MQTT...

Publishing messages with a command-line tool

We will use the mosquitto_pub command-line utility included in Mosquitto to generate a simple MQTT client that publishes a message to a topic. Open a Terminal in macOS or Linux, or a Command Prompt in Windows, go to the directory in which Mosquitto is installed, and run the following command:

mosquitto_pub -V mqttv311 -t sensors/octocopter01/altitude -m  "25 f" -d

The previous command will create an MQTT client that will establish a connection with the local MQTT server and then will make the client publish a message to the topic specified after the -t option: sensors/octocopter01/altitude. We specify the payload for the message after the -m option: "25 f". We specify the version of the MQTT protocol that we want to use when the client establishes the connection with -V mqttv311. This way, we indicate to the MQTT...

Publishing messages with a GUI tool

Now, we will use the MQTT.fx GUI utility to generate another MQTT client that publishes another message to the same topic, sensors/octocopter01/altitude. Follow these steps:

  1. Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
  2. Click Publish and enter sensors/octocopter01/altitude in the dropdown at the left-hand side of the Publish button.
  3. Enter the following text in the textbox below the Publish button: 32 f, as shown in the following screenshot:
  1. Then, click the Publish button. MQTT.fx will publish the entered text to the specified topic.

If you don't want to work with the MQTT.fx utility, you can run another mosquitto_pub command to generate another MQTT client that publishes a message to the topic. You just need to open another Terminal in macOS or Linux, or another Command Prompt in Windows...

Unsubscribing from topics with a GUI tool

Whenever we don't want a subscriber to receive more messages whose destination topic name matches one or more topic filters, the subscriber can send a request to unsubscribe to a list of topic filters to the MQTT server. Obviously, unsubscribing from topic filters is the opposite of subscribing to topic filters. We will use the MQTT.fx GUI utility to unsubscribe the MQTT client from the sensors/octocopter01/altitude topic. Follow these steps:

  1. Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
  2. Click Subscribe.
  1. Click on the panel that displays the sensors/octocopter01/altitude topic name on the left-hand side of the window. Then, click on the Unsubscribe button located in this panel. The following screenshot shows this button:
  1. MQTT.fx will unsubscribe the client from the sensors/octocopter01...

Learning best practices for topics

We already know that MQTT allows us to publish messages on topics. A publisher always has to specify the topic name to which a message will be published. The easiest way to understand topic names in MQTT is to think about them as paths in a file system.

If we have to save dozens of files that have information about different types of sensor for a diverse number of drones, we can create a hierarchy of directories or folders to organize all the files that we will save. We can create a directory named sensors, then one sub-directory for each drone, such as octocopter01, and finally a sub-directory with the sensor name, such as altitude. The path in macOS or Linux will be sensors/octocopter01/altitude because these operating systems use a forward slash (/) as a delimiter. In Windows, the path will be sensors\drone\altitude because this operating...

Understanding MQTT wildcards

When we analyzed the subscription operation, we learned that an MQTT client can subscribe to one or more topic filters. If we specify a topic name as a topic filter, we will only subscribe to a single topic. We can take advantage of the following two wildcards to create topic filters that subscribe to all the topics that match the filter:

  • Plus sign (+): This is a single-level wildcard that matches any name for a specific topic level. We can use this wildcard instead of specifying a name for any topic level in the topic filter.
  • Hash (#): This is a multilevel wildcard that we can use only at the end of a topic filter, as the last level, and it matches any topic whose first levels are the same as the topic levels specified at the left-hand side of the # symbol.

For example, if we want to receive all the messages related to altitude for all the drones...

Learning about the different QoS levels

Now that we understand how connection, subscription, and publication work in combination with topic names and topic filters with wildcards, we can dive deep into the QoS levels. So far, we have analyzed how both subscription and publication work with a QoS level equal to 0. Now, we will understand what this number means and how things work when we use the other available QoS levels for publication and subscription.

Remember that publication involves publishing from the MQTT client to the MQTT server and then from the server to the subscribed client. It is very important to understand that we can publish with a QoS level and subscribe with another QoS level. Hence, there is a QoS level for the publish process between the publisher and the MQTT server and another QoS level for the publish process between the MQTT server and the subscriber...

Working with at least once delivery (QoS level 1)

First, we will use wildcards to subscribe to a topic filter with QoS level 1, and then we will publish one message to a topic name that will match the topic filter with QoS level 1. This way, we will analyze how both publishing and subscription work with QoS level 1.

We will use the mosquitto_sub command-line utility included in Mosquitto to generate a simple MQTT client that subscribes to a topic filter with QoS level 1 and prints all the messages it receives. Open a Terminal in macOS or Linux, or a Command Prompt in Windows, go to the directory in which Mosquitto is installed, and run the following command:

mosquitto_sub -V mqttv311 -t sensors/+/altitude -q 1 -d

The previous command will create an MQTT client that will establish a connection with the local MQTT server and then will make the client subscribe to the topic filter...

Working with exactly once delivery (QoS level 2)

First, we will use wildcards to subscribe to a topic filter with QoS level 2, and then we will publish one message to a topic that will match the topic filter with QoS level 2. This way, we will analyze how both publishing and subscription work with QoS level 2.

We will use the mosquitto_sub command-line utility included in Mosquitto to generate a simple MQTT client that subscribes to a topic filter with QoS level 1 and prints all the messages it receives. Open a Terminal in macOS or Linux, or a Command Prompt in Windows, go to the directory in which Mosquitto is installed, and run the following command:

mosquitto_sub -V mqttv311 -t sensors/quadcopter30/# -q 2 -d

The previous command will create an MQTT client that will establish a connection with the local MQTT server and then will make the client subscribe to the topic filter...

Understanding overhead in the different Quality of Service levels

The following diagram summarizes the different packages that are exchanged between an MQTT client and an MQTT server to publish a message with QoS levels 0, 1, and 2. This way, we can easily recognize the increased overhead as we increase the QoS level:

It is very important to take into account the additional overhead required by QoS level 2 and to use it only when it is really necessary.

Test your knowledge

Let's see whether you can answer the following questions correctly:

  1. QoS level 0 for MQTT means:
    1. Exactly once delivery
    2. At most once delivery
    3. At least once delivery
  2. QoS level 1 for MQTT means:
    1. Exactly once delivery
    2. At most once delivery
    3. At least once delivery
  3. QoS level 2 for MQTT means:
    1. Exactly once delivery
    2. At most once delivery
    3. At least once delivery
  4. If the application isn't able to tolerate duplicates and we have to make sure that the messages reach the subscribers only once, the appropriate choice is:
    1. QoS level 0
    2. QoS level 1
    3. QoS level 2
  5. Which QoS level has the highest overhead:
    1. QoS level 0
    2. QoS level 1
    3. QoS level 2

The rights answers are included in the Appendix, Solutions.

Summary

In this chapter, we worked with different tools to interact with the Mosquitto MQTT 3.1.1 server we installed in Chapter 1, Installing an MQTT 3.1.1 Mosquitto Server. We worked with an unsecured MQTT server to easily understand the interaction between the MQTT clients and the MQTT server.

We subscribed to topics via the command-line and GUI tools. Then, we published messages with QoS level 0 and we unsubscribed from topics. We learned best practices related to topics; and single-level, and multilevel wildcards. We studied in detail the different Quality of Service levels supported by MQTT and when it is appropriate to use each of them. We analyzed their advantages and disadvantages.

Now that we understood how the MQTT 3.1.1 basics work, we will learn how to secure an MQTT server and to follow best practices related to security, which are the topics that we are going to...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On MQTT Programming with Python
Published in: May 2018Publisher: PacktISBN-13: 9781789138542
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gaston C. Hillar

Gaston C. Hillar is Italian and has been working with computers since he was 8 years old. Gaston has a Bachelor's degree in computer science (graduated with honors) and an MBA. Currently, Gaston is an independent IT consultant and a freelance author who is always looking for new adventures anywhere in the world. He was a senior contributing editor at Dr. Dobb's, and has written more than a hundred articles on software development topics. He has received the prestigious Intel Black Belt Software Developer award eight times. He has written many articles about Java for Oracle Java Magazine. Gaston was also a former Microsoft MVP in technical computing. He lives with his wife, Vanesa, and his two sons, Kevin and Brandon.
Read more about Gaston C. Hillar