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

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.

主站蜘蛛池模板: 甘谷县| 拉萨市| 扎赉特旗| 通海县| 拉萨市| 吴川市| 通榆县| 和龙市| 涪陵区| 县级市| 龙游县| 镇江市| 聂拉木县| 石屏县| 霍邱县| 马公市| 清涧县| 县级市| 大宁县| 临沂市| 延长县| 稷山县| 咸阳市| 壤塘县| 宜川县| 伽师县| 开鲁县| 齐齐哈尔市| 马山县| 石棉县| 沂南县| 启东市| 鞍山市| 凉城县| 云龙县| 新邵县| 遂溪县| 咸阳市| 兰考县| 宁武县| 陆良县|