-
Notifications
You must be signed in to change notification settings - Fork 2
/
bluetooth-listener.js
56 lines (41 loc) · 1.17 KB
/
bluetooth-listener.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
var io = require('socket.io-client');
var moment = require('moment');
var socket = io.connect('http://localhost:7076');
socket.on('disconnect', function () {
console.log('disconnect');
});
var serialport = require('serialport');
var serialPort = new serialport.SerialPort(
"/dev/tty.RNBT-356E-RNI-SPP",
{
baudrate: 115200,
parser: serialport.parsers.readline("\n")
}
);
serialPort.open(function () {
var re = /(S|Q|B)([0-9]*)/;
console.log('open');
serialPort.on('data', function (data) {
var ts = moment().valueOf();
var res = data.match(re);
if (!res) {
return;
}
var type = res[1];
var value = res[2];
console.log(ts, type, value);
if (type == 'S') {
value = parseInt(value, 10);
console.log('Sending heartbeat', value);
socket.emit('heartbeat', { ts: ts, value: value }, function(resp, data) {
console.log('server sent resp code ' + resp);
});
} else if (type == 'B') {
value = parseInt(value, 10);
console.log('Sending BPM', value);
socket.emit('bpm', { ts: ts, value: value }, function(resp, data) {
console.log('server sent resp code ' + resp);
});
}
});
});