基于mqtt的C语言源码,基于paho.mqtt.c库实现QT封装调用

本文介绍了如何在Windows下使用CMake编译带有SSL支持的paho.mqtt.c库,并展示了如何在C语言中使用MQTTClient发布和订阅消息。然后,文章提供了一个QT框架下的MQTTWrapper类,该类使用工作线程来封装MQTT操作,确保与UI线程不冲突。MQTTWrapper实现了消息队列和多路事件机制,用于在后台处理MQTT的发布、订阅和回调功能。
摘要由CSDN通过智能技术生成

windows下使用cmake编译:

b240c273d006

image.png

默认选项是没有选择静态库和SSL的,上面我都选中了。

编译SSL版本需要openssl库支持,windows现成的开发包请在这里下载:

http://slproweb.com/products/Win32OpenSSL.html

安装好openssl库后,需要设置OPENSSL_ROOT_DIR环境变量,如下所示:

b240c273d006

image.png

如果上面没有问题的话,就会在build下生成VS项目文件,找到Eclipse Paho C.sln并打开,全部重新生成,生成的库及位置如下:

b240c273d006

image.png

库生成后,就可以来写代码了。将源码中的头文件和刚刚生成的库都收集到一起,比如我是这样放置的:paho.mqtt.c/include/头文件 paho.mqtt.c/lib/库文件。

首先参考在线手册上的发布用法:

#include

#include

#include

#include "MQTTClient.h"

#define ADDRESS "tcp://localhost: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;

MQTTClient_create(&client, ADDRESS, CLIENTID,

MQTTCLIENT_PERSISTENCE_NONE, NULL);

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 = strlen(PAYLOAD);

pubmsg.qos = QOS;

pubmsg.retained = 0;

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);

MQTTClient_disconnect(client, 10000);

MQTTClient_destroy(&client);

return rc;

}

订阅用法:

#include

#include

#include

#include "MQTTClient.h"

#define ADDRESS "tcp://localhost:1883"

#define CLIENTID "ExampleClientSub"

#define TOPIC "MQTT Examples"

#define PAYLOAD "Hello World!"

#define QOS 1

#define TIMEOUT 10000L

volatile MQTTClient_deliveryToken deliveredtoken;

void delivered(void *context, MQTTClient_deliveryToken dt)

{

printf("Message with token value %d delivery confirmed\n", dt);

deliveredtoken = dt;

}

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)

{

int i;

char* payloadptr;

printf("Message arrived\n");

printf(" topic: %s\n", topicName);

printf(" message: ");

payloadptr = message->payload;

for(i=0; ipayloadlen; i++)

{

putchar(*payloadptr++);

}

putchar('\n');

MQTTClient_freeMessage(&message);

MQTTClient_free(topicName);

return 1;

}

void connlost(void *context, char *cau

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值