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

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());
});
主站蜘蛛池模板: 万州区| 南宫市| 萨迦县| 满洲里市| 蒙阴县| 冀州市| 视频| 尼玛县| 荆州市| 浙江省| 始兴县| 宣化县| 莱芜市| 冀州市| 芮城县| 乐陵市| 社旗县| 乐山市| 高碑店市| 乌兰察布市| 江西省| 宜兰市| 丰顺县| 普陀区| 丹江口市| 周至县| 葵青区| 临沭县| 梁平县| 南宫市| 成武县| 砚山县| 芒康县| 自贡市| 连平县| 岑巩县| 龙门县| 九台市| 遂川县| 松溪县| 浪卡子县|