MQTT Tutorial: An Easy Guide to Getting Started with MQTT
Introduction to MQTT
MQTT is a publish/subscribe protocol designed for connecting IoT devices. Unlike HTTP’s request/response paradigm, MQTT operates in an event-driven manner, allowing messages to be pushed to clients. This architectural approach enables highly scalable solutions by decoupling data producers and data consumers, eliminating dependencies between them. Two key components to establish MQTT connection for publishing and subscribing of the messages are MQTT Clients and MQTT Broker as shown in the diagram below.
Benefits of MQTT
MQTT offers several key benefits:
- Lightweight and efficient: MQTT minimizes the resources required by clients and network bandwidth.
- Bidirectional communication: MQTT facilitates communication between devices and servers, supporting publishing and subscribing. It also allows broadcasting messages to groups of devices.
- Scalability: MQTT can scale to support millions of devices or “things” in an IoT or IIoT ecosystem.
- Quality of Service (QoS) levels: MQTT specifies different QoS levels to ensure reliable message delivery.
- Persistent sessions: MQTT supports persistent sessions between devices and servers, reducing reconnection time over unreliable networks.
- Security features: MQTT supports TLS encryption for message confidentiality and authentication protocols for client verification.
From Automotive to Energy: Real-World Applications and Use Cases of MQTT
MQTT finds applications across various industries and domains. It has been adopted by companies such as BMW, Liberty Global, Fortum, Hytera, Awair, and Matternet, as showcased in HiveMQ’s customer success stories. These companies have successfully leveraged MQTT in automotive, telecommunications, energy, public safety, and connected product domains.
MQTT Basics: Mastering the Essentials of this IoT Protocol
At the core of MQTT are MQTT brokers and MQTT clients. The MQTT broker is an intermediary between senders and receivers, dispatching messages to the appropriate recipients. MQTT clients publish messages to the broker, and other clients subscribe to specific topics to receive messages. Each MQTT message includes a topic, and clients subscribe to topics of their interest. The MQTT broker maintains a subscriber list and uses it to deliver messages to the relevant clients.
An MQTT broker can also buffer messages for disconnected clients, ensuring reliable message delivery even in unreliable network conditions. To achieve this, MQTT supports three different Quality of Services (QoS) levels: 0 (at most once), 1 (at least once), and 2 (exactly once).
There are two versions of the MQTT specification: MQTT 3.1.1 and MQTT 5. While most commercial MQTT brokers now support MQTT 5, some IoT-managed cloud services still primarily support MQTT 3.1.1. We highly recommend using MQTT 5 for new IoT deployments due to its enhanced features that focus on robustness and cloud-native scalability.
For a comprehensive understanding of the protocol, we highly recommend delving into the MQTT Essentials series of articles or exploring the companion MQTT Essentials video series. Additionally, the MQTT 5 Essentials series offers a detailed introduction to the specific features of MQTT 5.
Many open source clients are available in a variety of programming languages. HiveMQ provides its own MQTT clients through HiveMQ MQTT Client Libraries, which are designed to simplify the deployment and implementation of MQTT clients and offer users top-notch functionality, performance, security, and reliability. Some of the programming languages supported include C#, C++, Java, Websockets, Python, and more. Eclipse Paho also offers MQTT client libraries for languages like C/C++ and Python. You can find a comprehensive list of MQTT clients at mqtt.org.
MQTT Brokers come in various implementations, catering to different needs, such as open-source, commercial, and managed cloud services. HiveMQ offers two commercial editions: HiveMQ Professional and HiveMQ Enterprise. HiveMQ also offers HiveMQ Cloud, a managed cloud MQTT service, and HiveMQ Community Edition, an open-source version. For an extensive list of MQTT brokers, please visit mqtt.org.
If you are looking to find the ideal MQTT broker for your IoT or industry 4.0 use case, read our article MQTT Broker Comparison – Which is the Best for Your IoT Application?
Example Implementation of MQTT
To illustrate how MQTT works, below is a simple example that utilizes HiveMQ Cloud. To test this implementation on a live cluster, sign-up for HiveMQ Cloud Free plan, which allows you to connect up to 100 IoT devices at no cost. Sign-up without credit card information.
As a first-time user, HiveMQ Cloud seamlessly redirects you to the dedicated “Getting Started” section within the management view of your cluster, ensuring a smooth and hassle-free onboarding experience. To get you started, refer the below use case example that was tested on Java 11. This example serves as a valuable resource to help you get up and running quickly. We’ve also prepared a publicly hosted example in our ready-to-go Github repository, along with an example tailored for our public broker.
MQTT Use Case Example:
IoT Application Built Using Raspberry Pi, MQTT, and Temperature Sensor
In this example, we explore the connection of a temperature and brightness sensor to a Raspberry Pi, leveraging the power of MQTT to transmit sensor data to a designated MQTT broker effortlessly. Discover how another device, acting as a control center, can effortlessly receive and process the MQTT data, enabling efficient monitoring and control of your IoT ecosystem.
Step 1 - Use the Raspberry Pi as an MQTT Client Connected to Sensors
Let’s begin your MQTT journey by creating a powerful MQTT client to publish sensor data. In this step, we’ll utilize a thermometer, brightness sensor, and the robust HiveMQ Cloud as our MQTT broker. Once you’ve successfully signed up for HiveMQ Cloud, head to the Details section on the Overview tab of your cluster. There, you’ll discover your unique hostname. Copy this hostname and replace it in the code snippet provided below. There you will find your hostname. Copy this hostname to replace it in the example code snippet below.
To establish a secure connection between your MQTT client and the cluster, creating MQTT credentials is essential. Navigate to the Access Management tab of your HiveMQ Cloud Basic cluster, define your MQTT credentials, and select "+Add." These credentials will also replace
"<your_password>" within the code snippet, ensuring seamless integration. Let’s dive into the exciting world of MQTT with confidence and efficiency.
Let’s dissect the code snippet provided above to understand its functionality:
- Creating the MQTT Client: The code initializes the MQTT client, ensuring a unique identifier is used. An automatic reconnect feature is also enabled to handle potential instability in the sensor’s internet connection.
- Establishing Connection to
"<your_host>": The client connects to the specified host. Notably, a “will” message is set, allowing the broker to automatically publish a “sensor gone” notification if the sensor loses its connection.
- Periodic Publication of Simulated Sensor Data: The code periodically publishes simulated brightness and temperature data using the methods getBrightness() and getTemperature() methods, ensuring a steady stream of information for further processing.
With this code snippet, you can confidently create an MQTT client, establish a stable connection, and regularly transmit vital sensor data.
Now, let’s move on to the next step in our implementation process:
Step 2 - Implementing the Subscribing Client
In this crucial step, we focus on creating the subscribing client responsible for consuming the values published on the topics
Implementing the subscribing client enables the reception of sensor data transmitted via MQTT. This functionality allows you to process and utilize the received information for various applications efficiently.
The code snippet above performs the following actions:
- Creates an MQTT client instance, similar to the sensor client, with the client ID prefixed as
- Establishes a connection between the client and the specified host in
<your_host>. To ensure message buffering when the control center is offline, a session expiry interval of 1 hour is set.
- Subscribes the client to all topics starting with
homeusing the multi-level wildcard
#in the topic filter. Any incoming messages with their corresponding topic and payload are printed. If the sensor loses connection, the topic
home/willand the payload “sensor gone” are printed.
By implementing both the sensor client and the subscribing client, you can establish a seamless MQTT communication system where sensor data is published and consumed by the control center, enabling efficient monitoring and control of devices.
Now that you’ve gained a basic understanding of MQTT, congratulations! However, the world of MQTT is vast, and there’s always room for more knowledge. To fuel your ongoing learning journey, we highly recommend exploring these additional resources:
Dive deeper into the MQTT protocol by exploring our resources that offer a wealth of technical details to enhance your understanding.
- Take a glance at the MQTT Glossary, which provides a concise overview of key MQTT terminologies, allowing you to grasp them quickly and easily.
- Read the MQTT Essentials and MQTT 5 Essentials series of articles to learn more technical details about the protocol.
- For those who prefer a locally-hosted solution, you can explore the evaluation license of our HiveMQ Platform. This option allows you to experience the benefits of HiveMQ while running it on your infrastructure.
- We also have the HiveMQ MQTT Client and the MQTT CLI for easy testing of MQTT systems.
- If you have specific requirements or need guidance regarding MQTT and IoT messaging, feel free to Contact HiveMQ. Our team of experts has extensive experience in assisting companies with developing reliable and scalable IoT applications. We’ll be more than happy to discuss your unique needs and provide tailored solutions.
Remember, MQTT is an exciting and evolving field; there’s always more to explore and discover.
MQTT protocol is lightweight and efficient compared to other IoT protocols. It offers bidirectional communication and can scale to support millions of IoT or IIoT devices. It also supports persistent sessions and supports TLS encryption. Moreover, you can build your own specification tailored for your industry using MQTT.
Some of the key components of MQTT architecture are MQTT clients and MQTT broker. To learn more, read the article MQTT Client and Broker Connection.
In MQTT, the broker is the server that receives and distributes messages to clients based on their topic subscriptions. Clients, which can be any device running an MQTT library, either publish messages to the broker (publishers) or receive messages from the broker (subscribers). Often, a client can act as both a publisher and a subscriber.
Connection timeout could be due to a number of reasons including unstable network connection, Broker being down, wrond IP address and port number. You handle this by increasing connection timeout, using Last will and Testament, checking security settings and firewalls, and MQTT version compatibility.
Ensure that the certificates used for the SSL/TLS connection are correct and valid. If the client is set to verify the server’s hostname against the server’s certificate, make sure the hostname used to connect to the server matches the one in the server’s certificate.
Check network connection, verify broker is available, verify broker hostname and port, and check client configuration.
There are MQTT client libraries for many programming languages and platforms. These incluse Java, C#, NodeJS, Python, Arduino, Micropython, C, and more. Check out our MQTT Client library page.
A majority of home automation applications already use MQTT protocol. MQTT is also being increasingly used in industrial automation with many PLCs, SCADA and Historians systems natively supporting it.
There are several alternatives to MQTT protocol, such as CoAP, AMQP, etc. As MQTT protocol is lightweight, efficient, offers bidirectional communication and several other benefits, experts across industry verticals prefer MQTT over other messaging protocol.