Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi and MQTT Essentials

You're reading from   Raspberry Pi and MQTT Essentials A complete guide to helping you build innovative full-scale prototype projects using Raspberry Pi and MQTT protocol

Arrow left icon
Product type Paperback
Published in Sep 2022
Publisher Packt
ISBN-13 9781803244488
Length 272 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Dhairya Parikh Dhairya Parikh
Author Profile Icon Dhairya Parikh
Dhairya Parikh
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1:Covering the Basics
2. Chapter 1: Introduction to Raspberry Pi and MQTT FREE CHAPTER 3. Chapter 2: MQTT in Detail 4. Chapter 3: Introduction to ESP Development Boards 5. Chapter 4: Node-RED on Raspberry Pi 6. Part 2: Practical Implementation – Building Two Full-Scale Projects
7. Chapter 5: Major Project 1: IoT Weather Station 8. Chapter 6: Major Project 2: Smart Home Control Relay System 9. Part 3: How to Take Things Further – What Next?
10. Chapter 7: Taking Your MQTT Broker Global 11. Chapter 8: Project Prototype to Product – How? 12. Index 13. Other Books You May Enjoy

What is MQTT and how does it work?

In this section, we will learn about the essential concepts of MQTT. First, we will look at the basic concepts of MQTT and some history, followed by the functionality and components of MQTT. Finally, we will have a brief encounter with the salient features of MQTT.

Please note that there are different versions of MQTT, and most of what we discuss is relevant to the MQTT protocol version 3.1.

What is MQTT?

MQTT stands for Message Queuing Telemetry Transport. It is a lightweight communication protocol.

According to the official MQTT v3.1 documentation:

MQTT is a Client-Server publish/subscribe messaging transport protocol. It is lightweight, open, simple, and designed to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required, and network bandwidth is at a premium.

This is a clear and clean definition of the MQTT protocol in just a few lines. It is a messaging protocol designed for easy implementation, primarily client side. It is an open and lightweight communication protocol with minimal packet overhead. It is generally used for communication between two or more devices.

Basic concepts of MQTT

Now that we know what MQTT is, we will explore the basic concepts of MQTT we came across in the previous section. More specifically, we will look at MQTT as a publish/subscribe protocol.

What exactly is a publish/subscribe protocol?

The publish/subscribe protocol is an alternative to the traditional client-server architecture. It means that instead of categorizing both sending and receiving machines as clients, the clients who send a message are publishers and the clients who receive the messages are the subscribers.

Another essential feature of such protocols is the decoupling between the clients. In simple words, the clients never directly communicate with each other. They are mediated by the third component of this system, known as the broker. In this book, we will be using our Raspberry Pi as an MQTT broker, which connects different client devices within a local network. The primary function of the broker is to mediate and manage all communications between the various clients (i.e., publishers and subscribers).

To better understand how the whole system works, please see Figure 1.1, which shows how the communication protocol operates with a very simplified diagram. In this example, the publishing client is a temperature monitor and the subscribing device is a mobile phone:

Figure 1.1 – Basic MQTT communication flow

Figure 1.1 – Basic MQTT communication flow

Please note that this is just a simplified representation. There can be multiple publishers and subscribers connected to a single broker. As you can see, the temperature monitor sends the current temperature value of 27°C through the MQTT communication protocol, which is then received by the MQTT broker, which routes it to the subscriber, a mobile application in our case.

We will now look into some details about publishers, subscribers, and brokers with the help of an example:

  • Publishers: These devices or machines are responsible for sending the collected data to the brokers. For instance, if you have an air quality monitoring system that monitors the CO2 levels in the air every 30 seconds, the device will be set to publish the CO2 concentration every 30 seconds.
  • Subscribers: These devices receive the requested sensor data from the brokers. Considering the preceding example, an air purifier can be a subscriber of our air quality monitoring system. It constantly receives the CO2 concentration values, and when it crosses a threshold value, the purifier automatically turns on.
  • Broker: This intermediary device connects various publishers and subscribers by managing and routing the data. We will be using Raspberry Pi as a broker for the entirety of this book.

Please note that both the publishers and subscribers are referred to as clients. A client can be a publisher, subscriber, or both as both these processes are entirely independent of each other, as we will see in the later chapters.

But another question arises now: how does the broker manage or route which information is sent where? The following section will answer this question, exploring MQTT functionality.

Functionality and components of MQTT

We have already seen the significant components of MQTT, but we will now explore how these components communicate with each other.

MQTT has no client device addresses or identifiers, making it easy to build an expansible, ad hoc network. The only thing all clients must know is the address of the broker. So, how do messages get routed between the clients? The solution for this is topics and messages.

This is how the whole system works:

  1. First, the publisher sends the data collected to the broker on a particular topic, which is similar to a channel for data transmission and reception. Please note that a topic can have several subtopics too. For example, in an application where you send the temperature data from a sensor connected to your fridge, the topic will look something like this:

Kitchen/Fridge/

The main topic is the kitchen, and the appliance is the subtopic. The message will be Temperature:14 on the given topic.

  1. The subscribers listen to the topic. So, if the subscriber is listening to the Kitchen topic, it will have access to all the subtopics that are a part of this topic.
  2. The primary function of the broker is to manage all the available topics and route the information according to the type of client, namely publishers and subscribers.

Now that we are aware of the details of MQTT, we will have a look at the salient features of this communication protocol.

Salient features of MQTT

This section will cover the main features of this communication protocol:

  • Lightweight and efficient:

MQTT clients are tiny, and they require minimal resources to operate. So, even microcontrollers such as ESP8266 can be used as a client as long as they have an active connection to a network.

This protocol is highly efficient thanks to the small message headers that provide maximum network bandwidth efficiency.

  • Bidirectional communication protocol

MQTT allows to-and-fro messaging capability. This means a device can be a publisher and a subscriber simultaneously. This also allows easy broadcasting of messages to several devices at once. 

  • Highly scalable:

There is no worry about maintaining clients’ addresses or IDs; it is effortless to expand the MQTT network. Moreover, the decoupling between the publishers and subscribers makes things even more accessible. The only things required on the client side are the broker’s IP address and the topic name

  • Reliability:

MQTT is highly reliable when it comes to message delivery. As this is an essential aspect of any communication protocol, MQTT comes with three predefined quality of service (QoS) levels:

  • QoS 0: At most once
  • QoS 1: At least once
  • QoS 2: Exactly once
  • Support for unreliable networks:

Many IoT devices are connected over unreliable networks, and MQTT’s support for persistent sessions reduces the client’s time with the broker. For example, several monitoring devices are deployed on moving vehicles or in remote areas such as forests.

  • Highly secure:

MQTT makes it easy to encrypt messages using TLS and authenticate clients using modern authentication protocols, such as OAuth.

This is the end of this section. We covered the basics of MQTT and the components and salient features of this popular communication protocol. This protocol will be discussed in detail in Chapter 2,MQTT in Detail, of this book.

You have been reading a chapter from
Raspberry Pi and MQTT Essentials
Published in: Sep 2022
Publisher: Packt
ISBN-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.
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 $19.99/month. Cancel anytime
Banner background image