当前位置:网站首页>QT(37)-mosquitto-MQTT客户端
QT(37)-mosquitto-MQTT客户端
2022-07-21 03:16:00 【多云的夏天】
1.MQTT服务器端下载安装mosquitto
2.MQTT客户端 QT版
1.MQTT服务器端下载安装mosquitto
1.1安装服务器端
sudo apt-get install mosquitto
1.2安装客户端
sudo apt-get install mosquitto-clients
1.3安装设备端
sudo apt-get install mosquitto-dev
1.4修改配置文件
cd /etc/mosquitto
vim mosquitto.conf
allow_anonymous false #表示不可以匿名访问
password_file /etc/mosquitto/pwfile.example #用户和密码放在pwfile.example
1.5重启服务
service mosquitto restart
1.6访问
#1.客户端:访问本地,用户名:test ;密码:[email protected];主题:11
mosquitto_sub -h localhost -p 1883 -u test -P [email protected] -t "11"
#2.服务器端可以查看到一个连接
lsof -i | grep mosquitto
#3.服务器端发布
mosquitto_pub -h localhost -p 1883 -u test -P [email protected] -t "11" -m "helloworld"
#此时客户端可以看到发布了
2.QT版的MQTT客户端
2.1环境配置:
下载 https://github.com/qt/qtmqtt/tree/5.15
配置:项目->配置
双击.pro文件做配置
常见问题:
1.没有头文件:
把头文件复制后放入QT的安装目录下。文件夹名称位QtMqtt
D:\ProgramData\Qt\Qt5.14.2\5.14.2\mingw73_32\include\
2.编译后出错:
修改成:
connect(socket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)> (&QAbstractSocket::error),
this, static_cast<void(QMqttConnection::*)(QAbstractSocket::SocketError)>(&QMqttConnection::transportError) );
3.复制被编译好的两个文件:bin目录下:Qt5Mqtt.dll,Qt5Mqttd.dll
复制后放入QT的安装目录下 D:\ProgramData\Qt\Qt5.14.2\5.14.2\mingw73_32\bin
2.2第一个demo程序:
1.pro文件设置:network
#1.添加core
QT += core gui network
#2.添加lib
LIBS +=D:\QTPrj\MQTT\build-qtmqtt-Desktop_Qt_5_14_2_MinGW_32_bit-Debug\lib\Qt*.dll
2..h文件
#include <QtMqtt/qmqttclient.h>
{
private slots:
void on_connectButton_clicked():
void connectSuccessSolt(); //连接成功
void recvMessageSolt();
private:
QMqttClient *client;
}
3.cpp文件
#include <QMessageBox>
Widget::~Widget()
{
ui->setupUi(this);
client=new QMqttClient;
client->setHostname("");
client->setPort(1883);
client->setUsername("test");
connect(client,&QMqttClient::connected,this,&Widget::connectSuccessSlot);
}
//1.开始连接服务器
void Widget::on_connectButton_clicked()
{
client->connectToHost();
}
//连接成功
void Widget::connectSuccessSlot()
{
QMessageBox::information(this,"连接提示","连接成功"); connect(client,&QMqttClient::messageReceived,this,&Widget::recvMessageSolt);
connect(client,&QMqttClient::disconnected,[this](){
QMessageBox::warning(this,"连接提示","服务器断开");
});
}
//处理消息
void Widget::recvMessageSolt(const QByteArray &ba,const QMqttTopicName &topic)
{
QString str= topic.name()+QString(ba);
ui->textEdit->setText(str);
}
//订阅
void Widget::on_subButton_clicked()
{
client->subscribe(ui->subTopicEdit->text());
}
//发布
void Widget::on_pubButton_clicked()
{
QString msg=ui->msgEdit->text();
QByteArray ba;
ba.append(msg);
client->publish(ui->pubTopicEdit->text(),ba);
}
3. openssl 里添加 两个库。
边栏推荐
- What is the difference between session and cookies?
- [MCU simulation project] 4 × 4 matrix keyboard scanning
- [experience sharing] mathematical modeling paper format requirements and summary of common problems
- 解决跨域问题的8种方案(最新最全)
- Jujube technology CEO's monthly DDC briefing (phase III) -- Introduction to new functions of DDC network and review of essence of Wenchang chain upgrade plan
- Protocole TCP
- 电子招标采购商城系统:优化传统采购业务,提速企业数字化升级
- 软件测试面试题:软件测试各个阶段通常完成什么工作?各个阶段的结果文件是什么?包括什么内容?
- Essential tools for streamlit Data Science
- 论文翻译解读:Logmap:Logic-based and scalable ontology matching
猜你喜欢
Obtain the screenshot of the front panel through programming in LabVIEW
[pytoch] learning notes (I) create your own data set
How easyexcel exports files in a project
ThreadLocal
不想醒来因为好吃
Phpstudy_ Pro builds sqli labs shooting range for SQL injection test
What is gamefi?
Synchronized lock range
[information retrieval] implementation of information retrieval system
V853开发板硬件资料——RISC-V核E907用户手册
随机推荐
regular expression
[cloud native kubernetes] how to use health check under kubernetes cluster
知识图谱问答技术实践与探索
Phpstudy_pro搭建Sqli-labs靶场,进行SQL注入测试
Unity—Input类
软件测试面试题:BUG管理工具的跟踪过程(用BugZilla为例子)
PyTorch 深度学习实践 第2讲/作业(Linear Model)
[QT入门篇]信号槽机制
[OBS] release signed construction based on cmake
What is gamefi?
MYSQL07_获取日期函数、聚合函数、字符串函数
C language introduction practice (10): module taking problem
KingbaseES V8R6有什么办法,默认不读取sys_catalog下的系统视图?
V853 development board hardware data - risc-v core e907 user manual
Software testing interview question: what is the strategy of software testing?
强制卸载额外域控制器
According to the framework's swagger interface document
软件测试面试题:您认为在测试人员同开发人员的沟通过程中,如何提高沟通的效率和改善沟通的效果?维持测试人员同开发团队中其他成员良好的人际关系的关键是什么?
Advanced C language (XIV) - Document Management
kubernetes的这几种存储卷,别再傻傻分不清了