mqtt 客户端开发(1)

使用MQTTClient.h库进行mqtt通讯【C语言】_c语言mqtt库_39度C的博客-CSDN博客

目录
MQTT
安装paho.mqtt.c库
安装依赖
安装
编译及运行
编译
运行
源代码
MQTT
MQTT是一种轻量级物联网消息推送协议,使用MQTT开发物联网设备将会十分简单方便。

关于MQTT服务器的安装可以看我的这篇文章:搭建MQTT服务器

安装paho.mqtt.c库

安装依赖

sudo apt-get install libssl-dev

安装

git clone https://github.com/eclipse/paho.mqtt.c.git

cd paho.mqtt.c
make
sudo make install

编译及运行

将下面的源代码放到test.c文件。

编译

gcc -o test test.c -l paho-mqtt3c

gcc编译引用的API接口库:参考文章

paho-mqtt3a :异步API接口的库,API函数都是以MQTTAsync开头,API函数定义在MQTTAsync.h。

paho-mqtt3as :异步API接口的库,API函数都是以MQTTAsync开头,API函数定义在MQTTAsync.h,这个库默认带有TLS加密功能,同时也需要openssl库支持,才能使用这个库。

paho-mqtt3c :同步API接口的库,API函数都是以MQTTClient开头,API函数定义在MQTTClient.h。

paho-mqtt3cs :同步API接口的库,API函数都是以MQTTClient开头,API函数定义在MQTTClient.h,这个库默认带有TLS加密功能,同时也需要openssl库支持,才能使用这个库。

运行
./test

源代码

#include 
#include 
#include 
#include 
#include "MQTTClient.h"

#define MQTT_Uri    "localhost"   				// MQTT服务器的地址和端口号
#define ClientId    "ubuntu16"                  // ClientId需要唯一
#define UserName    "pi"                  		// 用户名
#define PassWord    "12345678"                  // 用户名对应的密码

// 失去连接回调函数
void connect_lost(void *context, char *cause)
{
    printf("Connection lost,The reason: %s \n",cause);
}

// 收到主题信息回调函数
int message_arrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    printf("Receive topic: %s, message data: \n", topicName);
    printf("%.*s\n", message->payloadlen, (char*)message->payload);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

// 主题发布成功回调函数
void delivery_complete(void *context, MQTTClient_deliveryToken dt)
{
    printf("publish topic success,token = %d \n", dt);
}

int main(int argc, char* argv[])
{
    // 1、定义一个MQTT客户端结构体指针
    MQTTClient client;

    // 2、创建一个MQTT客户端
    int rc;
    if ((rc = MQTTClient_create(&client, MQTT_Uri, ClientId, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to create client, return code %d\n", rc);
        exit(EXIT_FAILURE);
        goto exit;
    }

    // 3、创建一个MQTT连接配置结构体,并配置其参数
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    conn_opts.username = UserName;          // 用户名 
    conn_opts.password = PassWord;          // 用户名对应的密码
    conn_opts.keepAliveInterval = 60;       // 心跳时间
    conn_opts.cleansession = 1;             // 清除会话
    
    // 4、设置MQTT连接时的回调函数
    if ((rc = MQTTClient_setCallbacks(client, NULL, connect_lost, message_arrived, delivery_complete)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to set callbacks, return code %d\n", rc);
        rc = EXIT_FAILURE;
        goto destroy_exit;
    }

    // 5、开始连接到MQTT服务器
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
        goto destroy_exit;
    }

    // 6、定义一个主题消息存储结构体
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    char mag_data[] = "I am Ubuntu16.";
    pubmsg.payload = mag_data;
    pubmsg.payloadlen = (int)strlen(mag_data);
    pubmsg.qos = 1;                 // qos等级为1 
    pubmsg.retained = 0;            // 服务器不保留消息
    MQTTClient_deliveryToken token; // 标记MQTT消息的值,用来检查消息是否发送成功

    // 7、发布主题信息
    if ((rc = MQTTClient_publishMessage(client, "pubtest", &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to publish message, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }

    // 8、订阅主题
    if ((rc = MQTTClient_subscribe(client, "subtest", 1)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to subscribe, return code %d\n", rc);
        rc = EXIT_FAILURE;
    }

    // 9、等待输入'Q'或'q'退出
    printf("Press Q or q +  to quit\n\n");
    int ch;
    do
    {
            ch = getchar();
    } while (ch!='Q' && ch != 'q');

    if ((rc = MQTTClient_unsubscribe(client, "ubuntu16_subscribe")) != MQTTCLIENT_SUCCESS)
    {
            printf("Failed to unsubscribe, return code %d\n", rc);
            rc = EXIT_FAILURE;
    }

    // 10、断开连接
    if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to disconnect, return code %d\n", rc);
        rc = EXIT_FAILURE;
    }

destroy_exit:
    MQTTClient_destroy(&client);
exit:
    return rc;
}


 

你可能感兴趣的:(开发语言,c++)