Node.js 如何收听AWS物联网阴影更新
我有一个东西和一个应用程序连接在一起使用AWS物联网,两者都使用。这个东西有一个可以设置的温度(Node.js 如何收听AWS物联网阴影更新,node.js,amazon-web-services,aws-iot,aws-sdk-nodejs,Node.js,Amazon Web Services,Aws Iot,Aws Sdk Nodejs,我有一个东西和一个应用程序连接在一起使用AWS物联网,两者都使用。这个东西有一个可以设置的温度(set_temp)和一个温度传感器(actual_temp) THING侦听$aws/things/THING\u ID/shadow/updates/delta/MQTT主题。应用程序在$aws/things/things\u ID/shadow/updates/主题上发布以下消息: { "state": { "desired": { "set_tem
set_temp
)和一个温度传感器(actual_temp
)
THING侦听$aws/things/THING\u ID/shadow/updates/delta/
MQTT主题。应用程序在$aws/things/things\u ID/shadow/updates/
主题上发布以下消息:
{
"state": {
"desired": {
"set_temp": 38.7
}
}
}
此MQTT消息传播到对象阴影,然后传播到对象本身。但是,当THING在$aws/things/THING\u ID/shadow/updates/
主题上报告以下内容时:
{
"state": {
"reported": {
"set_temp": 38.7,
"actual_temp": 32.4
}
}
}
。。。Thing Shadow接收到消息,但不会将消息传播到应用程序。这对于set_temp
来说很好,因为它实际上不需要传播回应用程序。但是当实际温度改变时,它应该传播回应用程序,但它永远不会
根据法律,这应该是有效的。他们甚至说要在邮件中包含“所需:null
”
你怎么能“听”影子的东西而不轮询它呢?要么我做错了什么,要么AWS的物联网平台存在漏洞
更新(包括实际代码):
App.js:
var awsIot = require('aws-iot-device-sdk');
var name = 'THING_ID';
var app = awsIot.device({
keyPath: '../../certs/private.pem.key',
certPath: '../../certs/certificate.pem.crt',
caPath: '../../certs/root-ca.pem.crt',
clientId: name,
region: 'ap-northeast-1'
});
app.subscribe('$aws/things/' + name + '/shadow/update/accepted');
app.on('message', function(topic, payload) {
// THIS LINE OF CODE NEVER RUNS
console.log('got message', topic, payload.toString());
});
var awsIot = require('aws-iot-device-sdk');
var name = 'THING_ID';
var device = awsIot.device({
keyPath: '../../certs/private.pem.key',
certPath: '../../certs/certificate.pem.crt',
caPath: '../../certs/root-ca.pem.crt',
clientId: name,
region: 'ap-northeast-1'
});
device.subscribe('$aws/things/' + name + '/shadow/update/delta');
device.on('message', function (topic, payload) {
console.log('got message', topic, payload.toString());
});
// Publish state.reported every 1 second with changing temp
setInterval(function () {
device.publish('$aws/things/' + name + '/shadow/update', JSON.stringify({
'state': {
'reported': {
'actual_pool_temp': 20 + Math.random() * 10
}
}
}));
}, 1000);
Device.js:
var awsIot = require('aws-iot-device-sdk');
var name = 'THING_ID';
var app = awsIot.device({
keyPath: '../../certs/private.pem.key',
certPath: '../../certs/certificate.pem.crt',
caPath: '../../certs/root-ca.pem.crt',
clientId: name,
region: 'ap-northeast-1'
});
app.subscribe('$aws/things/' + name + '/shadow/update/accepted');
app.on('message', function(topic, payload) {
// THIS LINE OF CODE NEVER RUNS
console.log('got message', topic, payload.toString());
});
var awsIot = require('aws-iot-device-sdk');
var name = 'THING_ID';
var device = awsIot.device({
keyPath: '../../certs/private.pem.key',
certPath: '../../certs/certificate.pem.crt',
caPath: '../../certs/root-ca.pem.crt',
clientId: name,
region: 'ap-northeast-1'
});
device.subscribe('$aws/things/' + name + '/shadow/update/delta');
device.on('message', function (topic, payload) {
console.log('got message', topic, payload.toString());
});
// Publish state.reported every 1 second with changing temp
setInterval(function () {
device.publish('$aws/things/' + name + '/shadow/update', JSON.stringify({
'state': {
'reported': {
'actual_pool_temp': 20 + Math.random() * 10
}
}
}));
}, 1000);
似乎是因为我对应用程序和其他东西使用了相同的clientId
。它们不应该有相同的clientId
。下面是我使用的代码,其中包含clientId
var app = awsIot.device({
keyPath: '../../certs/private.pem.key',
certPath: '../../certs/certificate.pem.crt',
caPath: '../../certs/root-ca.pem.crt',
clientId: 'MY_ID', // This needs to be different for all parties
region: 'ap-southeast-2'
});
由于AWS的支持,这一问题得到了解答