-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (35 loc) · 1.12 KB
/
server.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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
var SerialPort = require("serialport").SerialPort
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// Change serial connection to whatever your Ardunio is connected to
var serialPort = new SerialPort("/dev/tty.usbmodem621");
serialPort.on("data", function (data) {
console.log(data);
});
// reduce logging
io.set('log level', 1);
var clients = Array();
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('client connected');
// emit status socket data to update interface with connection state
socket.emit('status', { connected: 'true' });
// On 'pan' socket, write to serial port and broadcast to update other clients
socket.on('pan', function (data) {
console.log(data);
serialPort.write(data.pan+".");
socket.broadcast.emit('pan', { pan: data.pan });
});
});