-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdevfest.js
81 lines (66 loc) · 2.01 KB
/
gdevfest.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
79
80
81
// var
var app = require('http').createServer(),
io = require('socket.io').listen(app),
express = require('express'),
api = express(),
BTSP = require('bluetooth-serial-port'),
serial = new BTSP.BluetoothSerialPort(),
sockets = [],
buffer = '';
// store all connected sockets
io.sockets.on('connection', function (socket) {
sockets.push(socket);
});
// [GET] /disco/on - turns on disco mode
api.get('/disco/on', function(req, res){
res.send('disco time!');
sockets.forEach(function(socket) {
socket.emit('disco', {state: true});
});
});
// [GET] /disco/off - turns off disco mode
api.get('/disco/off', function(req, res){
res.send('noooo disco time!');
sockets.forEach(function(socket) {
socket.emit('disco', {state: false});
});
});
// check for bluetooth sender
serial.on('found', function(address, name) {
// check the found address with the address of the
// bluetooth enabled Arduino device here.
if(name !== 'HC-05') {
return false;
}
console.log('found bluetooth module',name, 'at address',address);
serial.findSerialPortChannel(address, function(channel) {
console.log('found channel: ',channel);
serial.connect(address, channel, function() {
serial.on('data', function(data) {
data = data.toString('utf-8');
buffer += data;
if(buffer.indexOf('}') >= 0) {
sendChunk(buffer.replace(/\r\n/,''));
buffer = '';
}
});
}, function () {
console.log('cannot connect');
});
});
});
var sendChunk = function(msg) {
console.log(msg);
try {
msg = JSON.parse(msg);
sockets.forEach(function(socket) {
socket.emit('button', msg);
});
} catch(e) {
console.log('bad chunk, sending failed!');
return;
}
};
serial.inquire();
api.listen(9000);
app.listen(8000);