1 、下载压缩包 解压
paho.mqtt.c-master.zip
https://github.com/mqtt/mqtt.github.io/wiki/libraries
http://download.csdn.net/detail/zhanyongli2008/9908059
2、编译linux下源码包 直接在目录下执行make命令
生成动态库在 build/output/ 目录下,
头文件目录 srt/目录下。
可以将动态库和头文件一个拷贝出来,放在自己应用的源码里面可以直接使用。
3、嵌入式linux平台交叉编译
将Makefile中的gcc换成嵌入式交叉编译工具 :(注意如果已经生成过bulid文件 就不会继续执行makefile ,需要删除以前的生成文件,或者直接将压缩包重新解压,直接修改makefile文件后执行make命令编译)
makefile将编译器换掉CC= arm-arago-linux-gnueabi-gcc
操作和步骤2相同,只是生产的文件是响应嵌入式linux平台的动态库
4、MQTT应用开发
Demo程序
#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 ADDRESS "tcp://192.168.202.129:1883"
//#define ADDRESS "tcp://192.168.5.114:1883"
#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平台下编译 (动态库使用方法这里不做介绍了)
g++ hellotwo.cpp -o hello -L./linux_mqtt_c -lpaho-mqtt3c
嵌入式linux平台下编译 (生成的文件需要在相应的目标平台下执行)
arm-arago-linux-gnueabi-g++ hellotwo.cpp -o hello -L./arm_mqtt_c -lpaho-mqtt3c
5、本机mosquitto服务器搭建
安装 mosquitto这里不做介绍了
启动代理服务: mosquitto -v
另起终端执行刚才的测试程序,服务器端会看到连接状态和订阅的信息和心跳的状态,应用端会直接检测连接状态和接收数据