第九篇 华为云Iot SDK的简单应用

第九篇 华为云Iot SDK的简单应用

一、华为云Iot SDK API的简单使用

1.初始化SDK

2.绑定连接配置信息

3.连接服务器

4.上报属性

5.接收命令

二、实现智能家居灯光状态上报

🔖以下是上报数据到华为云Iot的代码片段,配合串口控制灯光,改变灯的状态(ON/OFF),并将状态上报至华为云。

华为云Iot SDK小demo

c 复制代码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include "hw_type.h"
#include "iota_init.h"
#include "iota_cfg.h"
#include "log_util.h"
#include "json_util.h"
#include "string_util.h"
#include "iota_login.h"
#include "iota_datatrans.h"
#include "mqtt_ctl.h"
#include "light.h"
#include "device.h"

#define     MQTT_REQUEST_ADDR           "8185a863ed.st1.iotda-device.cn-south-1.myhuaweicloud.com"
#define     MQTT_REQUEST_DEVID          "设备ID"
#define     MQTT_REQUEST_DEVPASSWD      "密码"
#define     MQTT_REQUEST_PORT            8883



static int mqtt_set_connection(){
    /** 初始化sdk **/
    if(IOTA_Init(".")) {
        fprintf(stderr, "IOTA_Init(): error.\n");
        IOTA_Destroy();
        return -1;
    }

    /** 绑定sdk配置 **/
    IOTA_ConfigSetStr(EN_IOTA_CFG_MQTT_ADDR,MQTT_REQUEST_ADDR);
    IOTA_ConfigSetUint(EN_IOTA_CFG_MQTT_PORT,MQTT_REQUEST_PORT);
    IOTA_ConfigSetStr(EN_IOTA_CFG_DEVICEID,MQTT_REQUEST_DEVID);
    IOTA_ConfigSetStr(EN_IOTA_CFG_DEVICESECRET,MQTT_REQUEST_DEVPASSWD);
    IOTA_ConfigSetUint(EN_IOTA_CFG_AUTH_MODE,EN_IOTA_CFG_AUTH_MODE_SECRET);
    IOTA_ConfigSetUint(EN_IOTA_CFG_CHECK_STAMP_METHOD, EN_IOTA_CFG_CHECK_STAMP_OFF);

    /** 连接服务器 **/
    int ret = IOTA_Connect();
    if(ret != 0){
        fprintf(stderr,"IOTA_Connect(): error code: %d.\n",ret);
        return -1;
    }
    return 0;
}

/** 上报属性线程 **/
static void *sync_dev_properties(void *arg){
    struct Device *cur_dev = NULL;
    char *light_name[4] = {"厨房灯","卧室灯","客厅灯","卫生间灯"};
    int i;

    /* 组织上报灯光状态的初始json数据 */
    cJSON *lights = cJSON_CreateObject();
    cJSON *update_key = NULL;
    char *update_value = NULL;
    for(i=0;i<4;i++){
        cJSON_AddItemToObject(lights,light_name[i], cJSON_CreateString(statostr(read_dev_status(light_name[i]))));
    }

    /* 每秒向服务器上报数据 */
    ST_IOTA_SERVICE_DATA_INFO lights_service = {
            .event_time = NULL,
            .service_id = "家居灯光",
            .properties = cJSON_Print(lights)
    };
    while(1){
        if(IOTA_PropertiesReport(&lights_service,1,0,NULL) != 0 ){
            fprintf(stderr,"[mqtt_ctl.c] IOTA_PropertiesReport():occurred error.\n");
        }
        printf("[mqtt_ctl.c debug]%s\n",lights_service.properties);
        sleep(2);
        for(i=0;i<4;i++) {
            update_key = cJSON_GetObjectItem(lights, light_name[i]);
            update_value = statostr(read_dev_status(light_name[i]));
            cJSON_SetValuestring(update_key, update_value);
        }
        lights_service.properties = cJSON_Print(lights);
        printf("[mqtt_ctl.c debug]%s\n",lights_service.properties);
    }
}

/** 接收控制命令线程 **/
static void *recv_dev_cmd(void *arg){
	//TODO
}

int mqttCtl_module_create(){
    mqtt_set_connection();
    /** 创建线程上报属性/状态 **/
    pthread_t report_tid;
    if(pthread_create(&report_tid,NULL,sync_dev_properties,NULL)){
        fprintf(stderr,"[mqtt_ctl.c] pthread_create():create thread sync_dev_properties occurred error.");
        return -1;
    }
    /** 创建线程读取下发的命令 **/
    pthread_t recv_cmd_tid;
    if(pthread_create(&recv_cmd_tid,NULL,recv_dev_cmd,NULL)){
        fprintf(stderr,"[mqtt_ctl.c] pthread_create():create thread recv_dev_cmd occurred error.");
        return -1;
    }
    return 0;
}
void mqttCtl_module_destroy(void){
    //TODO 回收线程、cJSON对象、SDK资源释放
}
相关推荐
猫猫的小茶馆10 分钟前
【STM32】通用定时器基本原理
c语言·stm32·单片机·嵌入式硬件·mcu·51单片机
Luna-player1 小时前
黑布淡入淡出效果
经验分享·游戏
独行soc1 小时前
#渗透测试#批量漏洞挖掘#HSC Mailinspector 任意文件读取漏洞(CVE-2024-34470)
linux·科技·安全·网络安全·面试·渗透测试
BD_Marathon1 小时前
Ubuntu下Tomcat的配置
linux·ubuntu·tomcat
pumpkin845141 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust
饥饿的半导体2 小时前
Linux快速入门
linux·运维
BD_Marathon2 小时前
Ubuntu:Tomcat里面的catalina.sh
linux·ubuntu·tomcat
BD_Marathon2 小时前
设置LInux环境变量的方法和区别_Ubuntu/Centos
linux·ubuntu·centos
Me4神秘2 小时前
Linux国产与国外进度对垒
linux·服务器·安全
2401_858286112 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序