-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (116 loc) · 3.45 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
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
/* eslint-disable node/no-missing-require */
/**
* This is a small example app to turn off and on
* the built-in LED of an arduino by data sent
* from the browser with socket.io.
*/
'use strict';
// Initialize application constants
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const bodyParser = require("body-parser");
const ab2str = require('arraybuffer-to-string')
const port = process.env.PORT || 3000;
const SerialPort = require('serialport');
//const SerialPort = serialport.SerialPort;
const serial = new SerialPort('COM3', {
baudRate: 9600,
parser: SerialPort.parsers.byteLength(1)
});
// Values to send over to Arduino.
const HIGH = Buffer.from([1]);
const LOW = Buffer.from([0]);
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
/* ===========================================
*
* Setup a simple server.
*
=========================================== */
app.get('/', (req, res) => {
res.render('home');
});
http.listen(port, () => {
console.log(`listening on *:${port}`);
});
/* ===========================================
*
* Socket.io stuff
*
=========================================== */
io.on('connection', (socket) => {
console.log('a user connected');
/**
* Socket listener to determine whether or not to send HIGH / LOW
* values to Arduino.
*/
socket.on('message', (msg) => {
console.log('Message received: ', msg);
switch (msg) {
case '1':
serial.write(HIGH);
break;
case '2':
serial.write(HIGH);
break;
default:
break;
}
});
});
/* ===========================================
*
* Serialport stuff
*
=========================================== */
serial.on('open', () => {
console.log('Port is open!');
});
/**
* EventListener to receive data from .ino script uploaded to Arduino.
*
*/
var message = [];
var arr = [];
var messag1;
serial.on('data', (data) => {
//let message;
process.stdout.write(data.toString("utf8"));
message = data.toString("utf8");
arr.push(data.toString("utf8"));
io.sockets.emit('new message', message);
});
//console.log(messag1);
serial.on('close', () => {
console.log('Serial port disconnected.');
io.sockets.emit('close');
});
var k = {
"Wood": 0.08,
"Brick": 0.6,
"Concrete": 0.8,
"Glass": 0.8
}, k_value, kt;
var R_value, a = 0.000091, tDiff = 0.0, d, lambda = 0.0, t1, t2;
app.post("/calculate", function(req, res) {
kt = req.body.k;
k_value = k[req.body.k];
d = parseFloat(req.body.d);
t1 = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];
t2 = arr[7] + arr[8] + arr[9] + arr[10] + arr[11];
tDiff = Math.abs(parseFloat(t1) - parseFloat(t2));
console.log(tDiff);
lambda = (k_value * a * tDiff)/d;
console.log(lambda);
R_value = d/lambda;
console.log(R_value);
//res.redirect({d: d, lambda: lambda, k: req.body.k, a: a, tDiff: tDiff, r: R_value},"/calculate1");
res.redirect("/calculate");
});
app.get("/calculate", function(req, res) {
res.render("value.ejs", {d: d, lambda: lambda, k: kt, a: a, tDiff: tDiff, r: R_value});
});