关于如何在 Raspberry Pi 上接收 AWS IOT 主题的简单示例

Simple example on how to receive an AWS IOT topic on a Raspberry Pi

我正在寻找一个简单 JavaScript 示例来说明如何从主题接收消息。我正在尝试使用 CodeBuild、Lambda、SNS,最后是 IoT 创建一个 Build Cop。我已经成功地向该主题发布了消息,但我终究无法弄清楚如何接收消息。

SDK 中的示例没有详细记录(至少对我而言),我无法弄清楚要使用哪个导入或为什么,以及如何订阅一个简单的主题。

向事物发送消息的代码如下。我从 Lambda 发送它。代码是用 TypeScript 编写的,但我正在将转译后的 JavaScript 复制并粘贴到控制台中,因为它似乎本身不支持 TypeScript。

const params = {
  topic: 'topic/buildcop',
  payload: color,
  qos: 1
};


this.iotdata.publish(params, function(err, data){
  if(err){
    console.log(`error: ${err}`);
  }
  else{
    console.log("success?");
    //context.succeed(event);
  }
});

我不确定这是否是您的意思,但这里有一个关于如何使用 javascript sdk 订阅主题的示例:

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.
// 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 device = awsIot.device({
   keyPath: <YourPrivateKeyPath>,
  certPath: <YourCertificatePath>,
    caPath: <YourRootCACertificatePath>,
  clientId: <YourUniqueClientIdentifier>,
      host: <YourCustomEndpoint>
});

//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device
  .on('connect', function() {
    console.log('connect');
    device.subscribe('topic_1');
    device.publish('topic_2', JSON.stringify({ test_data: 1}));
  });

device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

您可以在此处查看更多示例:https://github.com/aws/aws-iot-device-sdk-js#jobs