QMQTT快速入门

文章目录

  • QMQTT快速入门
    • 环境搭建
    • mosquitto 服务器和客户端配置
      • 服务器配置
      • 客户端配置
      • 模拟MQTT的发布订阅
    • QMQTT - Windows下的客户端
      • 项目代码展示
      • 遇到的问题

QMQTT快速入门

环境搭建

  • 准备一台linux设备和一台windows设备虚拟机也是可以的;
  • 安装mosquitto ;
  • 准备QMQTT环境 - windows下;

mosquitto 服务器和客户端配置

服务器配置

首先安装mosquitto

sudo apt install mosquitto

之后我们对配置文件进行修改

sudo vim /etc/mosquitto/mosquitto.conf

添加下面的内容

allow_anonymous false
password_file /etc/mosquitto/pwfile.example

我们可以新建用户

mosquitto_passwd -c /etc/mosquitto/pwfile.example test

之后通过如下命令查询服务占用的端口

lsof -i | grep mosquitto

客户端配置

首先安装客户端

sudo apt install mosquitto-clients

模拟MQTT的发布订阅

先重新打开一个终端,之后订阅一个mqtt的主题

mosquitto_sub -h localhost -p 1883 -u test -P 123456 -t "ccc"

再打开一个客户端发布对应主题的消息

mosquitto_pub -h localhost -p 1883 -u test -P 123456 -t "ccc" -m "Hello World"

这个时候我们可以看到对应的消息可以正常的收发则测试成功。

QMQTT - Windows下的客户端

编译会有单独的专栏,这里不做介绍,

我这里使用我自己编译好的QMQTT环境

项目代码展示

暂时还是简单的,后续会将功能逐步完善

CMakeList.txt

cmake_minimum_required(VERSION 3.23)
project(MQTTTest)
#指定C++标准
set(CMAKE_CXX_STANDARD 17)
#指定输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/output)
#自动编译QT文件
#set(CMAKE_PREFIX_PATH "C:/Qt/6.5.1/msvc2019_64")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
#开启包含当前编译目录
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#指定QT版本和对应的库
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets Network
        #        Core5Compat
        )
set(REQUIRED_LIBS_QUALIFIED Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Network
        #        Qt${QT_VERSION}::Core5Compat
        )
#寻找QT库
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
include_directories(src
        third_party/qmqtt/include
        )
file(GLOB HEADERS
        src/*.h
        )
file(GLOB SOURCES
        src/*.cpp
        )

file(GLOB UIS
        src/*.ui
        )
# 指定格式为utf-8
add_compile_options("$<$:/utf-8>")
add_compile_options("$<$:/utf-8>")

#增减windows库文件
if(WIN32)
    set(PLAT_FROM_DEP
            ws2_32.lib
            )
endif()

file(GLOB LIB_MQTT ${PROJECT_SOURCE_DIR}/third_party/qmqtt/lib/*.lib)
link_directories(${PROJECT_SOURCE_DIR}/third_party/qmqtt/lib)

#使用指定的源文件来生成目标可执行文件
add_executable(${PROJECT_NAME} main.cpp ${HEADERS} ${SOURCES} ${UIS})
if(WIN32)
    target_link_libraries(${PROJECT_NAME} ${PLAT_FROM_DEP})
endif()
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED} ${LIB_MQTT})

main.cpp

#include 
#include "mainwindow.h"
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    MainWindow mm;
    mm.show();
    return app.exec();
}

mainwindow.h

#ifndef MQTTTEST_MAINWINDOW_H
#define MQTTTEST_MAINWINDOW_H
#include 
#include "qmqtt.h"
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow() override;
protected:
    void initUi();
protected slots:
    void connectToHost();
    void connectSuccess();
    void error(const QMQTT::ClientError error);
private:
    QStatusBar *status_bar_;
    QMQTT::Client *client_;
};
#endif //MQTTTEST_MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    initUi();
    client_ = new QMQTT::Client(QHostAddress("192.168.0.113"), 1883, this);
    client_->setUsername("test");
    client_->setPassword("123456");
    connect(client_, &QMQTT::Client::connected, this, &MainWindow::connectSuccess);
    connect(client_, &QMQTT::Client::error, this, &MainWindow::error);
}

MainWindow::~MainWindow()
{

}

void MainWindow::initUi()
{
    QMenuBar *menuBar = new QMenuBar(this);
    setMenuBar(menuBar);
    QMenu *menu = new QMenu("操作", this);
    menuBar->addMenu(menu);
    QAction *action = new QAction("连接服务器", this);
    menu->addAction(action);
    status_bar_ = new QStatusBar(this);
    setStatusBar(status_bar_);
    connect(action, &QAction::triggered, this, &MainWindow::connectToHost);
}

void MainWindow::connectToHost()
{
    client_->connectToHost();
}

void MainWindow::connectSuccess()
{
    status_bar_->showMessage("连接成功");
    client_->subscribe("ccc");
    connect(client_, &QMQTT::Client::received, [this](const QMQTT::Message &message){
        std::cout << message.topic().toStdString() << std::endl;
        std::cout << message.payload().toStdString() << std::endl;
    });
}

void MainWindow::error(const QMQTT::ClientError error)
{
    std::cout << error << std::endl;
}

遇到的问题

连接失败 - 拒绝连接

  • 需要mosquitto 如下
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883

allow_anonymous false

password_file /etc/mosquitto/pwfile.example
  • 需要添加openssl动态库

你可能感兴趣的:(QT日常,qt,c++)