Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·101 lines (87 loc) · 3.09 KB
/
Copy pathserver.js
File metadata and controls
executable file
·101 lines (87 loc) · 3.09 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
90
91
92
93
94
95
96
97
98
99
100
101
var fs = require('fs'),
http = require('http'),
socketio = require('socket.io'),
url = require("url"),
SerialPort = require("serialport").SerialPort,
connection = require('./db');
var reDensity = /(\d+)\s+$/;
var socketServer;
var serialPort;
var portName = '/dev/tty.wch ch341 USB=>RS232 1410'; //change this to your Arduino port
var sendData = "";
var httpServer = http.createServer(function (request, response) {
connection.query('SELECT * FROM tblSensorData', function (err, rows, fields) {
response.writeHead(501, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(rows));
});
}).listen(9090);
// handle contains locations to browse to (vote and poll); pathnames.
function startServer(route,handle,debug)
{
// on request event
function onRequest(request, response) {
// parse the requested url into pathname. pathname will be compared
// in route.js to handle (var content), if it matches the a page will
// come up. Otherwise a 404 will be given.
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received");
var content = route(handle,pathname,response,request,debug);
}
var httpServer = http.createServer(onRequest).listen(1337, function(){
console.log("Listening at: http://localhost:1337");
console.log("Server is up");
});
serialListener(debug);
initSocketIO(httpServer,debug);
}
function initSocketIO(httpServer,debug)
{
socketServer = socketio.listen(httpServer);
if(debug == false){
socketServer.set('log level', 1); // socket IO debug off
}
socketServer.on('connection', function (socket) {
console.log("user connected");
socket.emit('onconnection', {pollOneValue:sendData});
socketServer.on('update', function(data) {
socket.emit('updateData',{pollOneValue:data});
});
socket.on('buttonval', function(data) {
serialPort.write(data + 'E');
});
socket.on('sliderval', function(data) {
serialPort.write(data + 'P');
});
});
}
// Listen to serial port
function serialListener(debug) {
serialPort = new SerialPort(portName, {
baudrate: 9600,
// defaults for Arduino serial communication
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
serialPort.on("open", function () {
console.log('open serial communication');
// Listens to incoming data
serialPort.on('data', function(data) {
data = data.toString();
console.log(data);
var density = data.match(reDensity)[1];
if (density) {
sendData = data.substring(data.indexOf('B') + 1, data .indexOf('E'));
// send the incoming data to browser with websockets.
socketServer.emit('update', sendData);
connection.query('INSERT INTO tblSensorData SET ?', {DATE: new Date(), CO2: density}, function (err) {
if (err) console.error('Error writing to db:', err.toString);
});
} else {
console.error('No match for received Arduino data!');
}
});
});
}
exports.start = startServer;