-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (84 loc) · 2.4 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
////////////////////////////
/////// HTTP PORTION ///////
////////////////////////////
var http = require('http');
var fs = require('fs');
var httpServer = http.createServer(requestHandler);
var url = require('url');
httpServer.listen(8080);
function requestHandler(req, res) {
var parsedUrl = url.parse(req.url);
// console.log("The Request is: " + parsedUrl.pathname);
fs.readFile(__dirname + parsedUrl.pathname,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading ' + parsedUrl.pathname);
}
res.writeHead(200);
res.end(data);
}
);
}
//global variable for brightness
var brightness;
/////////////////////////////
/////// WEB SOCKETS ///////
/////////////////////////////
var io = require('socket.io').listen(httpServer);
io.sockets.on('connection',
function (socket) {
console.log("We have a new client: " + socket.id);
///MY SOCKET EVENTS HERE
socket.on('led', function(data){
brightness = data;
sendBrightness();
});
socket.on('disconnect', function() {
console.log("Client has disconnected " + socket.id);
});
}
);
////////////////////////////
/////// SERIAL STUFF ///////
////////////////////////////
//npm install serialport
var serialport = require('serialport'); // include the library
var SerialPort = serialport.SerialPort; // make a local instance of it
// comment out after it's working
// serialport.list(function (err, ports) {
// ports.forEach(function(port) {
// console.log(port.comName);
// });
// });
// get port name from the command line:
// SET THIS TO YOUR OWN PORT
var portName = '/dev/cu.usbmodemHIDP1';
//then actually open th eport
var myPort = new SerialPort(portName, {
baudRate: 9600,
// look for return and newline at the end of each data packet:
parser: serialport.parsers.readline("\n")
});
//serial events
myPort.on('open', showPortOpen);
myPort.on('data', sendSerialData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
function sendBrightness(){
myPort.write(brightness.toString());
console.log('brightness: ' + brightness);
}
function showPortOpen() {
console.log('port open. Data rate: ' + myPort.options.baudRate);
}
function sendSerialData(data) {
// console.log(data);
io.sockets.emit('sensor', data);
}
function showPortClose() {
console.log('port closed.');
}
function showError(error) {
console.log('Serial port error: ' + error);
}