-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpi.js
78 lines (65 loc) · 2.04 KB
/
pi.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var awsIot = require('aws-iot-device-sdk');
var myThingName = 'pi_2';
var thingShadows = awsIot.thingShadow({
keyPath: './certs/<your_cert>-private.pem.key',
certPath: './certs/<your_cert>-certificate.pem.crt',
caPath: './certs/rootCA.pem',
clientId: myThingName,
region: 'us-east-1'
});
mythingstate = {
"state": {
"reported": {
"ip": "unknown"
}
}
}
var networkInterfaces = require( 'os' ).networkInterfaces( );
mythingstate["state"]["reported"]["ip"] = networkInterfaces['eth0'][0]['address'];
thingShadows.on('connect', function() {
console.log("Connected...");
console.log("Registering...");
thingShadows.register( myThingName );
// An update right away causes a timeout error, so we wait about 2 seconds
setTimeout( function() {
console.log("Updating my IP address...");
clientTokenIP = thingShadows.update(myThingName, mythingstate);
console.log("Update:" + clientTokenIP);
}, 2500 );
// Code below just logs messages for info/debugging
thingShadows.on('status',
function(thingName, stat, clientToken, stateObject) {
console.log('received '+stat+' on '+thingName+': '+
JSON.stringify(stateObject));
});
thingShadows.on('update',
function(thingName, stateObject) {
console.log('received update '+' on '+thingName+': '+
JSON.stringify(stateObject));
});
thingShadows.on('delta',
function(thingName, stateObject) {
console.log('received delta '+' on '+thingName+': '+
JSON.stringify(stateObject));
});
thingShadows.on('timeout',
function(thingName, clientToken) {
console.log('received timeout for '+ clientToken)
});
thingShadows
.on('close', function() {
console.log('close');
});
thingShadows
.on('reconnect', function() {
console.log('reconnect');
});
thingShadows
.on('offline', function() {
console.log('offline');
});
thingShadows
.on('error', function(error) {
console.log('error', error);
});
});