当前位置:   article > 正文

MQTT客户端应用编程及接口分析_mqtt接口文档

mqtt接口文档

MQTT客户端应用编程及接口分析

MQTT协议简介

MQTT是一个基于客户端-服务器的消息发布/订阅传输协议。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。

客户端服务端安装

1.安装

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppasudo 
sudo apt-get install mosquitto mosquitto-clients
  • 1
  • 2
  1. 测试
mosquitto_sub -h test.mosquitto.org -t "#" -v
  • 1

如果发现大量的消息滚动刷屏,说明该客户端链接成功

客户端开发包

SDK包下载地址:
eclipse/paho.mqtt.c

编译及生产lib和头文件
cmake -DMAKE_INSTALL_PREFIX=./output
make && make install
在这里插入图片描述

API接口介绍

接口类型头文件备注
Paho MQTT C Client Library MQTTClient.h非现程安全
Paho Asynchronous MQTT C Client LibraryMQTTAsync.h线程安全
数据结构
typedef void* MQTTClient;
typedef void* MQTTAsync;
  • 1
  • 2
MQTTClient 库接口
  1. MQTTClient_create() : MQTTClient.h
int MQTTClient_create(MQTTClient *handle,
                      const char *serverURI,
                      const char *clientId,
                      int         persistence_type,
                      void *      persistence_context 
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

此函数创建一个 MQTT 客户端,以便连接到指定的服务器并使用指定的持久性存储(参考 MQTTClient_persistence 函数)。另外,与创建函数对应的是销毁函数 MQTTClient_destroy()。

  1. MQTTClient_connect() : MQTTClient.h
int MQTTClient_connect(MQTTClient handle,
                       MQTTClient_connectOptions *options
)
  • 1
  • 2
  • 3

此函数尝试使用指定的选项将以前创建的客户端(调用 MQTTClient_create() 函数)连接到 MQTT 服务器。如果要启用异步消息和状态通知,调用该函数之前必须先调用 MQTTClient_setCallbacks() 函数。

  1. MQTTClient_destroy() : MQTTClient.h
void MQTTClient_destroy(MQTTClient *handle)
  • 1

此函数用于释放分配给 MQTT 客户端的内存(调用 MQTTClient_create() 函数)。当不再需要客户端时,应该调用它。

4.MQTTClient_disconnect() : MQTTClient.h

int MQTTClient_disconnect(MQTTClient handle, int timeout)
  • 1

此函数尝试断开客户端与 MQTT 服务器的连接。为了让客户端有时间完成对调用此函数时正在传递的消息的处理,需要指定一个超时期限。当超时期限过期时,即使仍有未完成的消息确认,客户端也会断开连接。下一次客户端连接到同一服务器时,任何未完成的 QoS 1 或 2 消息将根据前一个连接和新连接的清理设置重试(参考 MQTTClient_connectOptions.cleansession 和 MQTTClient_connect())。

  1. MQTTClient_free() : MQTTClient.h
void MQTTClient_free(void *ptr)
  • 1

此函数释放 MQTT C Client 库分配的内存,特别是主题名称。当 client 库和应用程序使用不同版本的 C 编译器编译时,在 Windows 上需要这样做。因此,在释放任何 MQTT C Client 分配的内存时,始终使用此函数是一个很好的策略。

  1. MQTTClient_freeMessage() : MQTTClient.h
void MQTTClient_freeMessage(MQTTClient_message ** msg)
  • 1

此函数释放分配给 MQTT 消息的内存,包括分配给消息有效负载的附加内存。当完全处理消息时,客户端应用程序调用此函数。重要提示:此函数不释放分配给消息主题字符串的内存。客户端应用程序负责使用 MQTTClient_free() 库函数释放此内存。

  1. MQTTClient_isConnected() : MQTTClient.h
int MQTTClient_isConnected(MQTTClient handle)
  • 1

此函数允许客户端应用程序测试某个 client 与 MQTT 服务器连接情况,已连接返回 true,否则返回 false。

  1. MQTTClient_publish() : MQTTClient.h
int MQTTClient_publish(MQTTClient                handle,
                       const char *              topicName,
                       int                       payloadlen,
                       const void *              payload,
                       int                       qos,
                       int                       retained,
                       MQTTClient_deliveryToken *dt
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

此函数试图将消息发布到给定主题(另请参见 MQTTClient_publishMessage 函数)。当此函数成功返回时,将发出 MQTTClient_deliveryToken。如果客户端应用程序需要测试 QoS1 和 QoS2 消息的成功传递,可以异步或同步地完成(参考 异步与同步客户端应用程序、 MQTTClient_waitForCompletion 和 MQTTClient_deliveryComplete 函数)。

  1. MQTTClient_publishMessage() : MQTTClient.h
int MQTTClient_publishMessage(MQTTClient                handle,
                              const char *              topicName,
                              MQTTClient_message *      msg,
                              MQTTClient_deliveryToken *dt
)
  • 1
  • 2
  • 3
  • 4
  • 5

此函数试图将消息发布到给定主题(另请参见 MQTTClient_publish 函数)。当此函数成功返回时,将发出一个 MQTTClient_deliveryToken 令牌。如果客户端应用程序需要测试 QoS1 和 QoS2 消息的成功传递,可以异步或同步地完成(参考 异步与同步客户端应用程序、 MQTTClient_waitForCompletion 和 MQTTClient_deliveryComplete 函数)。

  1. MQTTClient_receive() : MQTTClient.h
int MQTTClient_receive(MQTTClient            handle,
                       char **               topicName,
                       int *                 topicLen,
                       MQTTClient_message ** message,
                       unsigned long         timeout
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

此函数执行传入消息的同步接收。只有当客户端应用程序没有设置回调方法以支持消息的异步接收时,才应该使用它(请参见 异步与同步客户端应用程序 以及 MQTTClient_setCallbacks 函数)。使用此函数可以编写单线程客户端订阅者应用程序。当调用时,此函数将阻塞,直到下一条消息到达或指定的超时过期为止(另请参见 MQTTClient_yield 函数)。

重要提示:应用程序在处理完成后必须释放分配给主题和消息的内存(请参见 MQTTClient_freeMessage 函数)。

  1. MQTTClient_setCallbacks() : MQTTClient.h
int MQTTClient_setCallbacks(MQTTClient                    handle,
                            void *                        context,
                            MQTTClient_connectionLost *   cl,
                            MQTTClient_messageArrived *   ma,
                            MQTTClient_deliveryComplete * dc
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

此函数设置特定客户端的回调函数。如果客户端应用程序不使用特定的回调,请将相关参数设置为 NULL。调用 MQTTClient_setCallbacks() 将客户端置于多线程模式。任何必要的消息确认和状态通信都在后台处理,不需要客户端应用程序的任何干预。更多信息请参见 异步与同步客户端应用程序。

注意:在调用此函数时,MQTT 客户端必须断开连接。

  1. MQTTClient_setDisconnected() : MQTTClient.h
int MQTTClient_setDisconnected(MQTTClient handle,
                               void *     context,
                               MQTTClient_disconnected *co 
)
  • 1
  • 2
  • 3
  • 4

设置客户端的 MQTTClient_disconnected() 回调函数。如果从服务器接收到断开连接的数据包,将调用这个函数。该函数只对 MQTT V5 及以上版本有效。

  1. MQTTClient_setPublished() : MQTTClient.h
int MQTTClient_setPublished(MQTTClient             handle,
                            void *                 context,
                            MQTTClient_published * co
)
  • 1
  • 2
  • 3
  • 4
MQTTAsync 库接口
  • MQTTAsync_connect() : MQTTAsync.h
  • MQTTAsync_create() : MQTTAsync.h
  • MQTTAsync_createWithOptions() : MQTTAsync.h
  • MQTTAsync_destroy() : MQTTAsync.h
  • MQTTAsync_disconnect() : MQTTAsync.h
  • MQTTAsync_free() : MQTTAsync.h
  • MQTTAsync_freeMessage() : MQTTAsync.h
  • MQTTAsync_getPendingTokens() : MQTTAsync.h
  • MQTTAsync_getVersionInfo() : MQTTAsync.h
  • MQTTAsync_global_init() : MQTTAsync.h
  • MQTTAsync_isComplete() : MQTTAsync.h
  • MQTTAsync_isConnected() : MQTTAsync.h
  • MQTTAsync_malloc() : MQTTAsync.h
  • MQTTAsync_reconnect() : MQTTAsync.h
  • MQTTAsync_send() : MQTTAsync.h
  • MQTTAsync_sendMessage() : MQTTAsync.h
  • MQTTAsync_setAfterPersistenceRead() : MQTTAsync.h
  • MQTTAsync_setBeforePersistenceWrite() : MQTTAsync.h
  • MQTTAsync_setCallbacks() : MQTTAsync.h
  • MQTTAsync_setConnected() : MQTTAsync.h
  • MQTTAsync_setConnectionLostCallback() : MQTTAsync.h
  • MQTTAsync_setDeliveryCompleteCallback() : MQTTAsync.h
  • MQTTAsync_setDisconnected() : MQTTAsync.h
  • MQTTAsync_setMessageArrivedCallback() : MQTTAsync.h
  • MQTTAsync_setTraceCallback() : MQTTAsync.h
  • MQTTAsync_setTraceLevel() : MQTTAsync.h
  • MQTTAsync_setUpdateConnectOptions() : MQTTAsync.h
  • MQTTAsync_strerror() : MQTTAsync.h
  • MQTTAsync_subscribe() : MQTTAsync.h
  • MQTTAsync_subscribeMany() : MQTTAsync.h
  • MQTTAsync_unsubscribe() : MQTTAsync.h
  • MQTTAsync_unsubscribeMany() : MQTTAsync.h
  • MQTTAsync_waitForCompletion() : MQTTAsync.h
  • MQTTProperties_add() : MQTTProperties.h
  • MQTTProperties_copy() : MQTTProperties.h
  • MQTTProperties_free() : MQTTProperties.h
  • MQTTProperties_getNumericValue() : MQTTProperties.h
  • MQTTProperties_getNumericValueAt() : MQTTProperties.h
  • MQTTProperties_getProperty() : MQTTProperties.h
  • MQTTProperties_getPropertyAt() : MQTTProperties.h
  • MQTTProperties_hasProperty() : MQTTProperties.h
  • MQTTProperties_len() : MQTTProperties.h
  • MQTTProperties_propertyCount() : MQTTProperties.h
  • MQTTProperties_read() : MQTTProperties.h
  • MQTTProperties_write() : MQTTProperties.h
  • MQTTProperty_getType() : MQTTProperties.h
  • MQTTPropertyName() : MQTTProperties.h
  • MQTTReasonCode_toString() : MQTTReasonCodes.h

###代码使用案例

#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     "tcp://mqtt.eclipseprojects.io: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");
	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;

	if ((rc = MQTTAsync_create(&client, ADDRESS, 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, NULL, 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;
}
  • 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
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/176402
推荐阅读
相关标签
  

闽ICP备14008679号