第九篇 华为云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资源释放
}
相关推荐
房开民15 小时前
使用海康机器人相机SDK实现基本参数配置(C语言示例)
c语言·数码相机·机器人
2501_9200470316 小时前
git在Linux中的使用
linux·git·elasticsearch
程序设计实验室16 小时前
在鸡哥14x上安装Linux:Fedora 42 上手体验
linux
UNbuff_016 小时前
Linux bzip2 命令使用说明
linux·运维·服务器
Tina表姐17 小时前
(C题|NIPT 的时点选择与胎儿的异常判定)2025年高教杯全国大学生数学建模国赛解题思路|完整代码论文集合
c语言·开发语言·数学建模
芯片智造17 小时前
一文看懂什么是GaN HEMT以及其工艺流程(氮化镓高电子迁移率晶体管)
经验分享·芯片·半导体·晶体管·氮化镓高电子迁移率晶体管
IOT-Power17 小时前
Ubuntu下把 SD 卡格式化为 FAT32
linux·运维·ubuntu
嫩萝卜头儿17 小时前
虚拟地址空间:从概念到内存管理的底层逻辑
linux·服务器·网络
proware18 小时前
昇腾310i Pro固件说明
linux·运维·服务器
高山有多高18 小时前
详解文件操作
c语言·开发语言·数据库·c++·算法