-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathserver.js
147 lines (124 loc) · 3.95 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var PATH_TO_SERIAL_PORT = '';
var path = require('path');
var fs = require('fs');
var express = require('express');
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
// Don't set the serialport on development
if (process.env.NODE_ENV != "development"){
var sp = new SerialPort('/dev/ttyUSB0', { baudrate: 9600 });
}
// All the values we are getting from the ECU
var rpm, mph, coolantTemp = 0;
var currentData= [];
var frameStarted = false;
var lengthByte;
function handleData(data, bytesExpected){
// create an array of the size of requested data length and fill with requested data
for(var i = 0; i < data.length; i++){
// read just 1 byte at a time of the stream
var char = data.toString('hex',i,i+1);
if(char === "ff"){
// Beginning of data array, the frame has started
frameStarted = true;
// Get rid of last frame of data
currentData = [];
// remove last lengthByte number so that we can check what this frame's byte should be
lengthByte = undefined;
}else if(frameStarted){
// frame has started
if(!lengthByte){
// read lengthByte from the ECU
lengthByte = parseInt(char, 16);
}else{
// push byte of data onto our array
currentData.push(parseInt(char, 16));
}
}
}
if(currentData.length === bytesExpected){
// End of data, return the array of data
frameStarted = false;
return currentData.slice();
}
}
function convertRPM(mostSignificantBit, leastSignificantBit){
// combine most significant bit and least significant bit and convert to RPM
return ((mostSignificantBit << 8) + leastSignificantBit) * 12.5;
}
function convertCoolantTemp(data){
// Subtract 50 for Celsius
var celciusCoolantTemp = data - 50;
// Convert celcius to fahrenheit
var fahrenheitCoolantTemp = celciusCoolantTemp * 1.8 + 32;
return fahrenheitCoolantTemp;
}
function convertKPH(data){
// data * 2 gives KPH
return data * 2;
}
function convertMPH(data){
// data * 2 gives KPH
return convertKPH(data) * 0.6213711922;
}
function parseData(data){
if(data !== undefined){
rpm = convertRPM(data[1], data[2]);
coolantTemp = convertCoolantTemp(data[0]);
mph = convertMPH(data[3]);
}
}
var isConnected = false;
var command = [0x5A,0x08,0x5A,0x00,0x5A,0x01,0x5A,0x0b,0xF0];
var bytesRequested = (command.length - 1) / 2;
// Don't run this part for development.
if (process.env.NODE_ENV != "development"){
sp.on("open", function () {
// Write initialization bytes to the ECU
sp.write([0xFF, 0xFF, 0xEF], function(err, results) {});
sp.on('data', function(data) {
// Check to see if the ECU is connected and has sent the connection confirmation byte "10"
if(!isConnected && data.toString('hex') === "10"){
console.log("connected");
isConnected = true;
// Tell the ECU what data we want it to give us
sp.write(command, function(err,results){});
}else{
// Read the data from the stream and parse it
parseData(handleData(data, bytesRequested));
}
});
});
}
// Server part
var app = express();
app.use('/', express.static(path.join(__dirname, 'public')));
var server = app.listen(8090);
console.log('Server listening on port 8090');
// Socket.IO part
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log('New client connected!');
//send data to client
setInterval(function(){
// Change values so you can see it go up when developing
if (process.env.NODE_ENV === "development"){
if(rpm < 7200){
rpm += 11
} else{
rpm = 0
}
if(mph < 120){
mph += 1
} else{
mph = 0
}
if(coolantTemp < 210){
coolantTemp += 1
} else{
coolantTemp = 0
}
}
socket.emit('ecuData', {'rpm':Math.floor(rpm),'mph':Math.floor(mph),'coolantTemp':Math.floor(coolantTemp)});
}, 100);
});