第九篇 华为云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资源释放
}
相关推荐
宋康1 分钟前
C/C++ 指针避坑20条
c语言·开发语言·c++
davenian3 分钟前
< OS 有关 > Ubuntu 24 SSH 服务器更换端口 in jp/us VPSs
linux·ubuntu·ssh
StickToForever15 分钟前
第4章 信息系统架构(二)
经验分享·笔记·学习·职场和发展
诚信爱国敬业友善24 分钟前
GUI编程(window系统→Linux系统)
linux·python·gui
sekaii25 分钟前
ReDistribution plan细节
linux·服务器·数据库
阿噜噜小栈31 分钟前
Cursor 无限续杯
经验分享·笔记
仟濹41 分钟前
【二分搜索 C/C++】洛谷 P1873 EKO / 砍树
c语言·c++·算法
YH_DevJourney1 小时前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++
威哥爱编程2 小时前
Linux驱动开发13个实用案例
linux
去看日出2 小时前
Linux(centos)系统安装部署MySQL8.0数据库(GLIBC版本)
linux·数据库·centos