-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (86 loc) · 2.35 KB
/
server.js
File metadata and controls
109 lines (86 loc) · 2.35 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
99
100
101
102
103
104
105
106
107
108
109
var static = require('node-static'),
five = require('johnny-five');
//
// Node server
//
var file = new static.Server('./dist');
var server = require('http').createServer(function (request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
});
server.listen(process.env.PORT || 8080);
//
// Socket.io
//
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
var board = new five.Board();
socket.emit('server event', { text: 'Socket.io works' });
socket.on('client event', function (data) {
console.log(data.text);
});
//
// Arduino
//
board.on('ready', function () {
var tilt = new five.Button(2),
greenLed = new five.Led(7),
redLed = new five.Led(8),
totalTimePoured = 0,
pourStartTime = 0,
holdCalled = false,
downCalled = false;
board.repl.inject({
button: tilt
})
tilt.on('down', function () {
if (downCalled) {
return;
}
pourStartTime = new Date().getTime();
holdCalled = false;
downCalled = true;
console.log('START POURIN: ' + pourStartTime);
greenLed.on()
redLed.off()
})
tilt.on('hold', function () {
holdCalled = true;
var currentTime = new Date().getTime();
console.log('BEEN POURIN FOR: ' + (currentTime - pourStartTime) + ' MILLISECONDS');
})
tilt.on('up', function () {
redLed.on()
greenLed.off();
var endTime = new Date().getTime();
var thisPourLength = (endTime - pourStartTime);
pourStartTime = endTime;
downCalled = false;
// 500 millisecond hysteresis to get rid of some sensor noise
if (holdCalled && thisPourLength > 500) {
addPourTimeToTotal(thisPourLength);
console.log('BEERS DONE, POURED FOR: ' + thisPourLength + ' MILLISECONDS');
console.log('TOTAL FOR ALL POURS: ' + totalTimePoured + ' MILLISECONDS');
}
else {
console.log('POUR TOO SHORT');
}
holdCalled = false;
})
this.on('exit', function () {
greenLed.off()
redLed.off()
})
function reset() {
totalTimePoured = 0;
}
function addPourTimeToTotal(time) {
totalTimePoured += time;
// update UI
socket.emit('pour done', {
value: totalTimePoured
});
}
})
});