LiteOS云端对接教程05-LiteOS基于MQTTS对接华为OC平台实战

1. LiteOS OC MQTT 抽象组件

概述

为了适应各种各样的使用mqtt接入华为OC的模式,特采用该层次接口,对上提供应用所需的接口,对下允许接入方式的灵活适配。

OC MQTT AL的api接口声明在 中,使用相关的接口需要包含该头文件。

配置并连接

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

typedef struct
{
    en_oc_mqtt_mode  boot_mode;     //对接模式:直连模式和bs模式
    uint8_t          lifetime;      //保持连接时长
    char            *server_addr;   //mqtt服务器地址,ip或者域名
    char            *server_port;   //mqtt服务器端口
    en_mqtt_al_security_t   sec_type;      //当前仅支持crt模式
    char            *id;            //设备对接ID,默认使用NOTEID(设备标识码)对接
    char            *pwd;           //设备秘钥
    //int           device_mode;    //未使用
    fn_oc_mqtt_msg_deal     msg_deal;//接收到数据的回调函数
    void            *msg_deal_arg;   //回调函数参数
}oc_mqtt_config_t;

其中boot_mode是对接模式,对应华为平台的两种模式:

typedef enum
{
    en_oc_mqtt_mode_bs_static_nodeid_hmacsha256_notimecheck_json =0,
    en_oc_mqtt_mode_nobs_static_nodeid_hmacsha256_notimecheck_json,
    en_oc_mqtt_mode_last,
}en_oc_mqtt_mode;

本实验中使用的是直连模式,选择第二种。

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

/**
 * @brief the application use this function to configure the mqtt agent
 *
 * @param[in] param, refer to oc_mqtt_config_t
 *
 * @return code: define by en_oc_mqtt_err_code while 0 means success
 */
int oc_mqtt_config(oc_mqtt_config_t *param);

函数参数很清楚,将存放对接信息的结构体指针传入即可,API的返回值是由en_oc_mqtt_err_code定义的枚举类型,方便定位问题:

typedef enum
{
    en_oc_mqtt_err_ok          = 0,      ///< this means the status ok
    en_oc_mqtt_err_parafmt,              ///< this means the parameter err format
    en_oc_mqtt_err_network,              ///< this means the network wrong status
    en_oc_mqtt_err_conversion,           ///< this means the mqtt version err
    en_oc_mqtt_err_conclientid,          ///< this means the client id is err
    en_oc_mqtt_err_conserver,            ///< this means the server refused the service for some reason(likely the id and pwd)
    en_oc_mqtt_err_conuserpwd,           ///< bad user name or passwd
    en_oc_mqtt_err_conclient,            ///< the client id /user/pwd is right, but does not allowed
    en_oc_mqtt_err_subscribe,            ///< this means subscribe the topic failed
    en_oc_mqtt_err_publish,              ///< this means subscribe the topic failed
    en_oc_mqtt_err_configured,           ///< this means we has configured, please deconfigured it and then do configure again
    en_oc_mqtt_err_noconfigured,         ///< this means we have not configure it yet,so could not connect
    en_oc_mqtt_err_noconected,           ///< this means the connection has not been built, so you could not send data
    en_oc_mqtt_err_gethubaddrtimeout,    ///< this means get the hub address timeout
    en_oc_mqtt_err_sysmem,               ///< this means the system memory is not enough
    en_oc_mqtt_err_system,               ///< this means that the system porting may have some problem,maybe not install yet
    en_oc_mqtt_err_last,
}en_oc_mqtt_err_code_t;

数据上报

连接成功之后,向华为云平台上报数据需要关注两部分:

  • 发布消息主题:这个OC_MQTT组件会自动帮我们搞定;
  • 发布消息指令:发布时由用户指定;
  • 发布消息内容:在使用JSON数据格式通信时,需要在本地将数据封装为JSON格式,OC_MQTT组件中已经编写了一个助手程序,帮助我们封装和数据。

数据封装

上报华为云平台的数据格式如下:</

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于HarmonyOS的毕设可以选择开发一个智能家居系统。智能家居系统是利用物联网技术将家居设备连接到一起,通过智能化、自动化的方式实现对家居环境的监测、控制和管理。HarmonyOS作为华为开发的一种全场景智能终端操作系统,具有分布式能力、低零售硬件门槛和高并发处理能力,非常适合用于构建智能家居系统。 在这个毕设中,可以利用HarmonyOS开发一个智能家居APP,用户可以通过手机或平板电脑等终端设备来控制家居设备的开关、调节温度、光线亮度等。可以实现以下几个功能: 1. 远程控制:用户可以通过APP远程控制家居设备,无论身在何处,都能随时随地对家居环境进行调整。 2. 定时任务:用户可以设置定时任务,如定时开关灯、定时开启空调等,提高家居设备的智能化程度,节省能源。 3. 情景模式:用户可以根据自己的需求创建情景模式,例如"回家模式",一键打开电灯、空调,打造一个舒适的家居环境。 4. 数据统计与分析:系统可以将家居设备的使用数据进行统计与分析,例如用电量统计,帮助用户了解用电情况,提供节能建议。 在开发过程中,可以利用HarmonyOS提供的分布式能力,将家居设备连接到网关设备上,通过网关设备将数据传输到云端进行处理和存储。通过设备驱动及应用程序的开发,实现与各类家居设备的连接与控制,如灯光、温度传感器、门窗传感器等。 通过该毕设可以深入了解HarmonyOS的特性和应用,掌握物联网技术的开发和应用,提高对智能家居系统的理解和实践能力,以及对未来智能化生活的探索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值