更新源:sudo apt update
安装mqtt服务器:sudo apt install mosquitto
mqtt服务器配置
sudo nano /etc/mosquitto///修改配置文件,添加如下配置拒绝匿名访问
pid_file /run/mosquitto/
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/
include_dir /etc/mosquitto/
allow_anonymous false//不允许匿名访问
password_file /etc/mosquitto///设置用户名密码存放的文件
设置mqtt服务器的用户密码
mosquitto_passwd -c /etc/mosquitto/ zsl//表示用户为zsl,用户密码存放在etc/mosquitto/ -c表示清空etc/mosquitto/里面的信息,然后再将用户添加进去,不加-c将在etc/mosquitto/文件末尾一直追加
查看mqtt服务状态如遇见Unit could not be found提示信息则查看mqtt服务文件是否创建
sudo ls /lib/systemd/system/
sudo ls /etc/systemd/system/
如果没有创建则可以创建一个简单的服务文件
sudo nano /etc/systemd/system/ 添加如下内容: [Unit] Description=Mosquitto MQTT Broker Documentation=man:(5) man:mosquitto(8) After= [Service] ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/ Restart=on-failure User=mosquitto Group=mosquitto [Install] WantedBy= 重新加载systemd配置,使新的服务文件生效 sudo systemctl daemon-reload 查看服务器是否已经启动 sudo systemctl status mosquitto 查看mqtt启动失败的日志信息sudo tail -f /var/log/mosquitto/
客户端安装
sudo apt install mosquitto-clients
订阅消息
mosquitto_sub -h "127.0.0.1" -p 1883 -u zsl -P zsl -t "hello"
//-h "127.0.0.1"指定服务器的IP地址 -p 1833 端口 -u zsl 用户 -P zsl 密码 -t "hello"订阅的主题名称
服务端向客户端发消息
mosquitto_pub -h "127.0.0.1" -p 1883 -u zsl -P zsl -t "helo" -m "nihao"
qt mqtt服务器搭建
下载qt/mqtt库GitHub - qt/qtmqtt at 6.5.3然后修改如下
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
set(CMAKE_PREFIX_PATH "D:\Qt\6.5.3\mingw_64")#替换为自己的路径
cmake_minimum_required(VERSION 3.16)
include(.)
project(QtMqtt
VERSION "${QT_REPO_MODULE_VERSION}"
DESCRIPTION "Qt Mqtt Libraries"
HOMEPAGE_URL "/"
LANGUAGES CXX C
)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
find_package(Qt6 ${PROJECT_VERSION} CONFIG REQUIRED COMPONENTS BuildInternals Core Network)
find_package(Qt6 ${PROJECT_VERSION} CONFIG OPTIONAL_COMPONENTS Quick WebSockets # For tests
Gui Widgets) # For examples
qt_internal_project_setup()
qt_build_repo()
使用qt打开,选择mingw套件编译生成,
1、生成完之后,将C:\Users\www23\Desktop\1\qtmqtt-6.5.3\src\mqtt目录下的所有.h文件拷贝到D:\Qt\6.5.3\mingw_64\include\QtMqtt。qt安装目录下,
2、将C:\Users\www23\Desktop\1\qtmqtt-6.5.3\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug\include\QtMqtt所有的.h文件也拷贝过去,
3、将C:\Users\www23\Desktop\1\qtmqtt-6.5.3\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug\bin目录下的.dll文件拷贝到安装路径下-bin
4、将C:\Users\www23\Desktop\1\qtmqtt-6.5.3\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug\lib目录下的.a文件也拷贝到qt安装目录下-lib
5、在项目工程.pro文件写添加:
LIBS+=C:\Users\www23\Desktop\1\qtmqtt-6.5.3\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug\lib\
如果遇见QSslSocket cannot 则可以将OPENSSL库下载下来,将 拷贝到项目的build目录下。
windows下安装mqtt服务器
地址:/download/
添加环境变量//D:\Program Files\mosquitto//重启电脑生效
添加用户认证信息与linux一致,唯一需要注意的是,需要在路径名上添加双引号,mosquitto_passwd -c "D:\Program Files\mosquitto\" zsl
启动mosquitto:net start mosquitto//需要管理员身份运行
停止mosquitto:net stop mosquitto
查看当前所有服务的状态:net start
简单示例
发布:
QMqttClient client;
("127.0.0.1"); // 指定MQTT服务器地址
(1883); // 指定MQTT服务器端口
("zsl"); // 指定用户名
("zsl"); // 指定密码
QObject::connect(&client, &QMqttClient::connected, [&client]() {
qDebug() << "Connected to MQTT broker.";
// 发布消息到主题 "hello"
auto result = (QMqttTopicName("hello"), "这是一条测试消息");
if (result == -1) {
qDebug() << "Failed to publish message.";
} else {
qDebug() << "Message published.";
}
});
QObject::connect(&client, &QMqttClient::disconnected, []() {
qDebug() << "Disconnected from MQTT broker.";
});
QObject::connect(&client, &QMqttClient::errorChanged, [](QMqttClient::ClientError error) {
qDebug() << "MQTT Client Error:" << error;
});
();
订阅:
QMqttClient client;
("127.0.0.1"); // 指定MQTT服务器地址
(1883); // 指定MQTT服务器端口
("zsl"); // 指定用户名
("zsl"); // 指定密码
QObject::connect(&client, &QMqttClient::connected, [&client]() {
qDebug() << "Connected to MQTT broker.";
// 订阅主题 "hello"
QMqttSubscription *subscription = ("hello");
if (!subscription) {
qDebug() << "Failed to subscribe.";
} else {
qDebug() << "Subscribed to topic 'hello'.";
}
QObject::connect(subscription, &QMqttSubscription::messageReceived, [](const QMqttMessage &message) {
qDebug() << "Received message on topic" << () << ":" << ();
});
});
QObject::connect(&client, &QMqttClient::disconnected, []() {
qDebug() << "Disconnected from MQTT broker.";
});
QObject::connect(&client, &QMqttClient::errorChanged, [](QMqttClient::ClientError error) {
qDebug() << "MQTT Client Error:" << error;
});
();