Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

node.js - How to create iot device to aws using aws-iot-device-sdk-v2 package?

Using aws-iot-device-sdk-v2 package in a node.js app I am trying to connect new Iot device.

When using static credentials aws-iot-device-sdk-v2 package is working fine and giving proper response.

const CONFIG = {
  endpoint: ***,
  key: ***,
  cert: ***,
  client_id: ***,
  topic: ***,
  verbosity: ***,
  ca_file: ***,  
}

   var clientID = 'webapp-' + this.uniqueId() + new Date().getTime();

const client_bootstrap = new io.ClientBootstrap();
const config_builder = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder_from_path(CONFIG.cert, CONFIG.key);
config_builder.with_certificate_authority_from_path(undefined, CONFIG.ca_file);

config_builder.with_clean_session(false);
config_builder.with_client_id(clientID);
config_builder.with_endpoint(CONFIG.endpoint);

// Keep node running forever
setInterval(() => {}, 1 << 30);

const config = config_builder.build();
const client = new mqtt.MqttClient(client_bootstrap);
const connection = client.new_connection(config);

await connection.connect();
await this.subscribe(connection);

  async subscribe (connection) {
await connection.subscribe(CONFIG.topic, mqtt.QoS.AtLeastOnce, (topic, payload) => {

  try {
    console.log('Got message: ', topic);
    const json = decoder.decode(payload);
    console.log(`Got payload:  ${json}`)
    const message = JSON.parse(json);
  } catch(_) {}
});
  }
question from:https://stackoverflow.com/questions/65884555/how-to-create-iot-device-to-aws-using-aws-iot-device-sdk-v2-package

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...