【问题标题】:Updating thing-shadow on aws-iot using Node.js getting some error..also attached code for reffrence使用 Node.js 在 aws-iot 上更新 thing-shadow 得到一些错误..还附加了参考代码
【发布时间】:2016-12-26 06:39:56
【问题描述】:
var awsIot = require('aws-iot-device-sdk');

    var myThingName = 'pi_2';
var fs, configurationFile;

    configurationFile = './configuration.json';
    fs = require('fs');
    var args = JSON.parse(fs.readFileSync(configurationFile));
var thingShadows = awsIot.thingShadow({

   keyPath: args.privateKey,
   certPath: args.clientCert,
   caPath: args.caCert,
   clientId: myThingName,
   region: args.host

});

thingShadows
  .on('connect', function() {

    console.log('connect');
device.subscribe('topic_1');


    device.publish('topic_2', JSON.stringify({ test_data: 1}));
    });

执行此操作时出错:-

错误:getaddrinfo ENOTFOUND 在 errnoException (dns.js:37:11) 在 Object.onanswer [as oncomplete] (dns.js:124:16)

【问题讨论】:

  • 我得到了答案!!!我刚刚将区域 us-east-1(N.Virginia) 更改为 eu-west-1(Ireland)。由于服务器忙或此时没有响应而发生此错误..并且还在我的程序中添加新证书

标签: node.js aws-iot


【解决方案1】:

这是我在 aws-iot 上更新事物阴影的更新代码:-

var awsIot = require('aws-iot-device-sdk');
var myThingName = 'pi_2';
var thingShadows =awsIot.thingShadow({
keyPath: './private.pem.key',
certPath: './certificate.pem.crt',
caPath: './root-CA.crt',
clientId: myThingName,
region: 'ap-southeast-1'
});
mythingstate = {
"state": {
"reported": {
  "ip": "sagar"//update your state here.
}`}`}`

thingShadows.on('connect', function() {
console.log("Connected...");
console.log("Registering...");
thingShadows.register( myThingName);

//马上更新导致超时错误,所以我们等待2秒左右

setTimeout( function() {
console.log("Updating my IP address...");
clientTokenIP = thingShadows.update(myThingName, mythingstate);
console.log("Update:" + clientTokenIP);
}, 2500 );
// Code below just logs messages for info/debugging
thingShadows.on('status',function(thingName, stat, clientToken,
stateObject) {
console.log('received '+stat+' on '+thingName+':
'+JSON.stringify(stateObject));
});
thingShadows.on('update',function(thingName, stateObject) {
console.log('received update '+' on '+thingName+': '+`JSON.stringify(stateObject));
});
thingShadows.on('delta',function(thingName, stateObject) {
console.log('received delta '+' on '+thingName+': '+`JSON.stringify(stateObject));`});`

thingShadows.on('timeout',`function(thingName, clientToken) {`
console.log('received timeout for '+ clientToken)`});`

thingShadows.on('close', function() {`
console.log('close');
});

thingShadows`.on('reconnect', function() {`
console.log('reconnect');
});

thingShadows`.on('offline', function() {
console.log('offline');
});

thingShadows`.on('error', function(error) {
console.log('error', error);
});`});`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2018-01-15
    • 2018-07-28
    相关资源
    最近更新 更多