0%

linux下mqtt源码交叉编译和应用

1 、下载压缩包 解压

paho.mqtt.c-master.zip
直接下载源码即可,根据自己想要的版本下载就好,一般选最新稳定版
下载链接:
github:https://github.com/eclipse/paho.mqtt.c/releases
gitee:https://gitee.com/IOTTS/paho.mqtt.c.git

img

2、编译linux下源码包 直接在目录下执行make命令

img

生成动态库在 build/output/ 目录下,头文件目录 srt/目录下。可以将动态库和头文件一个拷贝出来,放在自己应用的源码里面可以直接使用。

img

3、嵌入式linux平台交叉编译

将Makefile中的gcc换成嵌入式交叉编译工具 :(注意如果已经生成过bulid文件 就不会继续执行makefile,需要删除以前的生成文件,或者直接将压缩包重新解压,直接修改makefile文件后执行make命令编译)
makefile将编译器换掉CC= arm-arago-linux-gnueabi-gcc

img

操作和步骤2相同,只是生产的文件是响应嵌入式linux平台的动态库

4、MQTT应用开发

Demo程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "src/MQTTClient.h" /* MQTT头文件的位置 */
#include <sys/time.h>
#include <termios.h>
#include <iostream>

#define ADDRESS "tcp://localhost:1883" /* 指定服务器的ip地址 这里使用本机的 mosquitto 进行测试使用 */
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L

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

MQTTClient_message *receivemsg = NULL ;
char* topicName_rev = NULL;
int topicLen_rev;

int rc;
int i;

MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 60;
conn_opts.cleansession = 1;

if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
pubmsg.payload = (void *)PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_subscribe(client, "test", 1); /* 订阅一个客户端 的一个话题*/
usleep(10000);
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); /* 发送消息 */
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);
for(;;)
{
if(MQTTClient_isConnected(client) == true) /* 检测连接状态 */
{
printf("alive \n");
}else{
printf(" no alive \n");
break;
}
rc = MQTTClient_receive(client,&topicName_rev, &topicLen_rev, &receivemsg,5000); /* 接收消息 */
if(rc == MQTTCLIENT_SUCCESS)
{
printf("Message REv %d delivered\n", rc);
printf("topicName: %s topicName_LEN: %d \n", topicName_rev,topicLen_rev);
if(topicName_rev != NULL) /* 滤掉心跳包 */
{
printf("topicName: ");
for(i=0;i<topicLen_rev;i++)
{
printf(" %c ", topicName_rev[i]);
}
printf("\n");
printf("Data: %s len:%d msgid: %d \n",(char *)receivemsg->payload,receivemsg->payloadlen,receivemsg->msgid);
if(strcmp((char *)receivemsg->payload,"ESC") == 0)
{
printf("ESC \n");
break;
}
}
}

usleep(10000);
usleep(100000);
}
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}

编译 (注意我这边将linux平台下和嵌入式linux平台下的动态库分别放在linux_mqtt_c 和arm_mqtt_c 两个文件夹下,就只直接将build文件下的内容直接全部拷贝到响应的文件夹中)
linux平台下编译 (动态库使用方法这里不做介绍了)

1
g++  hellotwo.cpp  -o hello -L./linux_mqtt_c -lpaho-mqtt3c

img

嵌入式linux平台下编译 (生成的文件需要在相应的目标平台下执行)

1
arm-arago-linux-gnueabi-g++ hellotwo.cpp -o hello -L./arm_mqtt_c -lpaho-mqtt3c

img

5、本机mosquitto服务器搭建

安装 mosquitto这里不做介绍了,可以参考下面文章
MQTT代理之Mosquitto-centos环境下搭建
通过Docker安装部署Mosquitto服务器
启动代理服务: mosquitto -v

img

另起终端执行刚才的测试程序,服务器端会看到连接状态和订阅的信息和心跳的状态,应用端会直接检测连接状态和接收数据

img

gcc -o dp-01 serial_test.c cJSON.c log.c -lpaho-mqtt3a -lpthread