Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将mqtt消息从nodejs发布到AWS shadow_Javascript_Node.js_Amazon Web Services_Mqtt - Fatal编程技术网

Javascript 如何将mqtt消息从nodejs发布到AWS shadow

Javascript 如何将mqtt消息从nodejs发布到AWS shadow,javascript,node.js,amazon-web-services,mqtt,Javascript,Node.js,Amazon Web Services,Mqtt,我正在使用nodejs发布、订阅AWS shadow。这是我从以下链接获得的代码 代码如下: var awsIot = require('aws-iot-device-sdk'); // // Replace the values of '<YourUniqueClientIdentifier>' and '<YourCustomEndpoint>' // with a unique client identifier and custom host endpoint

我正在使用nodejs发布、订阅AWS shadow。这是我从以下链接获得的代码

代码如下:

var awsIot = require('aws-iot-device-sdk');

//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourCustomEndpoint>'
// with a unique client identifier and custom host endpoint provided in AWS IoT cloud
// NOTE: client identifiers must be unique within your AWS account; if a client attempts
// to connect with a client identifier which is already in use, the existing
// connection will be terminated.
//
var thingShadows = awsIot.thingShadow({
   keyPath: <YourPrivateKeyPath>,
  certPath: <YourCertificatePath>,
    caPath: <YourRootCACertificatePath>,
  clientId: <YourUniqueClientIdentifier>,
      host: <YourCustomEndpoint>
});

//
// Client token value returned from thingShadows.update() operation
//
var clientTokenUpdate;

//
// Simulated device values
//
var rval = 187;
var gval = 114;
var bval = 222;

thingShadows.on('connect', function() {
//
// After connecting to the AWS IoT platform, register interest in the
// Thing Shadow named 'RGBLedLamp'.
//
    thingShadows.register( 'RGBLedLamp', {}, function() {

// Once registration is complete, update the Thing Shadow named
// 'RGBLedLamp' with the latest device state and save the clientToken
// so that we can correlate it with status or timeout events.
//
// Thing shadow state
//
       var rgbLedLampState = {"state":{"desired":{"red":rval,"green":gval,"blue":bval}}};

       clientTokenUpdate = thingShadows.update('RGBLedLamp', rgbLedLampState  );
//
// The update method returns a clientToken; if non-null, this value will
// be sent in a 'status' event when the operation completes, allowing you
// to know whether or not the update was successful.  If the update method
// returns null, it's because another operation is currently in progress and
// you'll need to wait until it completes (or times out) before updating the
// shadow.
//
       if (clientTokenUpdate === null)
       {
          console.log('update shadow failed, operation still in progress');
       }
    });
});
thingShadows.on('status',
    function(thingName, stat, clientToken, stateObject) {
       console.log('received '+stat+' on '+thingName+': '+
                   JSON.stringify(stateObject));
//
// These events report the status of update(), get(), and delete()
// calls.  The clientToken value associated with the event will have
// the same value which was returned in an earlier call to get(),
// update(), or delete().  Use status events to keep track of the
// status of shadow operations.
//
    });

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 on '+thingName+
                   ' with token: '+ clientToken);
//
// In the event that a shadow operation times out, you'll receive
// one of these events.  The clientToken value associated with the
// event will have the same value which was returned in an earlier
// call to get(), update(), or delete().
//
    });

var awsIot=require('aws-iot-device-sdk');
//
//替换“”和“”的值
//AWS物联网云中提供了唯一的客户端标识符和自定义主机端点
//注意:客户标识符在您的AWS帐户中必须是唯一的;如果客户端尝试
//要连接已在使用的客户端标识符,请使用现有
//连接将被终止。
//
var thingShadows=awsIot.thingShadow({
键路径:,
证书路径:,
卡帕斯:,
客户ID:,
主持人:
});
//
//thingShadows.update()操作返回的客户端令牌值
//
var-clientTokenUpdate;
//
//模拟设备值
//
var-rval=187;
var gval=114;
var-bval=222;
thingShadows.on('connect',function(){
//
//连接到AWS物联网平台后,在
//名为“RGBLedLamp”的阴影。
//
thingShadows.register('RGBLedLamp',{},function(){
//注册完成后,更新名为
//带有最新设备状态的“rgbledamp”并保存clientToken
//因此,我们可以将其与状态或超时事件关联起来。
//
//物影状态
//
var rgbLedLampState={“状态”:{“所需”:{“红色”:rval,“绿色”:gval,“蓝色”:bval}};
clientTokenUpdate=thingShadows.update('RGBLedLamp',rgbledlamptate);
//
//update方法返回一个clientToken;如果非null,则该值将
//在操作完成时以“状态”事件的形式发送,允许您
//了解更新是否成功。如果更新方法
//返回null,这是因为当前正在进行另一个操作,并且
//您需要等到它完成(或超时)之后才能更新
//影子。
//
如果(clientTokenUpdate==null)
{
log('更新卷影失败,操作仍在进行');
}
});
});
thingShadows.on('status'),
函数(thingName、stat、clientToken、stateObject){
log('received'+stat+'on'+thingName+':'+
stringify(stateObject));
//
//这些事件报告update()、get()和delete()的状态
//与事件关联的clientToken值将具有
//与先前调用get()时返回的值相同,
//update()或delete()。使用状态事件跟踪
//阴影操作的状态。
//
});
thingShadows.on('delta',
函数(thingName,stateObject){
log('在'+thingName+'上接收到增量:'+
stringify(stateObject));
});
thingShadows.on('timeout'),
函数(thingName、clientToken){
console.log('在'+thingName'上收到超时+
'带令牌:'+clientToken);
//
//如果阴影操作超时,您将收到
//这些事件之一。与
//事件的值将与先前事件中返回的值相同
//调用get()、update()或delete()。
//
});
我已为私钥和其他证书使用了正确的凭据。这没有问题。但是我的AWS阴影不会得到更新。顺便说一句,在我使用AWS MQTT代理测试AWS Shadow时,也正确地配置了AWS Shadow

我不确定如何使用主机:YourCustomEndpoint。我在IOT Core的选项卡测试中使用了端点,您可以从下拉菜单中获得该端点。这是对的还是我还有其他事情要解决

在这个github链接上的前一个示例中,您将mqtt消息从nodejs发布到AWS mqtt broker,这对我很有用。这段代码不起作用的原因可能是什么