第九篇 华为云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资源释放
}
相关推荐
apocelipes3 小时前
Linux c 运行时获取动态库所在路径
linux·c语言·linux编程
打码人的日常分享3 小时前
物联网智慧医院建设方案(PPT)
大数据·物联网·架构·流程图·智慧城市·制造
努力学习的小廉4 小时前
深入了解linux系统—— 进程池
linux·运维·服务器
int型码农4 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
秃头菜狗4 小时前
各个主要目录的功能 / Linux 常见指令
linux·运维·服务器
2301_793102494 小时前
Linux——MySql数据库
linux·数据库
jiunian_cn6 小时前
【Linux】centos软件安装
linux·运维·centos
程序员JerrySUN6 小时前
[特殊字符] 深入理解 Linux 内核进程管理:架构、核心函数与调度机制
java·linux·架构
孤寂大仙v6 小时前
【计算机网络】非阻塞IO——select实现多路转接
linux·计算机网络
玩转4G物联网6 小时前
零基础玩转物联网-串口转以太网模块如何快速实现与TCP服务器通信
服务器·网络·物联网·网络协议·tcp/ip·http·fs100p