Take you to understand the MQTT protocol
Author | Wang Yingyue, unit: China Mobile Smart Home Operation Center
Labs Guide
At present, the MQTT protocol is widely used in autonomous driving, industry, communication and other fields. In the smart home market, many IoT platforms such as China Mobile, Huawei, Alibaba, and Tencent all support MQTT protocol access. This article will introduce the characteristics and operation of the MQTT protocol in detail.
MQTT is an IoT messaging protocol under the Organization for the Advancement of Structured Information Standards (OASIS) standard. Its lightweight publish/subscribe transport mechanism is ideal for providing reliable messages for remotely connected devices. Communication service. At present, the MQTT protocol is widely used in autonomous driving, industry, communication and other fields. In the smart home market, many IoT platforms such as China Mobile, Huawei, Alibaba, and Tencent all support MQTT protocol access.
Part 01 Features of the MQTT protocol
- Lightweight and efficient
An MQTT client can be implemented with minimal resources, which allows the MQTT protocol to easily run on tiny chips. Minimal message settings for minimal network overhead.
- Scope Broadcast
Easily broadcast messages to millions of devices.
- stable transmission
Three types of message transmission assurance levels provide reliability assurance of message transmission in different scenarios.
- Weak network support
Session retention mechanism reduces device reconnection time in weak network environment.
- Security
Support multiple authentication protocols to ensure message security
Part 02How does the MQTT protocol work ●
The MQTT protocol defines a message broker node (Broker) and an MQTT client (Client), two types of network entities. The Broker receives the messages sent by the Client and is responsible for forwarding these messages to the specified Client. Client is any entity that can interact with Broker and is responsible for sending and receiving messages. Common clients such as IoT sensors and cloud servers. It can be seen that in the MQTT protocol, the message is forwarded through the Broker, instead of directly connecting and communicating between one Client and another Client. In order to distinguish, we can define Client as a message sender (Publisher-Client) and a message receiver (Subscriber-Client).
- The Client establishes a connection with the Broker, which can be a TCP/IP connection or an encrypted TLS connection.
- Subscriber-Client subscribes to a topic (Topic).
- Publisher-Client sends a Topic message to the Broker.
- The Broker forwards the message to all Subscriber-Clients that subscribe to the Topic.
Part 03MQTT协议质量保障
众所周知,物联网设备往往处在复杂的网络环境下,比如农业物联网中,蔬菜大棚的传感器设备;工业物联网中,封闭车间的温湿度传感器等等。在这些复杂网络场景下,设备状态是无法确定的,为了在这些场景下保障连接,MQTT协议提供了消息服务质量保障等级(Qos,Quality of Service levels)。
- Qos-0最多一次
Qos 0 保障消息到达终点最多不超过1次。消息可能到达1次或根本不会到达。适用于功率受限设备,可以最大减少传播时间,减少消息传递成本;适用于非关键消息传递,例如被频繁重复发送的消息。
- Qos-1 最少一次
Qos 1保障消息到达终点最少不低于1次。Broker在确认接收消息后会返回一个PUBACK消息给到Publisher-Client。如果消息发送失败,Publisher-Client将在一段时间后重发消息。此类型消息适用于重要不可丢失的消息传递,可利用该传输机制保证消息在离线设备上线后仍能收到。Subscriber-Client需要做好重复消息处理工作。
- Qos-2 只有一次
Qos 2 保障消息到达终点只有1次。此类消息保证接接收端对于一条消息只接收一次,是MQTT协议中最安全且传输速度最慢的一种消息传递形式。适用于十分重要且不允许重复消息出现的场景使用,由于消息十分重要,因此可忽视额外的网络开销。
Part 04最小MQTT应用搭建
在了解MQTT理论基础概念后,我们一起在Windows系统上搭建一个最小MQTT服务,实现MQTT消息的发送及订阅。
(1) Broker搭建
我们选择开源代理平台Mosquitto作为此次最小MQTT应用的单点Broker。
将Mosquitto Windows版下载完成后,通过Windows系统终端打开mosquitto.exe文件。输入./mosquitto.exe -p 1883 即在Windows系统上启动了一个MQTT服务,监听地址为127.0.0.1,端口是1883。
(2) Client搭建
完成Broker搭建后,我们还需要Client实现消息的发送和订阅。我们选择mqtt.fx软件作为此次最小MQTT应用的Client。打开mqtt.fx软件,我们通过设置MQTT Broker Address为127.0.01,Broker Port为1883,并自定义Client ID。完成设置后,点击主页面的Connect,页面右上角的连接指示灯显示为绿色即表示完成了连接,此时我们实现了Client到Broker的连接。
(3) 消息订阅
假定步骤二创建的Client为一个Subscriber-Client,此时通过点击主页面中的Subscribe,填写订阅Topic信息,在示例中我订阅的Topic为test,即可完成该Client对于该Topic订阅。
(4) 消息发送
重新打开一个mqtt.fx客户端,创建一个Publisher-Client,设置MQTT Broker信息与Subscriber-Client信息一致,成功连接Broker后,在Publish页面下,向指定Topic即test发送信息,此时我们可以在Subscriber-Client界面上看到消息成功的从Broker转发至Subscriber-Client。
至此一个最小的MQTT应用就搭建完成了。
综上所述,MQTT协议关注于消息,拥有轻量、高效、低开销等特性,相对于HTTP等广泛应用于互联网场景的协议来说,MQTT协议与物联网通信领域更加契合。