赞
踩
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdbool.h>
- #include "MQTTAsync.h"
-
- MQTTAsync mqttClient;
- static const char *subTopic = "xxx"; //设备订阅的主题
- static const char *pubTopic = "xxx"; //设备发布的主题
-
-
- int sendMessage(char *data, int qos) //发送数据
- {
- int rc;
- MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
-
- opts.context = mqttClient;
- //opts.onSuccess = NULL; /*发送成功回调*/
- //opts.onFailure = NULL; /*发送失败回调*/
- pubmsg.payload = data;
- pubmsg.payloadlen = strlen(data);
- pubmsg.qos = qos;
- printf("mqtt send: %s\n", (char *)pubmsg.payload);
- //尝试发送数据,想要知道消息是否发送成功,需要设置 opts 中的相关回调
- if ((rc = MQTTAsync_sendMessage(mqttClient, pubTopic, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
- {
- printf("MQTTAsync_sendMessage() fail, error code:%d\n", rc);
- }
-
- return 0;
- }
-
- void onConnectCallCBack(void *context, char *cause) //连接成功回调
- {
- printf("Connection successful.\n");
- int rc, qos;
- MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
-
- qos = 0;
- opts.context = mqttClient;
- //opts.onSuccess = NULL; /*订阅成功回调*/
- //opts.onFailure = NULL; /*订阅失败回调*/
- if ((rc = MQTTAsync_subscribe(mqttClient, subTopic, qos, &opts)) != MQTTASYNC_SUCCESS) //尝试订阅主题
- {
- printf("MQTTAsync_subscribe() fail, error code:%d\n", rc);
- }
- }
-
- void onConnectFailure(void *context, MQTTAsync_failureData *response) //连接失败回调
- {
- printf("connection fail, error code: %d, error message:%s\n", response ? response->code : 0, response ? response->message : 0);
- }
-
- void connlost(void *context, char *cause) //连接断开回调
- {
- printf("disconnect, cause: %s\n", cause);
- }
-
- int msgarrvd(void *context, char *pubTopic, int topicLen, MQTTAsync_message *message) //接收数据回调
- {
- char buf[message->payloadlen + 1] = {0};
-
- memcpy(buf, message->payload, message->payloadlen);
- printf("mqtt recv: %s\n", buf);
- MQTTAsync_freeMessage(&message);
- MQTTAsync_free(pubTopic);
-
- return 1;
- }
-
- void mqtt()
- {
- int rc;
- const char *serverUrl = "xxx"; //服务器地址
- const char *clientId = "xxx"; //客户端标识符
- const char *userName = "xxx"; //用户名
- const char *password = "xxx"; //密码
-
- MQTTAsync_create(&mqttClient, serverUrl, clientId, MQTTCLIENT_PERSISTENCE_NONE, NULL); //创建mqtt客户端
- MQTTAsync_setCallbacks(mqttClient, NULL, connlost, msgarrvd, NULL); //设置连接断开和接受数据回调
- MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; //初始化结构体
- conn_opts.cleansession = 1;
- conn_opts.username = userName;
- conn_opts.password = password;
- //conn_opts.onSuccess = onConnect; /*连接成功回调,此处设置的回调只在第一次连接成功时调用,SDK内部自动重连时不会调用;*/
- conn_opts.onFailure = onConnectFailure; //连接失败回调
- conn_opts.context = mqttClient;
- conn_opts.automaticReconnect = true; //开启断开自动重连
- conn_opts.minRetryInterval = 3; //最小重连间隔时间(秒),每次失败重连间隔时间都会加倍
- conn_opts.maxRetryInterval = 60; //最大重连间隔时间(秒)
- MQTTAsync_setConnected(mqttClient, NULL, onConnectCallCBack); //设置连接成功回调,不管是第一次连接成功还是重连成功都会调用此回调
- if ((rc = MQTTAsync_connect(mqttClient, &conn_opts)) != MQTTASYNC_SUCCESS) //尝试连接
- {
- printf("MQTTAsync_connect() fail, error code:%d\n", rc);
- }
- }
关于各回调函数的详细介绍:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。