【发布时间】:2020-04-01 11:09:11
【问题描述】:
尝试使用 manual 创建 aws-iot 设备。我在创建Pi3-DHT11-Node device 时完成了所有必需的设置。简化代码如下:
var awsIot = require('aws-iot-device-sdk');
const NODE_ID = 'Pi3-DHT11-Noded';
const INIT_DELAY = 15;
const TAG = '[' + NODE_ID + '] >>>>>>>>> ';
var thingShadow = awsIot.thingShadow({
keyPath: './certs_p/fea2f8efae7-private.pem.key',
certPath: './certs_p/fea2f8efae7-certificate.pem.crt',
caPath: './certs_p/AmazonRootCA1.pem',
clientId: NODE_ID,
host: 'a3cnel9blokzm0-ats.iot.eu-west-3.amazonaws.com',
port: 8883,
region: 'eu-west-3',
debug: true, // optional to see logs on console
});
thingShadow.on('connect', function() {
console.log(TAG, 'Connected.');
thingShadow.register(NODE_ID, {}, function() {
console.log(TAG, 'Registered.');
console.log(TAG, 'Reading data in ' + INIT_DELAY + ' seconds.');
setTimeout(sendData, INIT_DELAY * 1000); // wait for `INIT_DELAY` seconds before reading the first record
});
});
function sendData() {
var DHT11State = {
"state": {
"desired":
{
"temp": 20,
"humd": 33
}
}
};
var clientTokenUpdate = thingShadow.update(NODE_ID, DHT11State);
if (clientTokenUpdate === null) {
console.log(TAG, 'Shadow update failed, operation still in progress');
} else {
console.log(TAG, 'Shadow update success.');
}
// keep sending the data every 30 seconds
console.log(TAG, 'Reading data again in 30 seconds.');
setTimeout(sendData, 30000); // 30,000 ms => 30 seconds
}
脚本工作正常,但这不会改变阴影状态。为什么?如何解决?
【问题讨论】:
标签: node.js amazon-web-services aws-iot