×
Community Blog How to Manage Control Instructions for Offline Devices through Device Shadow

How to Manage Control Instructions for Offline Devices through Device Shadow

This article describes how to send and manage control instructions for offline devices through device shadow.

Updating the Running Parameters of Offline IoT-enabled Devices Through Device Shadow

1) Technical Solution for Updating the Running Parameters of Devices

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.

1

2) Device-side Development

To use the device shadow feature, it is mandatory to implement the following configurations.

  • Subscribe to the update topic (live update) of device shadow.
  • Pull device shadow upon startup to ensure that the device obtains the latest shadow data after going online.
/**
 * 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 })

3) Configuration Update on the Cloud

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);
    }
});

4) Running Results

4.1 Update Parameter Settings Through an API on the Cloud

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.

2

4.2 Obtain Live Updates on Online Devices

Online devices obtain parameter settings from the cloud through the subscribed topic of device shadow.

3

4.3 Obtain Updates After Devices Go Online

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.

4

0 0 0
Share on

GXIC

24 posts | 5 followers

You may also like

Comments

GXIC

24 posts | 5 followers

Related Products

  • IoT Platform

    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 More
  • IoT Solution

    A cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms

    Learn More
  • Global Internet Access Solution

    Migrate 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 More