官术网_书友最值得收藏!

  • Learning AWS IoT
  • Agus Kurniawan
  • 135字
  • 2021-06-30 19:26:06

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());
});
主站蜘蛛池模板: 西丰县| 额尔古纳市| 塔城市| 秀山| 宁津县| 肃南| 芦溪县| 沙田区| 黔西| 赤城县| 尤溪县| 孟村| 长乐市| 池州市| 湖州市| 平舆县| 鄂州市| 都江堰市| 麻栗坡县| 蕉岭县| 宜阳县| 漳州市| 湖州市| 汕头市| 项城市| 东乌珠穆沁旗| 建昌县| 绿春县| 泗水县| 保亭| 玉林市| 沾化县| 琼结县| 德清县| 彭阳县| 益阳市| 三河市| 黔江区| 大新县| 和田县| 农安县|