-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-device.js
43 lines (35 loc) · 1.06 KB
/
connect-device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const path = require('path');
const awsIot = require('aws-iot-device-sdk');
const inquirer = require('inquirer');
const certDir = path.resolve(__dirname, '../cert');
inquirer.prompt([{
type: 'input',
name: 'host',
message: 'Enter the AWS IoT endpoint to connect to (<id>.iot.<region>.amazonaws.com):',
}]).then(({ host }) => {
const device = awsIot.device({
keyPath: `${certDir}/deviceCert.key`,
certPath: `${certDir}/deviceCertAndCACert.crt`,
caPath: `${certDir}/aws-iot-root.cert`,
clientId: '12345678',
host,
});
device.on('connect', () => {
console.log('connected');
});
device.on('close', () => {
console.log('close');
});
device.on('reconnect', () => {
console.log('reconnect');
});
device.on('offline', () => {
console.log('offline');
});
device.on('error', function(error) {
console.log('error', error);
});
device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
});