In many IoT service scenarios, we need to dynamically adjust the running parameter settings of devices on the cloud. Many IoT-enabled devices fail to stay online round-the-clock, frequently go online and offline, and enter the scheduled sleep mode and stay offline due to unstable network connectivity and limited battery capacity. In this context, we need to figure out how to send control instructions from the cloud to offline devices and enable the devices to run service logic based on new instructions when they are online.
IoT Platform provides the device shadow feature to update the parameter settings of offline devices.
To use the device shadow feature, it is mandatory to implement the following configurations.
/**
* aliyun-iot-mqtt@0.0.4
*/
const mqtt = require('aliyun-iot-mqtt');
//设备身份三元组+区域
const deviceConfig = {
"productKey": "产品",
"deviceName": "设备",
"deviceSecret": "设备deviceSecret",
"regionId": "cn-shanghai"
};
//1.建立连接
const client = mqtt.getAliyunIotMqttClient(deviceConfig);
//2.订阅设备影子topic
const getShadow = `/shadow/get/${deviceConfig.productKey}/${deviceConfig.deviceName}`;
client.subscribe(getShadow)
client.on('message', function(topic, message) {
//收到消息后,显示设备影子中的远程配置参数
if (topic == getShadow) {
message = JSON.parse(message);
console.log(new Date().Format("yyyy-MM-dd HH:mm:ss.S"))
console.log("\tappConfig.content :", JSON.stringify(message.payload.state.desired.appConfig))
console.log("\tappConfig.timestamp :", JSON.stringify(message.payload.metadata.desired.appConfig.timestamp))
}
})
//3.主动获取设备影子中的远程配置参数
const updateShadow = `/shadow/update/${deviceConfig.productKey}/${deviceConfig.deviceName}`;
client.publish(updateShadow, JSON.stringify({method: "get"}), { qos: 1 })
The service systems store the latest parameter settings desired for device shadow by calling the UpdateDeviceShadow operation of device shadow. The following code runs to implement the same.
/**
* package.json 添加依赖:"@alicloud/pop-core": "1.5.2"
*/
const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;
const options = {
accessKey: "你的accessKey",
accessKeySecret: "你的accessKeySecret",
};
//1.初始化client
const client = new RPCClient({
accessKeyId: options.accessKey,
secretAccessKey: options.accessKeySecret,
endpoint: 'https://iot.cn-shanghai.aliyuncs.com',
apiVersion: '2018-01-20'
});
//2.desired中appConfig变更
const shadowMessage = {
method: "update",
state: {
desired: {
appConfig:{
maxTemperature: 39.5,
}
}
},
version: Date.now()
}
const params = {
ProductKey: "你的ProductKey",
DeviceName: "你的DeviceName",
ShadowMessage: JSON.stringify(shadowMessage)
};
co(function*() {
try {
//3.发起API调用,更新影子中配置参数
const response = yield client.request('UpdateDeviceShadow', params);
console.log(JSON.stringify(response));
} catch (err) {
console.log(err);
}
});
Log on to the console to view information about device shadow after a successful API call by service systems. Refer to the following snapshot for details.
Online devices obtain parameter settings from the cloud through the subscribed topic of device shadow.
Cloud-based parameter settings are cached in device shadow when devices are offline. After the devices go online, they pull the latest parameter settings from the cloud. The parameter update time is earlier than the current time. Devices determine whether to apply the latest parameter settings based on the update time.
How to Identify Frequently Dropped and Constant Offline Devices among 50,000 Devices
OpenAnolis - June 27, 2022
Balaban - July 23, 2021
Aliware - August 18, 2021
PM - C2C_Yuan - September 23, 2022
Alibaba Cloud Indonesia - August 15, 2023
Alibaba Cloud Native Community - October 13, 2020
Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.
Learn MoreA cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms
Learn MoreMigrate your Internet Data Center’s (IDC) Internet gateway to the cloud securely through Alibaba Cloud’s high-quality Internet bandwidth and premium Mainland China route.
Learn MoreMore Posts by GXIC