-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
98 lines (78 loc) · 2.51 KB
/
client.js
File metadata and controls
98 lines (78 loc) · 2.51 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use strict";
/* eslint no-use-before-define: 0 */
/* eslint no-process-exit: 0 */
var sphero = require('sphero')
var robots = {
'Sonic': 'F8:CC:A1:A7:51:86',
'Shadow': 'FF:AF:08:F6:7D:19',
'Silver': 'EB:76:90:85:BE:AD'
}
var robotName = process.argv[2]
var robotId = robots[robotName]
var orb = sphero(robotId)
var awsIot = require('aws-iot-device-sdk')
var device = awsIot.device({
keyPath: '/home/pi/sphero-iot/aws_certificates/' + robotName + '/private.pem.key',
certPath: '/home/pi/sphero-iot/aws_certificates/' + robotName + '/certificate.pem.crt',
caPath: '/home/pi/sphero-iot/aws_certificates/ca.pem',
clientId: 'raspberry_pi-' + robotName,
host: 'a2yujzh40clf9c.iot.us-east-2.amazonaws.com'
})
device.on('connect', function() {
console.log('Connected to AWS IoT');
device.subscribe('things/' + robotName + '/commands');
});
device.on('message', function(topic, payload) {
var message = JSON.parse(payload.toString())
if (topic == 'things/' + robotName + '/commands') {
switch (message.action) {
case 'roll':
orb.roll(message.speed, message.direction);
break;
case 'stop':
orb.roll(0,0);
break;
case 'color':
orb.color(message.color)
break;
}
}
{ action: 'roll', speed: 10, direction: 100 }
{ action: 'stop' }
{ action: 'color', color: 'blue' }
//console.log('message from ', topic)
//console.log('payload: ', payload.toString())
});
var thingShadows = awsIot.thingShadow({
keyPath: '/home/pi/sphero-iot/aws_certificates/' + robotName + '/private.pem.key',
certPath: '/home/pi/sphero-iot/aws_certificates/' + robotName + '/certificate.pem.crt',
caPath: '/home/pi/sphero-iot/aws_certificates/ca.pem',
clientId: 'sphero-' + robotName,
host: 'a2yujzh40clf9c.iot.us-east-2.amazonaws.com'
})
var keypress = require("keypress");
thingShadows.on('connect', function() {
console.log('shadow connected');
thingShadows.register(robotName, {}, function() {
orb.connect(listen);
})
})
function listen() {
orb.streamOdometer();
orb.on('odometer', function(data) {
thingShadows.update(robotName, {
'state': {
'reported': {
'odometerX': data.xOdometer.value[0],
'odometerY': data.yOdometer.value[0]
}
}
});
});
keypress(process.stdin);
process.stdin.on("keypress", function() { console.log('key') });
orb.color('ff0000', -0.5)
//console.log("listening for keys...");
process.stdin.setRawMode(true);
process.stdin.resume();
}