范例代码
作为极简代码就可以实现MQTT通信,通过下面几行代码来快速入门MQTT。
注:以下代码所使用库为paho-mqtt.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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[])
{
// MQTT客户端初始化
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
// 创建客户端与Broker服务器的连接
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
// MQTT Qos(通信质量)设定
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;
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "TOPIC_CICD_2_TS"
#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; i<message->payloadlen; i++)
{
putchar(*payloadptr++);
}
putchar('\n');
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
void connlost(void *context, char *cause)
{
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
}
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
int rc;
int ch;
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
MQTTClient_subscribe(client, TOPIC, QOS);
do
{
ch = getchar();
} while(ch!='Q' && ch != 'q');
MQTTClient_unsubscribe(client, TOPIC);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
根据以上代码,对于接收消息,也就是订阅主题,等待接收到消息的过程。
PahoMQTT库介绍
paho-MQTT库有MQTTClient和MQTTAsync两个库。上述示例代码为同步库MQTTClient的使用例子。
MQTTClient
MQTTClient是一个为了初学者使用的,简单地同步单线程使用的MQTT通信库。如上述代码,连接->发信/订阅->等待发信结束/等待收到消息 就是客户端流程。
当然,若MQTTClient_setCallBacks函数被调用的话,等待发信结束MQTTClient_waitForCompletion函数是非必要的。
特别注意:MQTTClient是个非线程安全的。若多线程使用MQTTClient库中函数,未有线程锁相关保护机制。所以请慎重使用。
MQTTAsync
MQTTAsync与MQTTClient主要的区别在于,MQTTAsync支持多线程调用。
所以使用MQTTAsync库时没有等待发信结束函数,MQTTClient_setCallBacks函数必须被设定。
详细使用,请查看以下地址。
库下载地址为:Eclipse Paho | The Eclipse Foundation
库说明文档为:Paho MQTT C Client Library: MQTT Client library for C (eclipse.org)
Paho Asynchronous MQTT C Client Library: Asynchronous MQTT client library for C (eclipse.org)