1. 本文下载的是最新版本paho.mqtt.c-master.zip,下载地址:github.com/eclipse-pah…
编译 paho.mqtt.c 库之前,需要先编译其依赖库:openssl,见博客君正X2670-编译libcurl/openssl/wpa_supplicant库1.编译libcurl库 1.1下载c - 掘金
2. paho.mqtt.c库交叉编译
2.1 CMakeLists.txt文件中开启编译sample和SSL
option(PAHO_BUILD_SAMPLES "Build sample programs" TRUE)
2.2 在根目录新建Build.mk文件
LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
CMAKE_PATH=$(LOCAL_PATH)
LOCAL_MODULE:= MQTT_MASTER
LOCAL_MODULE_TAGS :=optional
include $(BUILD_CMAKE_DEVICE)
rm -rf ./out/product/x2670halley.v10_nand_5.10-eng/obj/packages/example/lvgl_demo
make MQTT_MASTER
编译结果如下:
-- Install configuration: "Release"
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTAsync_publish.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTAsync_publish_time.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTAsync_subscribe.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTClient_publish.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTClient_publish_async.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/MQTTClient_subscribe.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/paho_c_pub.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/paho_c_sub.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/paho_cs_pub.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/paho_cs_sub.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/samples/pubsub_opts.c
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/CONTRIBUTING.md
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/epl-v20
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/edl-v10
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/README.md
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/share/doc/Eclipse Paho C/notice.html
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3c.so.1.3.15
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3c.so.1
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3c.so
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3a.so.1.3.15
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3a.so.1
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3a.so
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTVersion
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTAsync.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTClient.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTClientPersistence.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTProperties.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTReasonCodes.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTSubscribeOpts.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/include/MQTTExportDeclarations.h
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3cs.so.1.3.15
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3cs.so.1
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3cs.so
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3as.so.1.3.15
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3as.so.1
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/libpaho-mqtt3as.so
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig.cmake
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfig-release.cmake
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/lib/cmake/eclipse-paho-mqtt-c/eclipse-paho-mqtt-cConfigVersion.cmake
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/paho_c_sub
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/paho_c_pub
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/paho_cs_sub
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/paho_cs_pub
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTAsync_subscribe
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTAsync_publish
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTAsync_publish_time
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTClient_subscribe
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTClient_publish
-- Up-to-date: /root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/system/usr/bin/MQTTClient_publish_async
make[1]: Leaving directory '/root/rc26/rc26/out/product/x2670halley.v10_nand_5.10-eng/obj/packages/example/lvgl_demo/paho.mqtt.c-master/MQTT_MASTER'
/root/rc26/rc26
install -D /dev/null out/product/x2670halley.v10_nand_5.10-eng/obj/packages/example/lvgl_demo/paho.mqtt.c-master/MQTT_MASTER/.stampinstall
mkdir -p out/product/x2670halley.v10_nand_5.10-eng/obj/DEPANNER/MQTT_MASTER-intermediate/
install -D /dev/null out/product/x2670halley.v10_nand_5.10-eng/obj/DEPANNER/MQTT_MASTER-intermediate/stamp_build
mkdir -p out/product/x2670halley.v10_nand_5.10-eng/obj/DEPANNER/MQTT_MASTER-intermediate/
install -D /dev/null out/product/x2670halley.v10_nand_5.10-eng/obj/DEPANNER/MQTT_MASTER-intermediate/stamp_install
2.3 paho.mqtt部署到嵌入式开发板
将paho.mqtt编译生成的可执行文件、so库文件等拷贝到嵌入式板子上面的/usr/bin、、/usr/lib目录下面
3. paho.mqtt.c库写一个MQTT客户端
这是使用了 libpaho-mqtt3a.so 进行订阅消息的源码,源码路径在源码的这个路径:paho.mqtt.c-1.3.15/src/samples/MQTTAsync_subscribe.c,只更改了服务器地址。完整代码如下:
/*******************************************************************************
* Copyright (c) 2012, 2025 IBM Corp., Ian Craggs
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "127.0.0.1:1883"//"tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int disc_finished = 0;
int subscribed = 0;
int finished = 0;
void onConnect(void* context, MQTTAsync_successData* response);
void onConnectFailure(void* context, MQTTAsync_failureData* response);
void connlost(void *context, char *cause)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
printf("\nConnection lost\n");
if (cause)
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
}
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName);
return 1;
}
void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Disconnect failed, rc %d\n", response->code);
disc_finished = 1;
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
disc_finished = 1;
}
void onSubscribe(void* context, MQTTAsync_successData* response)
{
printf("Subscribe succeeded\n");
subscribed = 1;
}
void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
printf("Subscribe failed, rc %d\n", response->code);
finished = 1;
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Connect failed, rc %d\n", response->code);
finished = 1;
}
void onConnect(void* context, MQTTAsync_successData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc;
printf("Successful connection\n");
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
opts.onSuccess = onSubscribe;
opts.onFailure = onSubscribeFailure;
opts.context = client;
if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start subscribe, return code %d\n", rc);
finished = 1;
}
}
int main(int argc, char* argv[])
{
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
int rc;
int ch;
const char* uri = (argc > 1) ? argv[1] : ADDRESS;
printf("Using server at %s\n", uri);
if ((rc = MQTTAsync_create(&client, uri, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
!= MQTTASYNC_SUCCESS)
{
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
goto exit;
}
if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to set callbacks, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
while (!subscribed && !finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
if (finished)
goto exit;
do
{
ch = getchar();
} while (ch!='Q' && ch != 'q');
disc_opts.onSuccess = onDisconnect;
disc_opts.onFailure = onDisconnectFailure;
if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
while (!disc_finished)
{
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
}
destroy_exit:
MQTTAsync_destroy(&client);
exit:
return rc;
}
交叉编译然后拷贝可执行文件MQTTAsync_subscribe到嵌入式板子/usr/bin,给可执行程序chmod 644 MQTTAsync_subscribe,
在嵌入式板子运行./MQTTAsync_subscribe:
/usr/bin # ./MQTTAsync_subscribe
Using server at 127.0.0.1:1883
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1
Press Q to quit
Subscribe succeeded //下面发送消息之后,接收到的信息
Message arrived
topic: MQTT Examples
message: Hello World!
4. 发布MQTTAsync_publish.c
/*******************************************************************************
* Copyright (c) 2012, 2025 IBM Corp., Ian Craggs
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "127.0.0.1:1883"//"tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int finished = 0;
void connlost(void *context, char *cause)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
printf("\nConnection lost\n");
if (cause)
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
}
}
void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Disconnect failed\n");
finished = 1;
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
finished = 1;
}
void onSendFailure(void* context, MQTTAsync_failureData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;
printf("Message send failed token %d error code %d\n", response->token, response->code);
opts.onSuccess = onDisconnect;
opts.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
void onSend(void* context, MQTTAsync_successData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
int rc;
printf("Message with token value %d delivery confirmed\n", response->token);
opts.onSuccess = onDisconnect;
opts.onFailure = onDisconnectFailure;
opts.context = client;
if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Connect failed, rc %d\n", response ? response->code : 0);
finished = 1;
}
void onConnect(void* context, MQTTAsync_successData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
int rc;
printf("Successful connection\n");
opts.onSuccess = onSend;
opts.onFailure = onSendFailure;
opts.context = client;
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = (int)strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
{
/* not expecting any messages */
return 1;
}
int main(int argc, char* argv[])
{
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
const char* uri = (argc > 1) ? argv[1] : ADDRESS;
printf("Using server at %s\n", uri);
if ((rc = MQTTAsync_create(&client, uri, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to create client object, return code %d\n", rc);
exit(EXIT_FAILURE);
}
if ((rc = MQTTAsync_setCallbacks(client, client, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to set callback, return code %d\n", rc);
exit(EXIT_FAILURE);
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (!finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
MQTTAsync_destroy(&client);
return rc;
}
交叉编译然后拷贝可执行文件MQTTAsync_publish到嵌入式板子/usr/bin,给可执行程序chmod 644 MQTTAsync_publish,
在嵌入式板子运行./MQTTAsync_subscribe:
/usr/bin # ./MQTTAsync_publish
Using server at 127.0.0.1:1883
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
原始博客【MQTT】paho.mqtt.c 库的“介绍、下载、交叉编译” 详解,以及编写MQTT客户端例子源码-CSDN博客