1

我有一个 THING 和一个 APP 使用 AWS IOT 连接在一起,两者都使用AWS IOT SDK for Node.js。THING 有一个set_temp可以设置的温度 ( ) 和一个温度传感器 ( actual_temp)。

THING 监听$aws/things/THING_ID/shadow/updates/delta/MQTT 主题。$aws/things/THING_ID/shadow/updates/APP使用以下消息发布主题:

{
    "state": {
        "desired": {
            "set_temp": 38.7
        }
    }
}

此 MQTT 消息传播到事物影子,然后传播到事物本身。但是,当 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 文档,这应该可行。他们甚至说在 THING 的消息中发送包含“ desired: null”。

你怎么能在不投票的情况下“倾听”Thing Shadow?要么我做错了,要么 AWS 在他们的 IOT 平台上有一个大漏洞。

更新(包括实际代码):

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

1 回答 1

2

似乎是因为我clientId对 APP 和 THING 使用了相同的内容。他们不应该有相同的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,这个问题得到了回答,这里是特定的线程

于 2017-01-19T18:41:25.917 回答