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

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

Building an AWS IoT program

After we have configured our AWS IoT and added the IoT device, we can develop a program to access AWS IoT. In this scenario, our computer is used as an IoT thing. We also used Node.js to access AWS IoT, so we need to install AWS IoT SDK for JavaScript. For testing, we will build a Node.js application to access AWS IoT for such purposes as connecting, sending, and receiving.

Now, create a file called comp-demo.js. Then, write the following Node.js scripts:

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-'
});
device
.on('connect', function() {
console.log('connected');
device.subscribe('topic_1');
device.publish('topic_1', JSON.stringify({ test_data: 1}));
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});

Please change the path and certificate files from your AWS IoT on parameters such as keyPath, certPath, caPath, host, and region. Save this file.

主站蜘蛛池模板: 邳州市| 都昌县| 石狮市| 镇坪县| 当涂县| 安龙县| 繁峙县| 襄樊市| 丹棱县| 兴海县| 明光市| 平果县| 阿尔山市| 桓台县| 德州市| 丰都县| 高唐县| 韶关市| 肇庆市| 河南省| 寻乌县| 锦州市| 武隆县| 莲花县| 德保县| 武川县| 武鸣县| 宜都市| 铜川市| 福安市| 长白| 普定县| 垦利县| 万州区| 南通市| 德庆县| 弥勒县| 收藏| 长武县| 武清区| 湘潭县|