【问题标题】:Device shadow state not changes设备影子状态不变
【发布时间】:2020-04-01 11:09:11
【问题描述】:

尝试使用 manual 创建 aws-iot 设备。我在创建Pi3-DHT11-Node device 时完成了所有必需的设置。简化代码如下:

var awsIot = require('aws-iot-device-sdk'); 
const NODE_ID = 'Pi3-DHT11-Noded'; 
const INIT_DELAY = 15; 
const TAG = '[' + NODE_ID + '] >>>>>>>>> '; 

var thingShadow = awsIot.thingShadow({ 
  keyPath: './certs_p/fea2f8efae7-private.pem.key', 
  certPath: './certs_p/fea2f8efae7-certificate.pem.crt', 
  caPath: './certs_p/AmazonRootCA1.pem', 
  clientId: NODE_ID, 
  host: 'a3cnel9blokzm0-ats.iot.eu-west-3.amazonaws.com',
  port: 8883, 
  region: 'eu-west-3', 
  debug: true, // optional to see logs on console 
}); 

thingShadow.on('connect', function() { 
  console.log(TAG, 'Connected.'); 
  thingShadow.register(NODE_ID, {}, function() { 
    console.log(TAG, 'Registered.'); 
    console.log(TAG, 'Reading data in ' + INIT_DELAY + ' seconds.'); 
    setTimeout(sendData, INIT_DELAY * 1000); // wait for `INIT_DELAY` seconds before reading the first record 
  }); 
}); 

function sendData() { 
  var DHT11State = { 
    "state": { 
      "desired": 
        { 
        "temp": 20, 
        "humd": 33 
        }
    } 
  }; 

  var clientTokenUpdate = thingShadow.update(NODE_ID, DHT11State); 
  if (clientTokenUpdate === null) { 
    console.log(TAG, 'Shadow update failed, operation still in progress'); 
  } else { 
    console.log(TAG, 'Shadow update success.'); 
  } 

  // keep sending the data every 30 seconds 
  console.log(TAG, 'Reading data again in 30 seconds.'); 
  setTimeout(sendData, 30000); // 30,000 ms => 30 seconds 
} 

脚本工作正常,但这不会改变阴影状态。为什么?如何解决?

【问题讨论】:

    标签: node.js amazon-web-services aws-iot


    【解决方案1】:

    很可能您没有将影子同步到云端的设备设置。

    按照本教程这一部分中的步骤操作,并确保按照第 2 步单击“同步到云”,然后重新部署您的 Greengrass 组。

    https://docs.aws.amazon.com/greengrass/latest/developerguide/comms-enabled.html

    【讨论】:

    • 我使用的是 IoT 核心服务,而不是与 Greengrass 相关的东西。我找不到任何“同步到云端”选项。
    • 啊,我的错!当我看到这个问题时,我正在GG工作。好的...您是否尝试过订阅 AWS 控制台中的影子主题以查看是否收到了您的消息?订阅$aws/things/Pi3-DHT11-Noded/shadow/#,看看你是否看到任何消息。我怀疑它没有到达那里。您是否从您的应用程序中看到任何调试表明与 AWS 的通信正常?
    • 我尝试在 AWS IoT 控制台的测试部分订阅$aws/things/Pi3-DHT11-Noded/shadow/#。我在那里看到来自我的设备的消息。
    • 查看rejected 主题:$aws/things/Pi3-DHT11-Noded/shadow/update/rejected .. 如果您的影子有问题,aws iot 将拒绝它,并在此主题上发布原因。
    猜你喜欢
    • 2020-12-10
    • 2013-10-12
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多