An Introduction to IoT Client Development with MQTT

The Internet of Things (IoT) is a network of interconnected smart physical devices that collect and share data autonomously through the Internet. This technology brings countless advantages and applications, making it increasingly adopted by individuals, families, businesses, and industries. This article will briefly introduce how to implement a basic IoT Client-Broker-Server infrastructure using the MQTT protocol.

by
Jorge Duré
|
February 29, 2024

What is MQTT?

MQTT is an open Machine-to-Machine (M2M) communication protocol known for its lightweight, versatility, ease of use, and implementation. It can be used on small microcontrollers with limited resources and low bandwidth, making it ideal for IoT environments.

 

MQTT stands for "Message Queueing Telemetry Transport" and was initially designed to connect devices and send sensor information to remote servers in the industry.

It is widely used in various applications, ranging from everyday devices such as appliances and wearables, vehicles, telecommunications, and supply chain management, to industrial and manufacturing control.

 

How does it work?

This protocol is based on a publish/subscribe model, enabling asynchronous communication between the smart device, the broker, and subscribing clients. Subscribing clients can be any device connected to the network that implements the TCP/IP protocol stack and MQTT protocol.

 

The broker acts as an intermediary in the publish/subscribe model. It serves as a message agent that receives messages from devices and then distributes them to the corresponding subscribing clients.

 

 MQTT also facilitates distinguishing between different devices and subscribers using a message identifier called a "topic." In this way, the broker sends only the message the client requests when subscribing to that topic.

The diagram above shows an example with two smart sensor devices, one measuring and sending temperature data and the other measuring humidity in a specific environment. Some subscribing clients subscribe to one or more topics.

 

This mechanism allows a smart device to transfer information to many clients at a meager cost. All it needs to do is send a message to the broker, and the broker will handle all the "dirty work" efficiently. This allows the client to be implemented on small microcontrollers like Raspberry Pi, Arduino, or an ESP32.

 

Features

This protocol is considered lightweight as the messages have a small code footprint. Each message has a fixed 2-byte header, an optional variable header, a payload limited to 256 megabytes, and a Quality of Service (QoS) level. The minimalistic design of MQTT helps to minimize bandwidth and resource usage, making it suitable for constrained devices and networks in IoT applications.

 

The MQTT broker has several additional functions, such as handling client authentication and authorization, storing telemetry in a database, filtering and sending messages, etc. Therefore, it is ideally installed on efficient platforms with sufficient resources to handle all the required activity. In some cases, hundreds or even thousands of devices and clients are connected to a single broker.

 

The MQTT broker can be installed on any local, remote, virtual computer, etc. It also adapts well to the cloud, and cloud infrastructure services are available that provide MQTT brokers. Prominent examples include Azure IoT, AWS IoT, and Google Cloud IoT.

 

Regarding security, MQTT includes SSL/TLS transport, user authentication with usernames and passwords, and authentication using certificates. During the communication phase, a client can publish, subscribe, unsubscribe, and ping. The publishing operation sends a binary data block (the content) to a topic defined by the broker.

 

MQTT supports large binary message objects (BLOBs) of up to 256 MB in size, and the content format will be specific to the application.

 

QoS (Quality of Service)

This protocol supports a QoS mechanism, providing three levels of quality to ensure message delivery between the broker and clients:

 

● QoS 0 - unacknowledged (at most once): The message is sent only once. In case of failure, it is possible that the message will not be delivered.

● QoS 1 - acknowledged (at least once): The message is sent until delivery is confirmed. In case of failure, the subscriber may receive some duplicate messages.

● QoS 2 - assured (exactly once): It guarantees that each message is delivered only once to the subscriber.

 

Choosing the QoS level depends on the system’s requirements. Naturally, higher QoS levels result in a higher data payload.

 

Pros and Cons

The MQTT protocol has several valuable advantages but has certain disadvantages compared to similar protocols.

 

Advantages

● Low network usage due to the small size of data packets.

● Efficient and quick implementation of data transmission (lightweight protocol).

● Efficient distribution of data.

● Uses small amounts of energy, which is beneficial for IoT devices.

● Fast and efficient message delivery.

● MQTT clients can be easily optimized for devices with limited memory, such as Arduino or ESP32.

 

Disadvantages

● Difficult to create a globally scalable MQTT network.

● Slower transmission cycles compared to CoAP.

● MQTT is not encrypted by default. Instead, it uses TLS/SSL for security encryption.

● Resource discovery in MQTT relies on flexible topic subscriptions, while CoAP uses a stable resource discovery system.

Conclusion

While it’s not the only protocol available, MQTT is widely used in IoT, and it offers many advantages with its publish/subscribe model, standing out for its simplicity of communication and ease of implementation.

 

In the next entry, we will explore an example of a simple IoT infrastructure using Eclipse Mosquitto, an open-source broker that implements the MQTT protocol. We will also develop a primary Java client using the Eclipse Paho library.

 

Stay tuned for the upcoming tutorial on building an MQTT-based IoT infrastructure and client implementation.

 

 

Sources:

https://www.hivemq.com/mqtt-essentials/

https://mqtt.org/

https://www.eclipse.org/paho/

https://mosquitto.org/