arduino websocket serial project
This commit is contained in:
49
JavaScript/adruino-temp-sensor/client/draw.js
Normal file
49
JavaScript/adruino-temp-sensor/client/draw.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const MAX_POINTS = 60;
|
||||
const X_MAX = 60;
|
||||
const Y_MAX = 40;
|
||||
const X_DIV = 5;
|
||||
const Y_DIV = 4;
|
||||
|
||||
let canvas = document.getElementById('graph');
|
||||
let ctx = canvas.getContext('2d');
|
||||
let points = [];
|
||||
|
||||
function addPoint(temp) {
|
||||
if (points.length <= MAX_POINTS) {
|
||||
points.push(temp);
|
||||
}
|
||||
else {
|
||||
for (let i = 0; i <= MAX_POINTS - 1; i++) {
|
||||
points[i] = points[i + 1];
|
||||
}
|
||||
points[MAX_POINTS] = temp;
|
||||
}
|
||||
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
ctx.strokeStyle = "black";
|
||||
ctx.font = "20px Courrier New";
|
||||
ctx.textAlign="right";
|
||||
for (let i = X_DIV; i < MAX_POINTS; i += X_DIV) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(i * (canvas.width / MAX_POINTS), 0);
|
||||
ctx.lineTo(i * (canvas.width / MAX_POINTS), canvas.height);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
for (let i = Y_DIV; i < Y_MAX; i += Y_DIV) {
|
||||
ctx.fillText(Y_MAX - i , canvas.width - 1, i * (canvas.height / Y_MAX) - 2);
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, i * (canvas.height / Y_MAX));
|
||||
ctx.lineTo(canvas.width, i * (canvas.height / Y_MAX));
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
ctx.strokeStyle = "blue";
|
||||
for (let i = 0; i < points.length && points.length > 1; i++) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(i * (canvas.width / MAX_POINTS), canvas.height - (points[i] * 10));
|
||||
ctx.lineTo((i + 1) * (canvas.width / MAX_POINTS), canvas.height - (points[i + 1] * 10));
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
16
JavaScript/adruino-temp-sensor/client/index.html
Normal file
16
JavaScript/adruino-temp-sensor/client/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<title>Temperature</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="graph" width="600" height="400"></canvas>
|
||||
<script src="draw.js"></script>
|
||||
<script src="main.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
26
JavaScript/adruino-temp-sensor/client/main.js
Normal file
26
JavaScript/adruino-temp-sensor/client/main.js
Normal file
@@ -0,0 +1,26 @@
|
||||
console.log('Connecting to server')
|
||||
|
||||
var socket = io.connect('benkyd.duckdns.org:8081');
|
||||
let temp = [];
|
||||
|
||||
socket.on('connect', function(data) {
|
||||
socket.emit('handshake', 'client');
|
||||
});
|
||||
|
||||
socket.on('handshake', function(data) {
|
||||
socket.emit('ready');
|
||||
});
|
||||
|
||||
socket.on('temp', function(data) {
|
||||
console.log(data);
|
||||
addPoint(data);
|
||||
});
|
||||
|
||||
socket.on('lasthour', function(data) {
|
||||
console.log(data);
|
||||
data.forEach(point => {
|
||||
if (point != NaN || point != null || point != 'undefined' || point != 'NaN' || point != 'null' || point != 'undefined') {
|
||||
addPoint(point);
|
||||
}
|
||||
});
|
||||
});
|
||||
15
JavaScript/adruino-temp-sensor/client/style.css
Normal file
15
JavaScript/adruino-temp-sensor/client/style.css
Normal file
@@ -0,0 +1,15 @@
|
||||
body {
|
||||
background-color: #2f2f2f;
|
||||
}
|
||||
|
||||
canvas {
|
||||
background-color: rgb(245,245,245);
|
||||
padding: 0;
|
||||
margin: auto;
|
||||
display: block;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
}
|
||||
72
JavaScript/adruino-temp-sensor/index.js
Normal file
72
JavaScript/adruino-temp-sensor/index.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const SerialPort = require('serialport');
|
||||
const parsers = SerialPort.parsers;
|
||||
const fs = require('fs');
|
||||
let express = require('express');
|
||||
let app = express();
|
||||
let server = require('http').createServer(app);
|
||||
let io = require('socket.io')(server);
|
||||
/* COMMUNICATION WITH ARDUINO AND CLENSING OF DATA */
|
||||
|
||||
let lastHour = []
|
||||
let temperature;
|
||||
const parser = new parsers.Readline({
|
||||
delimiter: '\r\n'
|
||||
});
|
||||
|
||||
const port = new SerialPort('COM3', {
|
||||
baudRate: 115200
|
||||
});
|
||||
|
||||
port.pipe(parser);
|
||||
|
||||
port.on('open', () => {
|
||||
console.log('Serial connection on COM3 initialized')
|
||||
});
|
||||
|
||||
port.on('data', (data) => {
|
||||
let temp = parseFloat(data.toString());
|
||||
if (temp == NaN) return;
|
||||
if (temp == NaN) return;
|
||||
if (temp == undefined) return;
|
||||
if (temp == null) return;
|
||||
if (temp + 5 < temperature) return;
|
||||
if (temp - 5 > temperature) return;
|
||||
temperature = temp;
|
||||
});
|
||||
|
||||
/* SIMPLE WEBSERVER */
|
||||
|
||||
app.use('/', express.static(__dirname + '/client'));
|
||||
app.get('/', function(req, res) {
|
||||
res.sendFile(__dirname + '/client/index.html');
|
||||
});
|
||||
app.listen(8080);
|
||||
console.log('Server listening on 80')
|
||||
|
||||
/* WEBSOCKETS */
|
||||
|
||||
server.listen(8081);
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
let ip = socket.request.connection.remoteAddress;
|
||||
if (ip.startsWith('::ffff:')) ip = ip.substring(7);
|
||||
|
||||
socket.on('handshake', function(data) {
|
||||
console.log('New client connecting at ' + ip)
|
||||
socket.emit('handshake', 'server');
|
||||
});
|
||||
|
||||
socket.on('ready', function(data) {
|
||||
console.log('Handshake with ' + ip + ' successful')
|
||||
socket.emit('lasthour', lastHour);
|
||||
|
||||
setInterval(() => {
|
||||
socket.emit('temp', temperature);
|
||||
}, 60000);
|
||||
});
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
lastHour.push(temperature);
|
||||
if (lastHour.length > 60) lastHour.splice(0,1);
|
||||
}, 60000)
|
||||
2175
JavaScript/adruino-temp-sensor/package-lock.json
generated
Normal file
2175
JavaScript/adruino-temp-sensor/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
JavaScript/adruino-temp-sensor/package.json
Normal file
18
JavaScript/adruino-temp-sensor/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "adruino-temp-sensor",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"express": "^4.16.3",
|
||||
"fs": "0.0.1-security",
|
||||
"serialport": "^6.2.2",
|
||||
"socket.io": "^2.1.1"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Ben (plane000)",
|
||||
"license": "MIT"
|
||||
}
|
||||
Reference in New Issue
Block a user