[技术干货] 使用OC_MQTT_AL连接华为OC平台的测试代码分享

>本代码仅作分享,关于AT框架、SAL组件、Cjson组件、MQTT组件、mbedtls组件、OC_MQTT组件的讲解及配置详解,请持续关注小熊派官方教程。 本代码基于OC_MQTT_AL开启成功的前提,使用OC_MQTT_AL提供的API进行连接测试: ```c #include <osal.h> #include <oc_mqtt_al.h> #define DEFAULT_LIFETIME 60 #define DEFAULT_SERVER_IPV4 "49.4.93.24" #define DEFAULT_SERVER_PORT "8883" #define CN_MQTT_EP_NOTEID "321321321321" //设备标识码,不是Device ID #define CN_MQTT_EP_PASSWD "4ac51ec23edeb3eb34e4" //设备秘钥 static int task_reportmsg_entry(void *args) { int ret = -1; /* oc_mqtt 连接信息配置结构体 */ oc_mqtt_config_t config; /* 设置连接信息 */ config.boot_mode = en_oc_mqtt_mode_nobs_static_nodeid_hmacsha256_notimecheck_json; config.msg_deal = NULL; config.msg_deal_arg = NULL; config.lifetime = DEFAULT_LIFETIME; config.server_addr = DEFAULT_SERVER_IPV4; config.server_port = DEFAULT_SERVER_PORT; config.id = CN_MQTT_EP_NOTEID; config.pwd= CN_MQTT_EP_PASSWD; config.sec_type = en_mqtt_al_security_cas; /* 配置并对接云平台 */ ret = oc_mqtt_config(&config); if(ret != en_oc_mqtt_err_ok) { printf("config and connect error, ret = %d.\r\n", ret); return -1; } else { printf("config and connect success.\r\n"); } while(1) { printf("config ok.\r\n"); osal_task_sleep(2000); } } int standard_app_demo_main() { osal_task_create("task_reportmsg",task_reportmsg_entry,NULL,0x800,NULL,8); return 0; } ``` 测试结果: ![image.png](https://bbs-img-cbc-cn.obs.cn-north-1.myhuaweicloud.com/data/attachment/forum/202001/09/132121o753aqu491oapprf.png) ![image.png](https://bbs-img-cbc-cn.obs.cn-north-1.myhuaweicloud.com/data/attachment/forum/202001/09/1324555hp4rzr5sfpaubxp.png)