-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (69 loc) · 2.05 KB
/
index.js
File metadata and controls
89 lines (69 loc) · 2.05 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
var nconf = require('nconf'),
express = require('express'),
app = express(),
http = require('http').Server(app),
SerialPort = require('serialport'),
io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
/********* Environment configuration concerns *********/
// First consider commandline arguments and environment variables, respectively.
nconf.argv().env();
// Then load configuration from a designated file.
nconf.file({
file: 'config.json'
});
// Provide default values for settings not provided above.
nconf.defaults({
'http': {
'port': 3005
},
'serialPort': {
'port': 'COM4',
'baudRate': 9600
}
});
/********* SerialPort concerns *********/
const Readline = SerialPort.parsers.Readline;
// port subject to change!
var port = new SerialPort(nconf.get('serialPort:port'), {
baudRate: nconf.get('serialPort:baudRate')
}, (err) => {
if (err) {
return console.log('Error: ', err.message);
}
});
const parser = port.pipe(new Readline({
delimiter: '\r\n'
}));
port.on('open', () => {
// Server is connected to Arduino
console.log('Serial Port opened');
io.sockets.on('connection', (socket) => {
// Connecting to client
console.log('Socket connected');
socket.emit('connected');
var lastUniqueValue;
parser.on('data', (data) => {
var value = data;
if (lastUniqueValue !== value) {
socket.emit('data', value);
}
lastUniqueValue = value;
console.log('Data value: ', value);
});
});
});
/********* Socket.IO concerns *********/
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
/***************************************/
// Listen on port
app.set('port', (process.env.PORT || nconf.get('http:port') || 3005));
// Notfy port
http.listen(app.get('port'), () => {
console.log('Running on port', app.get('port'));
});