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

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.

主站蜘蛛池模板: 海原县| 巢湖市| 融水| 吉林市| 通河县| 红桥区| 时尚| 太保市| 天水市| 淳化县| 镇远县| 绥宁县| 天镇县| 普格县| 万盛区| 象山县| 两当县| 郧西县| 墨脱县| 佳木斯市| 哈巴河县| 宜黄县| 孝昌县| 庄浪县| 衢州市| 连平县| 河曲县| 遵化市| 阿鲁科尔沁旗| 遂平县| 德江县| 山丹县| 留坝县| 哈尔滨市| 揭西县| 广水市| 红安县| 建瓯市| 电白县| 芒康县| 始兴县|