目录
一、环境准备
二、客户端开发
2.1 下载并且编译MQTT
2.2 客户端开发准备
2.3 客户端开发源码
三、测试演示
一、环境准备
操作系统:centos7.5
链接: https://pan.baidu.com/s/1BRYQ13RTIgkyD0GDsXB1jQ 提取码: 29e2
MQTT服务器
使用EMQX作为服务器
安装地址:https://www.emqx.io/zh/downloads?os=CentOS
安装方法
//1. 可能需要先执行
curl -s https://assets.emqx.com/scripts/install-emqx-rpm.sh | sudo bash
yum update -y
//2. 然后
yum install -y epel-release
//3. 安装
yum install -y openssl-devel openssl11 openssl11-devel
sudo yum install emqx -y
// 启动emqx
sudo systemctl start emqx
// 设置开机启动
sudo systemctl enable emqx
// 查看状态
sudo systemctl status emqx
默认账号密码:
账号:admin
密码:public
二、客户端开发
客户端操作系统为:UOS
QT版本:5.11.3
// 安装qt
sudo apt-get install qtbase5-dev
sudo apt-get install qtcreator
/// 安装MQTT的编译依赖
sudo apt-get install qtdeclarative5-dev
sudo apt-get install libqt5websockets5-dev
sudo apt-get instal qtbase5-private-dev
sudo apt install qttools5-private-dev
2.1 下载并且编译MQTT
- 下载
下载地址:GitHub - qt/qtmqtt at v5.11.3
注意下载的版本需要与QT版本一致。
- 编译
打开工程
修改头文件引用。
// 在qtmqtt-5.11.3/qtmqtt-5.11.3/src目录下执行
mkdir build
sudo /usr/lib/x86_64-linux-gnu/qt5/bin/qmake ..
make -j16
编译的内容会自动同步到/lib中。
下图头文件需要拷贝出来在客户端项目中需要使用。
2.2 客户端开发准备
新建qt项目
引用MQTT文件
添加头文件
/usr/include/c++/8
2.3 客户端开发源码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qmqttclient.h"
#include <QPushButton>
#include <QLineEdit>
#include <QWidget>
#include <QHBoxLayout>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QMqttClient* pMqttClient_;
QPushButton * pPushBtn_;
QLineEdit * pLineEdit_;
QWidget *pWdgMain_;
QHBoxLayout *pLayoutMain_;
void initUI();
public slots:
void btnClick();
void brokerConnected();
void updateLogStateChange();
void brokerDisconnected();
void receiveMess(const QByteArray &, const QMqttTopicName &);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
,pMqttClient_(new QMqttClient(this))
, pPushBtn_(new QPushButton())
, pLineEdit_(new QLineEdit())
, pWdgMain_(new QWidget())
,pLayoutMain_(new QHBoxLayout())
{
pMqttClient_->setPort(1883);
pMqttClient_->setHostname("10.4.17.80");
pMqttClient_->connectToHost();
connect(pMqttClient_, &QMqttClient::connected, this, &MainWindow::brokerConnected);
connect(pMqttClient_, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
connect(pMqttClient_, &QMqttClient::disconnected, this, &MainWindow::brokerDisconnected);
connect(pMqttClient_, &QMqttClient::pingResponseReceived, this, [this]() {
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" PingResponse")
+ QLatin1Char('\n');
qDebug() << content;
});
initUI();
}
MainWindow::~MainWindow()
{
}
void MainWindow::initUI()
{
pLayoutMain_->addWidget(pLineEdit_);
pLayoutMain_->addWidget(pPushBtn_);
pWdgMain_->setLayout(pLayoutMain_);
setCentralWidget(pWdgMain_);
connect(pPushBtn_,&QPushButton::clicked,this,&MainWindow::btnClick);
}
void MainWindow::btnClick()
{
qInfo()<<"asdf:"<<pLineEdit_->text();
pMqttClient_->publish(QString("topic"),pLineEdit_->text().toLatin1());
}
void MainWindow::brokerConnected()
{
qInfo() << "Connected!";
if(pMqttClient_->state() == QMqttClient::Connected){
pMqttClient_->subscribe(QString("topic"), 0);
connect(pMqttClient_, SIGNAL(messageReceived(QByteArray,QMqttTopicName)), this, SLOT(receiveMess(QByteArray,QMqttTopicName)));
}
}
void MainWindow::updateLogStateChange()
{
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(": State Change")
+ QString::number(pMqttClient_->state())
+ QLatin1Char('\n');
qInfo() << content;
}
void MainWindow::brokerDisconnected()
{
qInfo() << "Disconnected!";
}
void MainWindow::receiveMess(const QByteArray &message, const QMqttTopicName &topic)
{
QString content;
content = QDateTime::currentDateTime().toString() + QLatin1Char('\n');
content += QLatin1String(" Received Topic: ") + topic.name() + QLatin1Char('\n');
content += QLatin1String(" Message: ") + message + QLatin1Char('\n');
qInfo() << content;
}
#-------------------------------------------------
#
# Project created by QtCreator 2023-05-11T17:24:28
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MQTTClient
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h \
include/QtMqtt/QtMqttDepends
unix:!macx: LIBS += -L$$PWD/../../../../../../../../usr/lib/ -lQt5Mqtt
INCLUDEPATH += /usr/include/c++/8
INCLUDEPATH += $$PWD/../../../../../../../../usr/include
DEPENDPATH += $$PWD/../../../../../../../../usr/include
INCLUDEPATH += $$PWD/include
三、测试演示
程序运行时自动订阅
查看服务器
多运行几个客户端。
订阅都是同一个主题。
在其中一个客户端中输入内容并且点击按钮发布,订阅了相同主题的客户端都能收到信息。