Reader small image

You're reading from  Raspberry Pi and MQTT Essentials

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803244488
Edition1st Edition
Right arrow
Author (1)
Dhairya Parikh
Dhairya Parikh
author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh

Right arrow

MQTT in Detail

This chapter touches on one of the main topics of this book: MQTT. As you saw in the previous chapter, when we used our Raspberry Pi as an MQTT client, it was straightforward and included just one command that needed to be executed. But the question is, how does the client connect to the broker, and how does it send the message to the intended client?

This chapter will address that exact point. It will significantly help you learn what MQTT is and how it works under the hood. You will gain a clear understanding of this communication protocol, along with the ability to use your laptops and computers as local MQTT clients, which is a bonus.

We’re going to cover the following main topics in this chapter:

  • Introducing MQTT clients
  • Understanding the MQTT protocol packet structure
  • Practical demonstration of MQTT in action

Let’s go ahead and begin.

Introducing MQTT clients

MQTT communication flow consists of a client (which can be a publisher or subscriber and in certain instances, both) and the broker, which manages the flow of all information across different clients. The following diagram provides an overview of how the MQTT message flow works:

Figure 2.1 – MQTT overview

As discussed earlier in this book, MQTT stands for Message Queuing Telemetry Transport. Simply put, it is a communication protocol designed for constrained devices with network limitations. It is designed as a lightweight publish/subscribe messaging protocol. But what does this mean? For this, we need to be familiar with the concepts of messages, topics, clients, and brokers. Let’s cover each and how they work.

MQTT messages

A message is a term given to the data that’s shared between different MQTT clients. It can be some text, sensor readings, and so on.

MQTT topics

Topics are one of the essential...

Understanding the MQTT protocol packet structure

MQTT control packets are how the data is managed within an MQTT network. For instance, as discussed in the previous section, when a client wants to connect to a broker, it sends a connect packet and, in response, gets a connack packet from the broker.

Similarly, when a client wants to publish something on a given topic, it sends a publish packet. When a client wants all the data arriving on a particular topic, it achieves that by subscribing to the specific topic using the subscribe packet.

In this section, we will provide a detailed discussion of each of the packets. Please note that we will be discussing MQTT v3.1.

Connect packet

The previous section mentions that parameters need to be passed along with a connect packet. The attributes of this message are as follows.

  • Fixed headers:

The fixed headers only contain the MQTT control packet type (which is the code for CONNECT); the remaining length will have...

Practical demonstration of MQTT in action

In this section, we will look at how the communication protocol works under the hood. For this, we will use an additional software called Wireshark, which lets us capture all the incoming and outgoing MQTT packets from our broker hosted on the Raspberry Pi.

Here, we will look at how a subscriber or a publisher connects to and sends the message through our MQTT broker. So, let’s get started:

  1. The first step is to install Wireshark on our Raspberry Pi. To do so, open your Terminal and type in the following command:
    sudo apt install wireshark 

It will ask you to confirm that you wish to install the package. Just type Y and press Enter. This will start the installation process. When this happens, you will see a configuration screen appear in front of you, as shown in the following screenshot. It will ask if you want to allow any user to have maximum privileges for the Wireshark application. I suggest that you choose No for...

Summary

We covered a lot of content about MQTT in this chapter. This will help you a lot in the upcoming chapters, especially the two projects that we will be creating since you will be aware of how devices are communicating with the Raspberry Pi’s MQTT broker.

In this chapter, we started by introducing the communication protocol, and we explored the main components or building blocks of MQTT. Next, we understood how the protocol works and covered some important control packets, which are the building blocks of MQTT.

Finally, we explored a practical scenario and understood how the actual communication happens under the hood by using a packet capturing software called Wireshark on our Raspberry Pi.

In the next chapter, we will cover some popular development boards manufactured by Espressif – the ESP8266-based NodeMCU development board and the ESP32 development board. We will demonstrate how to use these boards as an MQTT client and communicate with our Pi....

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022Publisher: PacktISBN-13: 9781803244488
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 €14.99/month. Cancel anytime

Author (1)

author image
Dhairya Parikh

Dhairya Parikh is an Electronics Engineer who currently works as a Data Engineer at Accenture. He has a year’s experience in building and maintaining data pipelines for a huge amount of data. In his free time, he builds IoT and Machine Learning projects and even writes about them. He has written several project articles for Circuit Cellar, which is a monthly tech magazine. He makes projects which positively impacts the society, making people’s life easier.
Read more about Dhairya Parikh