Data packets sent by IoT smart devices must be encapsulated by the corresponding protocol at each layer.
With the rapid development of Internet of Things (IoT) technology, most smart devices’ communication modules support various network communication protocols such as TCP, UDP, MQTT, CoAP, HTTP, and LwM2M, which include both transport layer and application layer protocols, each suited for different scenarios.
When designing IoT hardware products, we usually only need to choose one protocol. Therefore, selecting the appropriate communication protocol is crucial for building efficient and reliable business application systems.
MQTT Protocol
The MQTT protocol is a lightweight message transport protocol designed for low bandwidth, high latency, or unreliable network environments, using a client/server architecture with a publish/subscribe message transmission model.
Its design philosophy is lightweight, open, simple, and standardized, making it easy to implement. These characteristics make it a great choice for many scenarios, especially in constrained environments such as machine-to-machine (M2M) communication and IoT environments.
Characteristics of MQTT Protocol
The MQTT protocol’s packet format is very simple, consisting of a fixed header, variable header, and payload. It has the following characteristics:
-
Publish/Subscribe Model: Allows devices to exchange messages through a central node—MQTT Broker, which is very suitable for real-time data streams in distributed systems.
-
Quality of Service (QoS): Provides three levels of service quality, from at most once to exactly once, ensuring different levels of message reliability.
-
Lightweight: The MQTT message header requires a minimum of only 2 bytes, suitable for bandwidth-limited network environments.
-
Heartbeat Mechanism: Business applications do not need to worry about keeping alive long connections.
-
Bandwidth Saving: The MQTT protocol’s payload uses a binary protocol, which can save traffic. Traffic is lower than that of the HTTP protocol.
-
Firewall Traversal: The MQTT protocol can traverse network firewalls using MQTT over WebSocket.
-
Persistent Sessions: Supports reconnection, ensuring message continuity even in unstable network conditions.
-
Last Will and Testament Messages: Allows setting messages to be published when the client disconnects unexpectedly, and saves the latest messages for new subscribers.
HTTP Protocol
The HTTP protocol is an application layer protocol based on a request/response model, primarily used for data exchange between web browsers and servers. It has the following characteristics:
Statelessness: Each request is independent and does not retain session information.
Rich Tool Support: Has a large number of development libraries and debugging tools, easy to integrate into existing web architectures.
Easy to Understand and Use: Uses text format messages, which are easy for humans to read and debug.
However, in IoT application scenarios, the HTTP protocol exhibits the following limitations:
-
High Overhead: Each communication requires establishing a complete TCP connection and sending large header packets, resulting in high network bandwidth consumption.
-
Insufficient Real-time Performance: HTTP is stateless and cannot proactively push messages to the device side; the commonly used polling mechanism cannot meet the real-time needs of IoT.
-
Not Friendly to Low-Power Devices: Frequent connection and disconnection operations undoubtedly increase energy consumption, which is particularly evident in battery-powered small devices.
HTTP Protocol in IoT Application Scenarios
Although the MQTT protocol is widely recommended for its lightweight characteristics and IoT-optimized functions, this does not mean that the HTTP protocol is completely unsuitable for IoT.
-
Smart Home Apps: In this scenario, users typically do not require extremely high real-time performance, and the user-friendliness of the app interface is more important. The HTTP protocol can well meet such needs, especially when combined with existing web infrastructure.
-
Remote Monitoring: If the data collection frequency is not high and the latency requirements are low, HTTP is still a viable option. However, in cases requiring frequent updates or real-time feedback, the advantages of MQTT become more apparent.
In summary, the HTTP protocol is not entirely unsuitable for IoT applications. It can still play an important role in scenarios where extremely high real-time performance is not required and where there is existing mature web infrastructure support.
In most typical IoT projects, considering the comprehensive advantages of performance, energy consumption, and flexibility, the MQTT protocol is often a better choice for hardware devices.
Source: 5G Communication
Reviewed by: Zhao Lixin