-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (45 loc) · 1.31 KB
/
index.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
var express = require('express'),
http = require('http');
// Need two of them for parser
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
// USB PORT - change this into your port
// You can check this at youre arduino application
var portName = "/dev/tty.usbmodemfa131";
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 8080);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname, 'public'));
});
var sp = new SerialPort(portName, {
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false,
// for every new line parse the data!
parser: serialport.parsers.readline("\n")
});
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket){
console.log('connected socket');
sp.on('data', function (data) {
console.log(data);
socket.emit('newposition', data);
});
sp.on('close', function (err) {
console.log('port closed');
});
sp.on('error', function (err) {
console.error("error", err);
});
sp.on('open', function () {
console.log('port opened...');
});
});