第九篇 华为云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资源释放
}
相关推荐
搬砖的kk5 分钟前
openJiuwen 快速入门:使用华为云大模型搭建 AI 智能体
数据库·人工智能·华为云
恩创软件开发37 分钟前
回顾2025年
经验分享
AlexMercer101238 分钟前
【操作系统】操作系统期末考试 简答题 焚决
c语言·经验分享·笔记·操作系统
水天需0101 小时前
HISTFILE 介绍
linux
学嵌入式的六子1 小时前
如何使用VScode开发STM32【喂饭级教程】-全过程讲解
c语言·ide·vscode·stm32·单片机·嵌入式硬件
墨辰JC1 小时前
C语言可变参数讲解:stdarg.h应用
c语言·开发语言·蓝桥杯·内存·蓝桥杯嵌入式
CreasyChan1 小时前
VirtualBox 安装 CentOS 7.2
linux·运维·centos
C语言小火车1 小时前
C++右值引用与转移语义详解
c语言·开发语言
AAA.建材批发刘哥1 小时前
04--C++ 类和对象下篇
linux·c++·经验分享·青少年编程
杰克崔1 小时前
glibc社区提问
linux·运维·服务器·车载系统