QT Creator+ ARM Linux开发MQTT程序


前言

一、确保开发板文件系统有下面的.so库。

STM32MP157-QT文件系统移植MQTT已经做了,即ARM Linux文件系统已经安装了对应的MQTT库,那么你的文件系统应该有下面的东西:
就是.so库
在这里插入图片描述
下面开始搭建工程

二、QT Creator+ ARM Linux开发MQTT程序

2.1、新建一个QT项目,这里最好选非QT项目。

新建一个QT项目,这里最好选非QT项目,这里不再细讲。
在这里插入图片描述

2.2、拷贝相应的库和头文件。

首先看一下paho.mqtt.c编译完了以后有哪些文件,文件还是蛮多的,MQTT基于UDP做了一些封装。
我们关心的是下面这些东西

  1. build/output目录下的.so库文件,
  2. src目录下的.h头文件,
  3. build/output/samples目录下是mqtt client的一些使用例子。
root@osboxes:/home/osboxes/paho.mqtt.c# tree
.
├── about.html
├── android
│   └── Android.mk
├── appveyor.yml
├── build
│   ├── output
│   │   ├── libpaho-mqtt3a.so -> libpaho-mqtt3a.so.1
│   │   ├── libpaho-mqtt3a.so.1 -> libpaho-mqtt3a.so.1.3
│   │   ├── libpaho-mqtt3a.so.1.3
│   │   ├── libpaho-mqtt3as.so -> libpaho-mqtt3as.so.1
│   │   ├── libpaho-mqtt3as.so.1 -> libpaho-mqtt3as.so.1.3
│   │   ├── libpaho-mqtt3as.so.1.3
│   │   ├── libpaho-mqtt3c.so -> libpaho-mqtt3c.so.1
│   │   ├── libpaho-mqtt3c.so.1 -> libpaho-mqtt3c.so.1.3
│   │   ├── libpaho-mqtt3c.so.1.3
│   │   ├── libpaho-mqtt3cs.so -> libpaho-mqtt3cs.so.1
│   │   ├── libpaho-mqtt3cs.so.1 -> libpaho-mqtt3cs.so.1.3
│   │   ├── libpaho-mqtt3cs.so.1.3
│   │   ├── paho_c_version
│   │   ├── samples
│   │   │   ├── MQTTAsync_publish
│   │   │   ├── MQTTAsync_subscribe
│   │   │   ├── MQTTClient_publish
│   │   │   ├── MQTTClient_publish_async
│   │   │   ├── MQTTClient_subscribe
│   │   │   ├── paho_c_pub
│   │   │   ├── paho_cs_pub
│   │   │   ├── paho_cs_sub
│   │   │   └── paho_c_sub
│   │   └── test
│   │       ├── sync_client_test
│   │       ├── test1
│   │       ├── test10
│   │       ├── test11
│   │       ├── test15
│   │       ├── test2
│   │       ├── test3
│   │       ├── test4
│   │       ├── test45
│   │       ├── test5
│   │       ├── test6
│   │       ├── test9
│   │       ├── test95
│   │       ├── test_mqtt4async
│   │       └── test_mqtt4sync
│   └── VersionInfo.h
├── build.xml
├── cbuild.bat
├── cmake
│   ├── CPackDebConfig.cmake.in
│   ├── toolchain.linux-arm11.cmake
│   ├── toolchain.win32.cmake
│   └── toolchain.win64.cmake
├── CMakeLists.txt
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── deploy_rsa.enc
├── dist
│   ├── Makefile
│   └── paho-c.spec
├── doc
│   ├── CMakeLists.txt
│   ├── DoxyfileV3AsyncAPI.in
│   ├── DoxyfileV3ClientAPI.in
│   ├── DoxyfileV3ClientInternal.in
│   ├── man
│   │   └── man1
│   │       ├── paho_c_pub.1
│   │       ├── paho_cs_pub.1
│   │       ├── paho_cs_sub.1
│   │       └── paho_c_sub.1
│   └── pahologo.png
├── edl-v10
├── epl-v20
├── LICENSE
├── Makefile
├── notice.html
├── PULL_REQUEST_TEMPLATE.md
├── README.md
├── src
│   ├── Base64.c
│   ├── Base64.h
│   ├── Clients.c
│   ├── Clients.h
│   ├── CMakeLists.txt
│   ├── Heap.c
│   ├── Heap.h
│   ├── LinkedList.c
│   ├── LinkedList.h
│   ├── Log.c
│   ├── Log.h
│   ├── Messages.c
│   ├── Messages.h
│   ├── MQTTAsync.c
│   ├── MQTTAsync.h
│   ├── MQTTClient.c
│   ├── MQTTClient.h
│   ├── MQTTClientPersistence.h
│   ├── MQTTExportDeclarations.h
│   ├── MQTTPacket.c
│   ├── MQTTPacket.h
│   ├── MQTTPacketOut.c
│   ├── MQTTPacketOut.h
│   ├── MQTTPersistence.c
│   ├── MQTTPersistenceDefault.c
│   ├── MQTTPersistenceDefault.h
│   ├── MQTTPersistence.h
│   ├── MQTTProperties.c
│   ├── MQTTProperties.h
│   ├── MQTTProtocolClient.c
│   ├── MQTTProtocolClient.h
│   ├── MQTTProtocol.h
│   ├── MQTTProtocolOut.c
│   ├── MQTTProtocolOut.h
│   ├── MQTTReasonCodes.c
│   ├── MQTTReasonCodes.h
│   ├── MQTTSubscribeOpts.h
│   ├── MQTTTime.c
│   ├── MQTTTime.h
│   ├── MQTTVersion.c
│   ├── mutex_type.h
│   ├── OsWrapper.c
│   ├── OsWrapper.h
│   ├── samples
│   │   ├── CMakeLists.txt
│   │   ├── MQTTAsync_publish.c
│   │   ├── MQTTAsync_publish_time.c
│   │   ├── MQTTAsync_subscribe.c
│   │   ├── MQTTClient_publish_async.c
│   │   ├── MQTTClient_publish.c
│   │   ├── MQTTClient_subscribe.c
│   │   ├── paho_c_pub.c
│   │   ├── paho_cs_pub.c
│   │   ├── paho_cs_sub.c
│   │   ├── paho_c_sub.c
│   │   ├── pubsub_opts.c
│   │   └── pubsub_opts.h
│   ├── SHA1.c
│   ├── SHA1.h
│   ├── SocketBuffer.c
│   ├── SocketBuffer.h
│   ├── Socket.c
│   ├── Socket.h
│   ├── SSLSocket.c
│   ├── SSLSocket.h
│   ├── StackTrace.c
│   ├── StackTrace.h
│   ├── Thread.c
│   ├── Thread.h
│   ├── Tree.c
│   ├── Tree.h
│   ├── utf-8.c
│   ├── utf-8.h
│   ├── VersionInfo.h.in
│   ├── WebSocket.c
│   └── WebSocket.h
├── test
│   ├── CMakeLists.txt
│   ├── dll-copy.cmake
│   ├── mqttsas.py
│   ├── MQTTV3112.py
│   ├── MQTTV311.py
│   ├── MQTTV5.py
│   ├── openssl
│   │   └── applink.c
│   ├── python
│   │   ├── controlVMnetworkstate.py
│   │   ├── mqttasync_module.c
│   │   ├── mqttclient_module.c
│   │   ├── setup.py
│   │   ├── test1.py
│   │   ├── test2.py
│   │   └── test_offline.py
│   ├── ssl
│   │   ├── all-ca.crt
│   │   ├── capath
│   │   │   ├── 374b031c.0 -> server.pem
│   │   │   ├── ab88c291.0 -> test-root-ca.pem
│   │   │   ├── server.pem
│   │   │   └── test-root-ca.pem
│   │   ├── client.crt
│   │   ├── client.csr
│   │   ├── client-expired.crt
│   │   ├── client-expired.csr
│   │   ├── client.key
│   │   ├── client.pem
│   │   ├── client-revoked.crt
│   │   ├── client-revoked.csr
│   │   ├── client-revoked.key
│   │   ├── crl.pem
│   │   ├── gen.sh
│   │   ├── openssl.cnf
│   │   ├── rootCA
│   │   │   ├── crlnumber
│   │   │   ├── index.txt
│   │   │   ├── index.txt.attr
│   │   │   ├── index.txt.attr.old
│   │   │   ├── index.txt.old
│   │   │   ├── newcerts
│   │   │   │   ├── 01.pem
│   │   │   │   └── 02.pem
│   │   │   ├── serial
│   │   │   └── serial.old
│   │   ├── server.crt
│   │   ├── server.csr
│   │   ├── server-expired.crt
│   │   ├── server-expired.csr
│   │   ├── server.key
│   │   ├── signingCA
│   │   │   ├── crlnumber
│   │   │   ├── crlnumber.old
│   │   │   ├── index.txt
│   │   │   ├── index.txt.attr
│   │   │   ├── index.txt.attr.old
│   │   │   ├── index.txt.old
│   │   │   ├── newcerts
│   │   │   │   ├── 01.pem
│   │   │   │   ├── 02.pem
│   │   │   │   ├── 03.pem
│   │   │   │   └── 04.pem
│   │   │   ├── serial
│   │   │   └── serial.old
│   │   ├── test-alt-ca.crt
│   │   ├── test-alt-ca.csr
│   │   ├── test-alt-ca.key
│   │   ├── test-bad-root-ca.crt
│   │   ├── test-bad-root-ca.key
│   │   ├── test-fake-root-ca.crt
│   │   ├── test-fake-root-ca.key
│   │   ├── test-root-ca.crt
│   │   ├── test-root-ca.key
│   │   ├── test-signing-ca.crt
│   │   ├── test-signing-ca.csr
│   │   └── test-signing-ca.key
│   ├── sync_client_test.c
│   ├── test10.c
│   ├── test11.c
│   ├── test15.c
│   ├── test1.c
│   ├── test2.c
│   ├── test3.c
│   ├── test45.c
│   ├── test4.c
│   ├── test5.c
│   ├── test6.c
│   ├── test8.c
│   ├── test95.c
│   ├── test9.c
│   ├── test_connect_destroy.c
│   ├── test_issue373.c
│   ├── test_mqtt4async.c
│   ├── test_mqtt4sync.c
│   ├── test_persistence.c
│   ├── test_sync_session_present.c
│   ├── thread.c
│   └── tls-testing
│       ├── Dockerfile
│       ├── keys
│       │   ├── all-ca.crt
│       │   ├── server
│       │   │   ├── server.crt
│       │   │   ├── server.key
│       │   │   ├── server-mitm.crt
│       │   │   └── server-mitm.key
│       │   ├── test-root-ca.crt
│       │   └── test-signing-ca.crt
│       ├── mosquitto.conf
│       ├── mosquitto-docker.conf
│       ├── mosquitto.psk
│       └── mosquitto.pw
├── test_package
│   ├── CMakeLists.txt
│   ├── conanfile.py
│   └── test_package.c
├── travis-build.sh
├── travis-deploy.sh
├── travis-install.sh
├── travis-setup-deploy.sh
├── version.major
├── version.minor
└── version.patch

25 directories, 256 files

把头文件和库文件拷贝到工程路径里面,

  1. 库文件
├── build
│   ├── output
│   │   ├── libpaho-mqtt3a.so -> libpaho-mqtt3a.so.1
│   │   ├── libpaho-mqtt3a.so.1 -> libpaho-mqtt3a.so.1.3
│   │   ├── libpaho-mqtt3a.so.1.3
│   │   ├── libpaho-mqtt3as.so -> libpaho-mqtt3as.so.1
│   │   ├── libpaho-mqtt3as.so.1 -> libpaho-mqtt3as.so.1.3
│   │   ├── libpaho-mqtt3as.so.1.3
│   │   ├── libpaho-mqtt3c.so -> libpaho-mqtt3c.so.1
│   │   ├── libpaho-mqtt3c.so.1 -> libpaho-mqtt3c.so.1.3
│   │   ├── libpaho-mqtt3c.so.1.3
│   │   ├── libpaho-mqtt3cs.so -> libpaho-mqtt3cs.so.1
│   │   ├── libpaho-mqtt3cs.so.1 -> libpaho-mqtt3cs.so.1.3
│   │   ├── libpaho-mqtt3cs.so.1.3
│   │   ├── paho_c_version
  1. 头文件

(其实好多的头文件这里用不上,C文件这里也用不上)

├── src
│   ├── Base64.c
│   ├── Base64.h
│   ├── Clients.c
│   ├── Clients.h
│   ├── CMakeLists.txt
│   ├── Heap.c
│   ├── Heap.h
│   ├── LinkedList.c
│   ├── LinkedList.h
│   ├── Log.c
│   ├── Log.h
│   ├── Messages.c
│   ├── Messages.h
│   ├── MQTTAsync.c
│   ├── MQTTAsync.h
│   ├── MQTTClient.c
│   ├── MQTTClient.h
│   ├── MQTTClientPersistence.h
│   ├── MQTTExportDeclarations.h
│   ├── MQTTPacket.c
│   ├── MQTTPacket.h
│   ├── MQTTPacketOut.c
│   ├── MQTTPacketOut.h
│   ├── MQTTPersistence.c
│   ├── MQTTPersistenceDefault.c
│   ├── MQTTPersistenceDefault.h
│   ├── MQTTPersistence.h
│   ├── MQTTProperties.c
│   ├── MQTTProperties.h
│   ├── MQTTProtocolClient.c
│   ├── MQTTProtocolClient.h
│   ├── MQTTProtocol.h
│   ├── MQTTProtocolOut.c
│   ├── MQTTProtocolOut.h
│   ├── MQTTReasonCodes.c
│   ├── MQTTReasonCodes.h
│   ├── MQTTSubscribeOpts.h
│   ├── MQTTTime.c
│   ├── MQTTTime.h
│   ├── MQTTVersion.c
│   ├── mutex_type.h
│   ├── OsWrapper.c
│   ├── OsWrapper.h

2.3、添加相应的头文件和库文件

在这里插入图片描述

这里选择外部库和内部库都是可以的,区别是外部库可以顺便把相应的头文件加进来,内部库的话得分两步
在这里插入图片描述

选完了之后.pro文件是这样的,可以看到添加了相应的库文件和头文件。

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.c

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lpaho-mqtt3a
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lpaho-mqtt3a
else:unix: LIBS += -L$$PWD/lib/ -lpaho-mqtt3a

INCLUDEPATH += $$PWD/src
DEPENDPATH += $$PWD/src

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lpaho-mqtt3as
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lpaho-mqtt3as
else:unix: LIBS += -L$$PWD/lib/ -lpaho-mqtt3as

INCLUDEPATH += $$PWD/src
DEPENDPATH += $$PWD/src

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lpaho-mqtt3c
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lpaho-mqtt3c
else:unix: LIBS += -L$$PWD/lib/ -lpaho-mqtt3c

INCLUDEPATH += $$PWD/src
DEPENDPATH += $$PWD/src

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/release/ -lpaho-mqtt3cs
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/debug/ -lpaho-mqtt3cs
else:unix: LIBS += -L$$PWD/lib/ -lpaho-mqtt3cs

INCLUDEPATH += $$PWD/src
DEPENDPATH += $$PWD/src


Main.c里面添加下面的程序代码,编译,没有报错,那就是OK的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"

#define ADDRESS     "192.168.16.56:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
         printf("Failed to create client, return code %d\n", rc);
         exit(EXIT_FAILURE);
    }

    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }

    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = (int)strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
    {
         printf("Failed to publish message, return code %d\n", rc);
         exit(EXIT_FAILURE);
    }

    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
    rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);

    if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
        printf("Failed to disconnect, return code %d\n", rc);
    MQTTClient_destroy(&client);
    return rc;
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
移植Qt MQTTARM Linux 平台上需要以下几个步骤: 1. 安装交叉编译工具链:在Linux下,我们需要安装适用于ARM架构的交叉编译工具链,例如arm-linux-gnueabihf。这个工具链将会提供编译Qt和其它依赖库所需的工具和环境。 2. 下载Qt源码:从Qt官方网站上下载适用于ARM LinuxQt源码。选择适合您的版本和需要的模块。确保选择支持MQTT的模块。 3. 配置编译选项:解压源码后,使用命令行进入源码目录。运行`configure`脚本来配置编译选项,例如指定交叉编译工具链和目标平台。确保选择MQTT模块和适当的依赖库,在这种情况下是Paho MQTT C库和OpenSSL库。 4. 运行make:在配置完成后,运行`make`命令来编译Qt源码。这个过程可能会需要一段时间,具体取决于您的电脑性能和源码大小。 5. 安装Qt:编译完成后,使用`make install`命令来安装Qt到指定的路径。确保您选择正确的安装路径,以便在ARM Linux上使用。 6. 创建Qt MQTT项目:现在,您可以在ARM Linux上创建基于Qt MQTT的项目了。使用Qt Creator工具创建一个新项目,并在项目配置中添加必要的库和模块。确保设置正确的编译器和目标平台。 7. 编译和部署:通过Qt Creator编译和构建项目。然后,将生成的可执行文件和任何依赖的库文件复制到ARM Linux设备上。确保正确配置和安装依赖库,包括Paho MQTT C库和OpenSSL库。 8. 测试和调试:最后,在ARM Linux设备上运行和测试您的Qt MQTT应用程序。使用调试工具(例如GDB)来调试任何可能出现的问题,确保应用程序正常运行。 通过以上步骤,您应该能够成功将Qt MQTT移植到ARM Linux平台上,并在设备上运行和测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小男孩和胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值