我使用AWS IOT将THING和APP连接在一起,两者都使用AWS IOT SDK for Node.js。 THING有一个可以设置的温度(set_temp
)和一个温度传感器(actual_temp
)。
THING侦听$aws/things/THING_ID/shadow/updates/delta/
MQTT主题。 APP使用以下消息在$aws/things/THING_ID/shadow/updates/
主题上发布:
{
"state": {
"desired": {
"set_temp": 38.7
}
}
}
此MQTT消息传播到Thing Shadow,然后传播到THING本身。但是,当THING在$aws/things/THING_ID/shadow/updates/
主题上报告以下内容时:
{
"state": {
"reported": {
"set_temp": 38.7,
"actual_temp": 32.4
}
}
}
...... Thing Shadow收到它,但它不会将消息传播到APP。这适用于set_temp
,因为它实际上不需要传播回APP。但是当actual_temp
发生变化时,它应该传播回APP,但它永远不会传播。
根据AWS IOT documentation这应该有效。他们甚至说要发送包含“desired: null
”的信息来自THING。
你怎么能在没有投票的情况下“听”Thing Shadow?要么我做错了,要么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());
});
Device.js:
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);
答案 0 :(得分:1)
看起来因为我对APP和THING使用相同的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 Support,here is the specific thread。