50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
const fs = require('fs');
|
|
const http = require('node:http');
|
|
const { SerialPort } = require('serialport')
|
|
const { ReadlineParser } = require('@serialport/parser-readline');
|
|
|
|
const port = new SerialPort({path: '/com9', baudRate: 9600});
|
|
|
|
var str = {};
|
|
let obj;
|
|
|
|
const parser = port.pipe(new ReadlineParser({ delimeter: '\r\n' }));
|
|
|
|
parser.on('data', (data) => {
|
|
if (str != data) {
|
|
console.log(data);
|
|
}
|
|
|
|
str = data;
|
|
try {
|
|
obj = JSON.parse(data);
|
|
} catch (Exception) {
|
|
console.log(Exception);
|
|
}
|
|
});
|
|
|
|
const server = http.createServer((req, res) => {
|
|
if (req.url == '/') {
|
|
fs.readFile('index.html', (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404, 'Not Found');
|
|
res.write("No file found");
|
|
res.end();
|
|
} else {
|
|
res.writeHead(200, {
|
|
'Content-Type': 'text/html'
|
|
});
|
|
res.write(data);
|
|
res.end();
|
|
}
|
|
})
|
|
} else if (req.url == '/pin') {
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
res.end(obj?.firstElement.toString());
|
|
} else if (req.url == '/potonciometer') {
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
res.end(obj?.potenciometer.toString());
|
|
}
|
|
});
|
|
|
|
server.listen(2048); |