使用mosquitto函数实现与EMQX的发布和订阅

前面几个博客我们已经将这些基础都已经准备好了,接下来只需要简单的编程就可以实现了
首先我们创建一个结构体

typedef struct broker_sub_s
{
   
    char    host[64];  
    int     port;      
    char    username[32]; 
    char    passwd[64];
    char    clientid[64];
    char    subTopic[64];
    int     subQos;       
    int     keepalive;
}broker_sub_t;

这里面我们储存了关于broker的一些信息和连接是需要的数据,存储了地址、端口、用户名、密码、clientid、主题、服务等级、心跳连接的时间。将它们打包成一个结构体,能够让我们再设置回调函数时就有了很大的方便,

订阅

订阅到的信息将保存到下面的结构体

struct mosquitto_message{
   
    int mid;//消息序号ID
    char *topic; //主题
    void *payload; //主题内容 ,MQTT 中有效载荷
    int payloadlen; //消息的长度,单位是字节
    int qos; //服务质量
    bool retain; //是否保留消息
};

进行订阅的话我们首先要进行mosquitto初始化,然后传讲mosquitto实例,然后调用函数设置用户名和密码,之后我们可以设置对应的回调函数,我这里设置的是connect和message的回调,具体内容可看代码,因为我们传递的信息是json数据格式,所以我还写了个解析cjson的函数,具体可以看看我之前的博客,再connect回调中我们可以用mosquitto_subcrible发布消息(发布端类似),然后在另外一个中打印出信息,最会调用无限循环的函数。具体代码如下:

订阅头文件
/********************************************************************************
 *      Copyright:  (C) 2020 longyongtu<longyongtu13@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  temp_rpt_aly.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(17/07/20)
 *         Author:  longyongtu <longyongtu13@qq.com>
 *      ChangeLog:  1, Release initial version on "17/07/20 16:53:49"
 *                 
 ********************************************************************************/
#ifndef _MOSQUITTO_SUB_H
#define _MOSQUITTO_SUB_H

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <getopt.h>
#include <string.h>
#include <mosquitto.h>
#include <stdlib.h>
            
#include "cJSON.h"
        
    
#define  PR_VERSION    "1.0.0"
#define  HOST          "192.168.240.134" 
#define  PORT          1883
#define  USERNAME      "test_username"    
#define  PASSWD        "test_password" 
#define  CLIENTID      "12345"
#define  SUBTOPIC      "test"

#define  SUBQOS        1
#define  KEEPALIVE     30

typedef struct broker_sub_s
{
   
    char    host[64];
    int     port;
    char    username[32];
    char    passwd[64];
    char    clientid[64];
    char    subTopic[64];
    int     subQos;
    int     keepalive;
}broker_sub_t;

static void  print_usage( char *progname);
void connect_callback(struct mosquitto *mosq, void *obj, int rc);
int sub_init(struct mosquitto ** mosq, broker_sub_t broker, void *buf);
void printJson(cJSON * root);
void message_callback(struct mosquitto *mosq, void* obj, const struct mosquitto_message *message);

#endif

订阅C代码
/*********************************************************************************
 *      Copyright:  (C) 2020 longyongtu<longyongtu13@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  temp_rpt_aly.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(16/07/20)
 *         Author:  longyongtu <longyongtu13@qq.com>
 *      ChangeLog:  1, Release initial version on "16/07/20 14:32:00"
 *                 
 ********************************************************************************/
#include "mosquitto_sub.h"


void printJson(cJSON * root)//以递归的方式打印json的最内层键值对
{
   
    for(int i=0; i<cJSON_GetArraySize(root); i++)   //遍历最外层json键值对
    {
   
        cJSON * item = cJSON_GetArrayItem(root, i);
        if(cJSON_Object == item->type)      //如果对应键的值仍为cJSON_Object就递归调用printJson
            printJson(item);
        else                                //值不为json对象就直接打印出键和值
        {
   
            printf("%s:", item->string);
            printf("%s\r\n", cJSON_Print(item));
        
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值