Hi3861学习笔记
Publish函数(Hi3861)
要发布一个信息,命名为’‘warning_state’',
首先需要在函数 IotPublishSample 里定义,
.reportAction.subDeviceActionWarning = “warning_state”,
.reportAction.lightIntensityActionData = 1, /* 1 : safe */
hi_void IotPublishSample(void)
{
/* reported attribute */
WeChatProfile weChatProfile = {
.subscribeType = "type",
.status.subState = "state",
.status.subReport = "reported",
.status.reportVersion = "version",
.status.Token = "clientToken",
.reportAction.subDeviceActionWarning = "warning_state",
.reportAction.lightIntensityActionData = 1, /* 1 : safe */
}
然后在配置文件src\applications\sample\wifi-iot\app\iottencent_demo\iot_profile.h
中
找到type WeChatProfileReporte
, 加入以下两条语句:
typedef struct {
int warningstateActionData;
const char *subDeviceActionWarning;
}WeChatProfileReporte;
然后调用主函数里的IoTProfilePropertyReport
函数进行上报。
小程序端(shadow-query)
通过调用云函数iothub-shadow-query
来获取云端的影子设备信息,其中stateReported: deviceData.payload.state.reported
即为转译过来的对应信息。例如:stateReported.warning_state
即对应的上方publish的信息。
wx.cloud.callFunction({
name: 'iothub-shadow-query',
data: {
ProductId: app.globalData.productId,
DeviceName: app.globalData.deviceName,
SecretId: app.globalData.secretId,
SecretKey: app.globalData.secretKey,
},
success: res => {
wx.showToast({
icon: 'none',
title: 'Subscribe完成,获取云端数据成功',
})
//JSON转译回来 成为deviceData
let deviceData = JSON.parse(res.result.Data)
this.setData({
stateReported: deviceData.payload.state.reported
})
console.log("result:", deviceData)
},