动态 版块 发帖 消息 我的
Loading...
小绿叶技术博客
小绿叶技术博客
小绿叶技术Lv149   
mqtt 订阅多主题 rv1126 研究 c 语言 代码     

#---- 已完成验证 ----#


步骤:

1. 下载 mosquttio 客户端源码:https://mosquitto.org/files/source/

wget https://mosquitto.org/files/source/mosquitto-1.4.14.tar.gz


2. 编辑原来的   client/sub_client.c 文件为下面内容

3. cmake 方式编译生成可执行文件: mkdir build && cd build && cmake .. && make  

4. 执行文件 ./client/client/mosquitto_sub




#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include <string.h>
#define HOST "73e.top"
#define PORT  1883
#define KEEP_ALIVE 60
//心跳 60

#define named "eisc"
#define passwdd "000000"

#define zhutipub "v1/devices/me/attributes/request/1"
#define zhutisub "v1/devices/me/attributes/request/1"
#define inputpub "{"key":"name,id,status"}"

bool session = true;
void mqtt_subscribe_callback(struct mosquitto *mosq,
                void *userdata, int mid, int qos_count, const int *granted_qos)
{
    int i;
    printf("subscribed (mid: %d): %d", mid, granted_qos[0]);
    for(i=1; i < qos_count; i++){
        printf(", %d", granted_qos[i]);
    }
    printf("\n");
}

//消息回调函数,收到订阅的消息后调用
void mqtt_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
    if (message->payloadlen){
        printf("%s %s \n", message->topic, (char *)message->payload);
    }else{
        printf("%s (null)\n",message->topic);
    }
}




//mqtt连接回调
void mqtt_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
    int ret;
    if (!result){

//--------------------  订阅多主题 ---------------#
        ret = mosquitto_subscribe(mosq, NULL, zhutisub, 2);
        ret = mosquitto_subscribe(mosq, NULL, "eisc", 2);

        if(ret < 0){
            printf("Subscription failed\n");
        }else{
            printf("Subscription succeeded\n");
        }
    }else{
        printf("connect failed\n");
    }
}
//日志回调函数
void mqtt_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
    printf("log__   %s\n", str);
}
int main(void)
{
    int err = 0;
    printf("mqtt client init...\n");
    struct mosquitto *mosq = NULL;
    //libmosquitto 库初始化
    err = mosquitto_lib_init();
    if (err < 0){
        printf("mosquitto lib int fail...");
        goto fail_mosquitto_lib;
    }

//----------------   使用用户名 和 密码 --------------#
    //创建mosquitto客户端
    mosq = mosquitto_new("eisc",session,NULL);
// 客户端id 连接名字 
// clean_session:设置为 true 可指示代理在断开连接时清除所有消息和订阅,设置为 false 以指示代理保留它们
// 用户指针用户指针

    if (mosq == NULL){
        printf("create client failed...\n");
        err = -1;
        goto fail_mosquitto_new;
    }
    //设置回调函数
    mosquitto_log_callback_set(mosq, mqtt_log_callback);
    mosquitto_connect_callback_set(mosq, mqtt_connect_callback);
    mosquitto_message_callback_set(mosq, mqtt_message_callback);
    mosquitto_subscribe_callback_set(mosq, mqtt_subscribe_callback);
    
    // 设置用户密码:
    mosquitto_username_pw_set(mosq, named, passwdd);


    //客户端连接服务器:  有效 实例  服务器ip  服务器端口  保持连接时间心跳
    err = mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE);

    if (err < 0){
        printf("connect fail");
        goto fail_mosq_conn;
    }
    //主线成将被阻塞至此
    err = mosquitto_loop_forever(mosq, -1, 1);
    if (err < 0){
        printf("mosquitto loop fail");
        goto fail_mosq_loop_stop;
    }
    sleep(10);
//    mosquitto_publish(mosq, NULL, "v1/devices/me/attributes/request/1", strlen(buff)+1, buff, 0, 0);
// 发布消息函数说明:1结构体指针 2指向int指针 3发布字符串topic 4有效负载大小 5指向发送数据的指针 6

    mosquitto_disconnect(mosq);
    mosquitto_loop_stop(mosq, false);
    mosquitto_destroy(mosq);
    mosquitto_lib_cleanup();
    return 0;
fail_mosq_loop_stop:
    mosquitto_disconnect(mosq);
fail_mosq_conn:
    mosquitto_destroy(mosq);
fail_mosquitto_new:
    mosquitto_lib_cleanup();
fail_mosquitto_lib:
    return err;
}



由: // 参考地址:https://www.shuzhiduo.com/A/lk5a8qANJ1/ 改编

 14  已被阅读了125次  楼主 2022-07-04 14:37:40
回复列表

回复:mqtt 订阅多主题 rv1126 研究 c 语言 代码

guest
登录之后更精彩~
Powered by HadSky 7.7.16
© 2015-2022 PuYueTian
您的IP:8.219.134.200,2022-08-10 06:46:05,Processed in 0.01267 second(s).
Powered by HadSky
小绿叶技术博客