LiteOS云端对接教程03-LiteOS基于MQTT对接EMQ-X服务器

本文是LiteOS基于MQTT对接EMQ-X服务器的教程,涵盖了MQTT组件配置、连接、订阅和发布消息的步骤。通过配置ESP8266设备和MQTT组件,使用mqtt.fx进行对接验证,实现了物联网设备的数据上报和命令下发功能。
摘要由CSDN通过智能技术生成

1. LiteOS MQTT组件

概述

MQTT AL用来解耦基于MQTT的业务和MQTT的具体实现,具体来说以后的MQTT业务层应该有且只能使用MQTT AL提供的相关功能(API 数据结构 流程等)。MQTT AL定义MQTT的标准,用来屏蔽各个MQTT协议实现的差异(如软件库 或者硬件),让上层业务无需关心MQTT的实现部分。

MQTT AL的api接口声明在 中,使用相关的接口需要包含该头文件,关于函数的详细参数请参考该头文件的声明。

配置并连接

对接服务器的所有信息保存在结构体mqtt_al_conpara_t中,其定义在mqtt_al.h中,如下:

/** @brief defines the paramter for the mqtt connect */
typedef struct
{
    mqtt_al_string_t               serveraddr;   ///< mqtt server:support domain name and dot format
    int                            serverport;   ///< mqtt server port
    mqtt_al_security_para_t       *security;     ///< if NULL,will use en_mqtt_security_none
    en_mqtt_al_verison             version;      ///< mqtt version will be used
    mqtt_al_string_t               clientid;     ///< mqtt connect client identifier
    mqtt_al_string_t               user;         ///< mqtt connect user
    mqtt_al_string_t               passwd;       ///< mqtt connect passwd
    int                            cleansession; ///< 1 clean the session while 0 not
    mqtt_al_willmsg_t             *willmsg;      ///< mqtt connect will message
    unsigned short                 keepalivetime;///< keep alive time
    char                           conret;       ///< mqtt connect code, return by server
    int                            timeout;      ///< how much time will be blocked
}mqtt_al_conpara_t;

其中的一些参数值已经使用枚举给出:

  • security:安全连接参数(使用此需要确保mbedtls组件开启)

枚举值如下:

/** @brief  this enum all the transport encode we support now*/
typedef enum
{
    en_mqtt_al_security_none = 0,    ///< no encode
    en_mqtt_al_security_psk,         ///< use the psk mode in transport layer
    en_mqtt_al_security_cas,         ///< use the ca mode in transport layer,only check the server
    en_mqtt_al_security_cacs,         ///< use the ca mode in transport layer,both check the server and client
    en_mqtt_al_security_end,         ///< the end for the mqtt
}en_mqtt_al_security_t;

  • version:使用的MQTT协议版本

枚举值如下:

/** @brief enum the mqtt version*/
typedef enum
{
    en_mqtt_al_version_3_1_0 = 0,
    en_mqtt_al_version_3_1_1,
}en_mqtt_al_verison;

另外,在复制的时候还需要注意,很多字符串参数都是使用mqttalstring_t类型,其定义如下:

/** brief defines for all the ascii or data used in the mqtt engine */
typedef struct
{
    char *data;      ///< buffer to storage the data
    int   len;       ///< buffer data length
}mqtt_al_string_t;   //used to represent any type string (maybe not ascii)

在配置结构体完成之后,调用配置函数进行配置并连接,API如下:

/**
 *@brief: you could use this function to connect to the mqtt server
 *
 *@param[in] conparam  the parameter we will use in connect, refer to the data mqtt_al_conpara_t
 *@
 *@return: first you should check the return value then the return code in conparam
 *
 *@retval NULL which means you could not get the connect to the server,maybe network reason
 *@retval handle, which means you get the context,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值