-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws-iot-ble-receiver.js
64 lines (47 loc) · 1.37 KB
/
aws-iot-ble-receiver.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// AWS IoT Device app that continuously scans for and reports detected iBeacons - Receiver
var os = require('os');
var awsIot = require('aws-iot-device-sdk');
// use the hostname to identify this instance of the receiver
const receiver = os.hostname().split('.').shift() + '-receiver';
// use this topic for heartbeats
const topicHeartbeat = 'heartbeat';
// use this topic for detections
const topicDetection = 'detection';
// connect to AWS IoT
const aws = awsIot.device({
keyPath: './certs/private.pem.key',
certPath: './certs/certificate.pem.crt',
caPath: './certs/root-CA.crt',
region: 'eu-west-1',
clientId: receiver
});
// subscribe to the topics
aws.subscribe(topicHeartbeat);
aws.subscribe(topicDetection);
//
// AWS IoT event handlers
//
aws
.on('connect', function() {
console.log('AWS IoT Device Gateway: Connected');
});
aws
.on('close', function() {
console.log('AWS IoT Device Gateway: Closed');
});
aws
.on('reconnect', function() {
console.log('AWS IoT Device Gateway: Reconnected');
});
aws
.on('offline', function() {
console.log('AWS IoT Device Gateway: Offline');
});
aws
.on('error', function(error) {
console.log('AWS IoT Device Gateway: Error -', error);
});
aws
.on('message', function(topic, payload) {
console.log(payload.toString());
});