2016-12-21 5 views
0

AWS IOTを使用してTHINGとAPPを一緒に接続しました。両方ともAWS IOT SDK for Node.jsを使用しています。 THINGは設定可能な温度(set_temp)と温度センサー(actual_temp)を持っています。AWS IOT Thing Shadowのアップデートを聞くには

THINGは$aws/things/THING_ID/shadow/updates/delta/ MQTTトピックをリッスンします。 APPは、次のメッセージを使用$aws/things/THING_ID/shadow/updates/トピックにパブリッシュ:

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

このMQTTメッセージはシング影に伝播し、次いでシング自体に至ります。しかし、事は$aws/things/THING_ID/shadow/updates/トピックで次のように報告したとき:

{ 
    "state": { 
     "reported": { 
      "set_temp": 38.7, 
      "actual_temp": 32.4 
     } 
    } 
} 

...シング影はそれを受けて、それはAPPにメッセージを伝播しません。これは実際にはAPPに戻って伝播する必要がないので、set_tempの場合はこれで問題ありません。しかし、actual_tempが変更されると、それはAPPに戻って伝播するはずですが、それは決してありません。

AWS IOT documentationによれば、これは機能するはずです。彼らは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); 

答えて

0

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サポートhere is the specific threadのおかげで答えられました。

関連する問題