Table of contents
1. Introduction to MQTT
2. MQTT concept
2.1 Subscription
2.2 Session
2.3 Topic Name
2.4 Topic Filter
2.5 Message subscription
3. Roles in MQTT
3.1 Client
3.2 Server
4. Install mosquitto on X86 platform
4.1 Download mosquitto server
4.2 Download mosquitto client
4.3 mosquitto command
4.3.1 mosquitto
4.3.2 mosquitto_pub
4.3.3 mosquitto_sub
5. Software testing
6. QT uses MQTT
6.1 MQTT initialization
6.1.1 Source code download
6.2.1 Add source code to the project
6.3.1 Add header file path
6.4.1 Compilation
6.5.1 mqtt initialization
6.6.1mqtt signals and slots
6.7.1 MQTT Release
1. Introduction to MQTT
MQTT
(
Message Queuing Telemetry Transport
, Message Queuing Telemetry Transport Protocol) is a
" lightweight " communication protocol based on the publish/subscribe model. This protocol is built on
the
TCP
/IP protocol and was developed by IBM in 1999 . release. The biggest advantage of MQTT is that it can provide real-time and reliable messaging services for connecting remote devices with very little code and limited bandwidth. As a low-cost, low-bandwidth instant messaging protocol, it has wide applications in the Internet of Things, small devices, mobile applications, etc.
MQTT
is a client
-
server based message publish
/
subscribe transport protocol.
The MQTT
protocol is lightweight, simple, open and easy to implement,
These features make it suitable for a wide range of applications. In many cases, including in constrained environments such as machine-to-machine (
M2M
) communications and the Internet of Things (IoT
). It has been widely used in communication sensors through satellite links, medical equipment for occasional dialing, smart homes, and some miniaturized devices.
2. MQTT concept
2.1 Subscription
Subscriptions include topic filters (
Topic Filter
) and maximum quality of service (
QoS
). Subscriptions are associated with a
session
. A session can contain multiple subscriptions. Each subscription in each session has a different topic filter.
2.2 Session
After each client establishes a connection with the server, it is a session, and there is stateful interaction between the client and the server. A session exists between one network and may span multiple consecutive network connections between the client and server.
2.3 Topic Name
Connect to a label for an application message that matches the server's subscription. The server sends the message to every client subscribed to the matching tag.
It should be noted that message topics in
MQTT
are named hierarchically and are separated by '/'
In addition, wildcards can be used in topics to subscribe to multiple topics or multiple levels. There are two common wildcards:
1.
Single-layer wildcard
+
: A single-layer wildcard can only match one layer of topics, for example:
China/Beijing/+
, and can only match the topics of one layer below the
Beijing
topic, such as Xicheng, DongCheng, Xuanwu
, etc.
2.
Multi-level wildcard
#
: As the name suggests, multi-level wildcard can match topics at multiple levels, for example:
China/#
. The topics that can be matched may include: China/Beijing/Dongcheng, China/Shanghai/PuDong
, etc.
2.4 Topic Filter
A wildcard filter for topic names, used in subscription expressions to indicate multiple topics matched by the subscription.
2.5 Message subscription
The specific content received by message subscribers.
3. Roles in MQTT
1. Publisher and Subscriber are the clients, Broker is the server, and the message topic is the message type. The Broker filters the messages based on the Topic and pushes the messages to the client.
2. QoS
is used in
MQTT to represent quality of service. There are three types of quality of service (QoS) in the MQTT protocol :
1
)
QoS =0
, at most once, packet loss may occur, used in situations where real-time requirements are not high, for example, using this quality of service together with communication environment sensor data. It doesn't matter whether individual reads are lost or whether new reads are published immediately later.
2
)
QoS =1,
at least once, it is guaranteed that the packet will reach the destination, but heavy packets may occur.
3
)
QoS =2,
exactly once, ensuring that the packet will reach the destination and there will be no duplicate packets.
3.1 Client
1. Both Publisher
and
Subscriber
belong to the client.
2.
Publish application messages to other related clients.
3.
Subscribe to request to receive relevant application messages.
4.
Unsubscribe to remove requests to accept application messages.
5.
Disconnect from the server
3.2 Server
1.
The server side is the so-called
MQTT Broker
server.
2.
Accept network connections from clients.
3.
Accept application messages published by the client.
4.
Handle the client's subscription and unsubscription requests.
5.
Forward application messages to qualified subscribed clients.
6. The public servers (
Broker ) provided by
MQTT
are:
1) test.mosquitto.org
2) broker.hivemq.com
3) iot.eclipse.org
4. Install mosquitto on X86 platform
4.1 Download mosquitto server
Order:
sudo apt-get install mosquitto
4.2 Download mosquitto client
sudo apt-get install mosquitto-clients
4.3 mosquitto command
4.3.1 mosquitto
The command to
start the server has already been started
in
Ubuntu .
4.3.2 mosquitto_pub
Commands used to publish messages
mosquitto_pub -h 192.168.0.137 -t "house" -m "500"
-h
specifies which server to publish from
-t
specifies which topic to publish
-m
is the content of the message
4.3.3 mosquitto_sub
Commands used to subscribe to messages
mosquitto_sub -h 192.168.0.137 -t "house" -v
-h specifies which server to subscribe from;
-t
specifies which topic to subscribe to;
-v
means to print out the message received
5. Software testing
windows
install
mqttfx-1.7.1-windows-x64.exe
test software and open it
Test Results:
6. QT uses MQTT
6.1 MQTT initialization
6.1.1 Source code download
https://github.com/emqx/qmqtt
To use this library,
the Qt
version must be greater than
5.3
. It has two versions
: master
and
release
version
. We use the
release version
here.
6.2.1 Add source code to the project
Add to network module
6.3.1 Add header file path
Error: Solution
6.4.1 Compilation
Error
Solution:
6.5.1 mqtt initialization
QMQTT::Client *mqttClient ;
QString clientID;
qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));
clientID = "XYD-CXX"+QString::number(qrand()); //合成 mqtt id
//mqtt 初始化
mqttClient = new QMQTT::Client(ui->lEditServerIP->text(), 1883);
mqttClient->setClientId(clientID); //ClientId 不能重复,不然会覆盖之前连接的人的
//mqttClient->setUsername("user"); //服务器账号密码,不需要则不用
//mqttClient->setPassword("password");
mqttClient->connect();
6.6.1mqtt signals and slots
//mqtt 连接成功
connect(mqttClient,SIGNAL(connected()),this,SLOT(doProcessMqttConnected()));
//mqtt 退出成功
connect(mqttClient,SIGNAL(disconnected()),this,SLOT(doProcessMqttDisconnected()));
//mqtt 订阅成功
connect(mqttClient,SIGNAL(subscribed(QString)),this,SLOT(doProcessMqttSubscribe(QSt
ring)));
//mqtt 订阅到消息
connect(mqttClient,SIGNAL(received(QMQTT::Message)),this,SLOT(doProcessMqttReceived
(QMQTT::Message)));
6.7.1 MQTT Release
if(mqttIsConnected)
{
QMQTT::Message msg;
msg.setTopic(ui->lEditPub->text()); //发布的主题
//发布的内容,QString 转 QByteArray
msg.setPayload(ui->lEditPubPayload->text().toLatin1());
mqttClient->publish(msg);
}
Slot function for successful subscription: This is generally not used
void MqttForm::doProcessMqttSubscribe(QString topic)
{
//qDebug()<< topic + "is Subscribe";
}
Slot function for receiving information:
void MqttForm::doProcessMqttReceived(QMQTT::Message msg)
{
qDebug() << "topic:" + msg.topic();
qDebug() << "payload:" + msg.payload();
}