IotPublishSample增加项目
STEP1
iottencent_demo\app_demo_iot.c
在这里WeChatProfile
是在iot_profile.h
定义的struct类型,所以我们访问需要上报的action时,只需修改 .reportAction 对象。在这里笔者加入的是 batterylife 数据,并将数据设为0
hi_void IotPublishSample(void)
{
/* reported attribute */
WeChatProfile weChatProfile = {
.subscribeType = "type",
.status.subState = "state",
.status.subReport = "reported",
.status.reportVersion = "version",
.status.Token = "clientToken",
//在这里加入****************************
.reportAction.subDeviceActionBatterylife = "batterylife",
.reportAction.batterylifeActionData = 0, /* 1 : safe */
};
STEP2
接下来打开
src\applications\sample\wifi-iot\app\iottencent_demo\iot_profile.c
在里边加入
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionBatterylife,
payload->reportAction.batterylifeActionData);
这一步是增加需要上报的消息,并传给JSON处理模块编译。需要注意的是,所需上报的消息存放在payload变量当中,也就是我们需要在前端接收的消息应该是包含在payload下的,如果有兴趣的话可以进一步研究。下方是加入位置演示
#define WECHAT_SUBSCRIBE_TYPE "update"
#define WECHAT_SUBSCRIBE_VERSION "1.0.0"
#define WECHAT_SUBSCRIBE_TOKEN "DeviceSubscribe"
static char *MakeProfileReport(WeChatProfile *payload)
{
char *ret = NULL;
cJSON *root;
cJSON *state;
cJSON *reported;
/* create json root node */
root = cJSON_CreateObject();
if (root == NULL) {
return;
}
/* state create */
state = cJSON_CreateObject();
if (state == NULL) {
return;
}
/* reported create */
reported = cJSON_CreateObject();
if (reported == NULL) {
return;
}
/* add root object */
cJSON_AddItemToObject(root, payload->subscribeType, cJSON_CreateString(WECHAT_SUBSCRIBE_TYPE));
cJSON_AddItemToObject(root, payload->status.subState, state);
cJSON_AddItemToObject(state, payload->status.subReport, reported);
cJSON_AddItemToObject(root, payload->status.reportVersion, cJSON_CreateString(WECHAT_SUBSCRIBE_VERSION));
cJSON_AddItemToObject(root, payload->status.Token, cJSON_CreateString(WECHAT_SUBSCRIBE_TOKEN));
/* add reported item */
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionLight,
payload->reportAction.lightActionStatus);
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionMotor,
payload->reportAction.motorActionStatus);
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionTemperature,
payload->reportAction.temperatureData);
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionHumidity,
payload->reportAction.humidityActionData);
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionLightIntensity,
payload->reportAction.lightIntensityActionData);
//****************************
cJSON_AddNumberToObject(reported, payload->reportAction.subDeviceActionBatterylife,
payload->reportAction.batterylifeActionData);
//******************************
ret = cJSON_PrintUnformatted(root);
cJSON_Delete(state);
cJSON_Delete(reported);
cJSON_Delete(root);
return ret;
}
TASK3
同时别忘了在头文件src\applications\sample\wifi-iot\app\iottencent_demo\iot_profile.h
里的WeChatProfileReporte
中增加类型
typedef struct {
int lightActionStatus;
int motorActionStatus;
int temperatureData;
int humidityActionData;
int lightIntensityActionData;
int warningstateActionData;
//****************************
int batterylifeActionData;
const char *subDeviceActionBatterylife;
//****************************
const char *subDeviceActionWarning;
const char *subDeviceActionLight;
const char *subDeviceActionMotor;
const char *subDeviceActionTemperature;
const char *subDeviceActionHumidity;
const char *subDeviceActionLightIntensity;
}WeChatProfileReporte;
typedef struct {
const char *subscribeType;
WeChatProfileStatus status;
WeChatProfileReporte reportAction;
}WeChatProfile;