How to work with the program?

Now we will review our program, comp-demo.js. The following is a list of steps for the program:

  1. Firstly, we apply the required library from AWS IoT SDK for JavaScript. Then, we declare our device based on our IoT thing from AWS IoT:
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: 'cert/macos-computer.private.key',
certPath: 'cert/macos-computer.cert.pem',
caPath: 'cert/root-CA.crt',
host: 'xxxxxxx.iot.ap-southeast-1.amazonaws.com',
clientId: 'user-testing',
region: 'ap-southeast-'
});
  1. We try to connect to AWS IoT. After we are connected, we subscribe a specific topic, for instance, topic_1. Then, we send a message by calling the publish() function:
device
.on('connect', function() {
console.log('connected');
device.subscribe('topic_1');
device.publish('topic_1', JSON.stringify({ test_data: 1}));
});
  1. To receive an incoming message from AWS IoT, we listen to the message event as follows:
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.200.71