A Complete Guide for MQTT Protocal

A Complete Guide for MQTT Protocal

17 September 2023 0 By Anshul Pal

In this article we will see A Complete Guide for MQTT Protocal. It is a machine-to-machine protocol designed for the Internet of Things (IoT). It operates efficiently with low bandwidth usage, enabling IoT devices to interact seamlessly while requiring minimal coding and imposing a minimal burden on network resources. This article provides insights into how MQTT functions, underscores its significance, and delves into its prominent applications in the year 2023.

Your Queries

  • What is MQTT
  • History of MQTT
  • How MQTT Works
  • What are MQTT Components
  • Why is MQTT Protocal Important
  • How can AWS support your MQTT implementation
  • MQTT Client and Broker
  • Drawbacks of MQTT
  • Installing and Configuring MQTT Broker on Raspberry Pi

What is MQTT?

MQTT, short for Message Queuing Telemetry Transport, is a super important way for machines to talk to each other, especially in the Internet of Things (IoT) world. It’s like a special language they use to chat. What’s cool about MQTT is that it’s very lightweight and doesn’t hog up a lot of the internet’s space, which is great when you’re in places with slow or limited internet. Think of it as a language that helps machines talk without using too much internet power. It’s like having a smooth conversation between different devices, and they can all be talkers and listeners at the same time. So, they can share information easily.

In simple terms, MQTT helps gadgets in the IoT world communicate efficiently. It’s like a secret code they use to chat quickly and quietly, which is perfect when the internet is slow or not very strong. Imagine it as a language that lets devices have smooth conversations with each other, and they can all talk and listen together. This makes sharing information between devices really easy.

History of MQTT

MQTT, a clever way for machines to talk to each other, was created by Dr. Andy Stanford-Clark from IBM and Arlen Nipper. It used to be available under MQTT ORG, and the older versions were 3.1 and 3.1.1. Then, in 2014, it got officially published by OASIS, which became its new home for development. OASIS kept making it better, and version 3.1.1 was like an updated version of 3.1 with some small changes, like fixing how devices connect and making things clearer.

But now, the latest version is 5.0, and it’s like a big upgrade from 3.1.1. However, it’s not backward compatible, meaning old stuff might not work with it. Version 5.0 has lots of cool features that make it even better for making machines talk to each other smoothly. So, it’s like getting a shiny new tool to help machines chat and share information.

How MQTT Works

MQTT comprises two key elements: the MQTT broker and MQTT clients. The broker actively facilitates message exchange between senders and recipients, while MQTT clients play active roles by either publishing messages to the broker or subscribing to receive them. MQTT messages include topics for categorization, and clients actively choose the topics they wish to engage with.

The MQTT broker effectively employs topics and subscriber lists to efficiently distribute messages to the right clients. When a subscribing client loses connection, the broker buffers messages for future delivery upon reconnection. In case of an abrupt disconnection between a publishing client and the broker, the broker actively intervenes, closing the connection, and sending cached messages with publisher instructions to subscribers.

MQTT’s publish/subscribe (pub/sub) model maximizes bandwidth utilization, offering an alternative to traditional client-server communication. This model distinctly separates message senders (publishers) from recipients (subscribers). Intermediaries known as brokers actively manage communication flow between publishers and subscribers.

MQTT clients actively encompass both publishing and subscribing roles, making this communication framework adaptable to various devices and applications, from microcontrollers like Arduino to cloud-hosted application servers, all efficiently utilizing MQTT libraries for interactions within this dynamic ecosystem.

What are MQTT Components

In MQTT, there are fundamental concepts to grasp:

  1. Broker: The broker acts as a server responsible for distributing information to connected clients. It’s the central component in the publish/subscribe protocol and can efficiently handle numerous concurrently connected MQTT clients.
  2. Client: Clients are devices that connect to the broker to send or receive information. These MQTT clients can range from small microcontrollers to fully-fledged servers, all equipped with an MQTT library and linked to the MQTT Broker via various networks.
  3. Topic: Messages traverse from a publisher, through the broker, to one or more subscribers via topics. Topics are organized as hierarchical UTF-8 strings. Clients either publish messages to topics or subscribe to them. In essence, topics determine where messages are published and who registers interest in receiving them.
  4. Publish: Publishers are clients that transmit information to the broker, which then distributes it to interested clients based on the specified topic name.
  5. Subscribe: Clients inform the broker about the topics they want to follow. When a client subscribes to a topic, any message published to the broker related to that topic is delivered to all subscribers. Clients can also unsubscribe to cease receiving messages on a particular topic.
  6. QoS (Quality of Service): Each connection can specify a quality of service level to the broker, denoted by an integer value ranging from 0 to 2. It’s important to note that QoS settings do not impact the handling of TCP data transmissions but rather influence how MQTT clients interact in terms of message delivery.

Why is MQTT Protocal Important

In other words, the MQTT protocol has become a standard choice for transmitting data in the Internet of Things (IoT) because it offers the following advantages:

  1. Efficiency and Lightweight: MQTT is designed for high efficiency, demanding minimal computing power or memory. It can effectively operate on small IoT devices with limited resources. For example, a basic MQTT message can consist of only two data bytes, and the message headers are also compact, enabling optimized utilization of network bandwidth.
  2. Scalability: MQTT requires minimal code and consumes very little power during operations. Additionally, it has built-in capabilities to handle communication with a large number of IoT devices. This means that you can use MQTT to connect with a vast array of IoT devices, making it highly scalable.
  3. Reliability: Many IoT devices operate in challenging conditions with unreliable cellular networks, which may have low bandwidth and high latency. MQTT addresses these challenges by offering features that minimize the time it takes for an IoT device to reconnect with the cloud. It also provides three different levels of message delivery quality to ensure reliability for various IoT use cases: at most once (0), at least once (1), and exactly once (2).
  4. Security: MQTT simplifies the process of securing IoT communications. Developers can easily encrypt messages and authenticate devices and users using modern authentication protocols like OAuth, TLS1.3, Customer Managed Certificates, and more. This ensures that data transmission remains secure and protected.
  5. Extensive Support: MQTT is well-supported in various programming languages, such as Python. This means that developers can quickly implement MQTT in their applications with minimal coding effort, making it a versatile choice for IoT projects.

How can AWS support your MQTT implementation

MQTT is designed for high efficiency, demanding minimal computing power or memory. It can effectively operate on small IoT devices with limited resources. For example, a basic MQTT message can consist of only two data bytes, and the message headers are also compact, enabling optimized utilization of network bandwidth.

MQTT Client and Broker

An MQTT broker plays a central role in the MQTT architecture, similar to how a real estate broker conducts background checks and ensures rule compliance before initiating a transaction.

The MQTT broker handles message transactions instead of monetary ones. Here’s how it facilitates transactions between MQTT clients:

  1. It enables devices, also known as “client devices” or simply “clients,” to initiate connection requests.
  2. It authenticates devices based on the connection information they provide.
  3. Once authenticated, it ensures that devices can securely send and receive messages to/from other devices, optionally using Transport Layer Security (TLS) encryption.
  4. The broker stores messages within the server, allowing them to be re-sent in case of unexpected connection loss, client-connect, or client-disconnect events.

By allowing devices (clients) to communicate independently, MQTT brokers enable easy scalability of the architecture without impacting existing client devices. Since the MQTT broker handles the heavy lifting, client devices require minimal processing and bandwidth.

Now, let’s delve into how client devices operate. Clients encompass a wide range of “smart” devices, including smartphones, web apps, sensor nodes, actuators, and smartwatches. Clients can:

  1. Connect to a broker using a username and password.
  2. Subscribe to a topic to listen for messages published to that specific topic.
  3. Publish messages to a topic, effectively sending messages to that topic.

Drawbacks of MQTT

MQTT has several potential drawbacks:

  1. Slower Transmit Cycles: MQTT’s transmit cycles are slower compared to the Constrained Application Protocol (CoAP).
  2. Resource Discovery: MQTT’s resource discovery relies on flexible topic subscription, while CoAP uses a stable resource discovery system.
  3. Security: MQTT lacks encryption and relies on TLS/SSL (Transport Layer Security/Secure Sockets Layer) for security.
  4. Scalability Challenges: Creating a globally scalable MQTT network is difficult because MQTT’s topic structure can become complex as it grows.
  5. Interoperability: MQTT’s binary message payloads lack standardized encoding information, making interoperability problematic in open architectures with diverse applications.
  6. Authentication: MQTT has limited built-in authentication features. Usernames and passwords are transmitted in cleartext, necessitating the use of SSL/TLS for security.
  7. Client Authentication Complexity: Authenticating clients with client-side certificates is complex. MQTT lacks built-in mechanisms for controlling topic ownership and message publishing, requiring proprietary solutions that can introduce security risks.

Additionally, the absence of sender identification in MQTT messages means that message receivers cannot determine the sender’s identity unless this information is included in the message itself. Consequently, implementing security features on top of MQTT in a proprietary manner increases code complexity and implementation challenges.

Installing and Configuring MQTT Broker on Raspberry Pi

  1. Connect Raspberry Pi to your WiFi Router.
  2. Open the terminal on Main Raspberry Pi and execute the following commands.
  3. sudo nano /etc/hosts
    • Change rasberrypi to main rasberrypi
    • Press CTRL+O then ENTER and then CTRL+X
  4. sudo reboot
  5. Once the raspberry pi reboots run the following commands
  6. sudo apt install ufw
  7. sudo ufw enable
  8. sudo apt install mosquitto mosquitto-clients
  9. sudo systemctl enable mosquitto
  10. sudo ufw allow 1883

Once done, open two terminal, and in the first terminal enter the following subscribe command:

mosquitto_sub -t outTopic -v -h  localhost

While in the second terminal execute the following publish command:

mosquitto_pub -d -t outTopic -m "Hello World" -h localhost

You will see “Hello World” in the subscription terminal.

Suggested Reads!