Browse Source

Upload files to ''

master
JohnKampe 4 years ago
parent
commit
7dd3f01954
  1. 181
      index.js
  2. 5245
      npm-debug.log
  3. 1293
      package-lock.json
  4. 24
      package.json
  5. 64
      page.html

181
index.js

@ -0,0 +1,181 @@
const app = require('express')(); //loading express module
const http = require('http').createServer(app); //creating server
const io = require('socket.io')(http); //loading socket.io module using HTTP transport
const parser = require('body-parser'); //loading body-parser module
const Data = require('nedb'); //loading nedb module
const nodemailer = require('nodemailer'); //loading nodemailer module
require('dotenv').config(); //for database security
var temp_old_value = 0; //inserting the previous value of temperature
var temp_flag = 0; //flag to check if we got the previous value of temperature
var temp_result = 0; //inserting the percentage difference of temperature
var hum_old_value = 0; //inserting the previous value of air humidity
var hum_flag = 0; //flag to check if we got the previous value of air humidity
var hum_result = 0; //inserting the percentage difference of air humidity
var temp_list = []; //we need the lists to send the data to client
var air_list = [];
var ground_list = [];
var beauf_list = [];
var temp_count = 0; //our lists will have 10 values so we need counters to insert and update the values
var air_count = 0;
var ground_count = 0;
var beauf_count = 0;
app.use(parser.urlencoded({extended: true})); //precises that the request.body object will contain values of any type instead of just strings
const database = new Data('rasp_measurements.db'); //creating database with name rasp_measurements.db
database.loadDatabase(); //loading database or creating it if it doesn't exist
function calculate(old, neww){ //calculating the percentage difference between the old and the new measure
return (neww-old)/old*100;
}
app.get('/', (req,res) => { //sending the page.html as a result of clients request
res.sendFile(__dirname + '/page.html');
});
app.post('/', (request, response) => { //server sends data to Gmail, database and the client
let transporter = nodemailer.createTransport({//to notify the client we need specify the name of the email service we will use
service: 'gmail',
auth: { //the file .env has the email and password and the file .gitignore makes sure the file .env never gets pushed
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
if (request.body[0]){
if(temp_flag == 0){ //if we don't have the previous value then..
temp_old_value = request.body[0]; //..insert it to temp_old_value
temp_flag = 1; //then set temp_flag to 1
} else if(temp_flag == 1){ //now that we have the previous value we will get the new value..
console.log('Old value:',temp_old_value,', New value:',request.body[0]);
temp_result = calculate(temp_old_value, request.body[0]); //..to find the percentage defference between the new and old value
console.log('Temperature percentage difference:',temp_result.toFixed(1) + "%!\n");
database.insert({measure: 'temperature',value:parseInt(request.body[0])}); //inserting the new values to database
temp_flag = 0; //making temp_flag = 0 to do the same things for the next values
temp_list[temp_count] = parseInt(request.body[0]); //inserting the new value to list temp_list at temp_count position
console.log('List of temperatures:',temp_list); //for better understanding
temp_count +=1; //increasing the temp_count counter
if(temp_count == 10){ //if we have 10 values in the list then insert at the 11th position the correct id (0 for temperature)
temp_list[temp_count] = 0; //inserting the correct id
temp_count = 0; //start from the begining and update the values
io.on('connection', socket => { //then send the list to client
console.log('We have a connection');
socket.emit('graph', temp_list);
});
}
if(temp_result > 40){ //if temperature increased by 50% then send an email to kampe.test@gmail.com to warn him
let mailOptions = {
from: process.env.EMAIL,
to: 'kampe.test@gmail.com',
subject: 'ATTENTION',
text: 'Temperature increased alot!'
};
transporter.sendMail(mailOptions, function(err, data) { //sending the email
if (err){
console.log('Error Occurs');
} else {
console.log('Email sent (for temperature).');
}
});
}
}
} else if (request.body[1]) {
if(hum_flag == 0){ //if we don't have the previous value then..
hum_old_value = request.body[1]; //..insert it to temp_old_value
hum_flag = 1; //then set temp_flag to 1
} else if(hum_flag == 1){ //now that we have the previous value we will get the new value..
console.log('Old value:',hum_old_value,', New value:',request.body[1]);
hum_result = calculate(hum_old_value, request.body[1]);//..to find the percentage defference between the new and old value
console.log('Air humidity percentage difference:',hum_result.toFixed(1) + '%!');
database.insert({measure: 'air humidity',value:parseInt(request.body[1])}); //inserting the new values to database
hum_flag = 0; //making hum_flag = 0 to do the same things for the next values
air_list[air_count] = parseInt(request.body[1]); //inserting the new value to list air_list at air_count position
console.log('List of air humidity:',air_list); //for better understanding
air_count +=1; //increasing the air_count counter
if(air_count == 10){ //if we have 10 values in the list then insert at the 11th position the correct id (1 for air humidity)
air_list[air_count] = 1; //inserting the correct id
air_count = 0; //start from the begining and update the values
io.on('connection', socket => { //then send the list to client
console.log('We have a connection');
socket.emit('graph', air_list);
});
}
if(hum_result < 50){ //if humidity decreased by 50% then send an email to kampe.test@gmail.com to warn him
let mailOptions = {
from: process.env.EMAIL,
to: 'kampe.test@gmail.com',
subject: 'ATTENTION',
text: 'Humidity decreased alot!'
};
transporter.sendMail(mailOptions, function(err, data) { //sending the email
if (err){
console.log('Error Occurs');
} else {
console.log('Email sent (for air humidity).');
}
});
}
}
} else if (request.body[2]) {
ground_list[ground_count] = parseInt(request.body[2]); //inserting the new value to list ground_list at ground_count position
console.log('List of air humidity:',ground_list); //for better understanding
ground_count +=1; //increasing the air_count counter
if(ground_count == 10){ //if we have 10 values in the list then insert at the 11th position the correct id (2 for ground humidity)
ground_list[ground_count] = 2; //inserting the correct id
ground_count = 0; //start from the begining and update the values
io.on('connection', socket => { //then send the list to client
console.log('We have a connection');
socket.emit('graph', ground_list);
});
}
console.log('Ground humidity value: ',request.body[2]);
database.insert({measure: 'ground humidity',value:parseInt(request.body[2])}); //inserting the new values to database
} else if (request.body[3]) {
beauf_list[beauf_count] = parseInt(request.body[3]); //inserting the new value to list beauf_list at beauf_count position
console.log('List of beaufort:',beauf_list); //for better understanding
beauf_count +=1; //increasing the air_count counter
if(beauf_count == 10){ //if we have 10 values in the list then insert at the 11th position the correct id (3 for beaufort)
beauf_list[beauf_count] = 3; //inserting the correct id
beauf_count = 0; //start from the begining and update the values
io.on('connection', socket => { //then send the list to client
console.log('We have a connection');
socket.emit('graph', beauf_list);
});
}
console.log('Ground beaufort value: ',request.body[3]);
database.insert({measure: 'beaufort',value:parseInt(request.body[3])}); //inserting the new values to database
}
response.end(); //ending the event
});
http.listen(3000, () => { console.log('Server is listening...'); }); //listening at port 3000

5245
npm-debug.log

File diff suppressed because it is too large

1293
package-lock.json

File diff suppressed because it is too large

24
package.json

@ -0,0 +1,24 @@
{
"name": "server",
"version": "1.0.0",
"description": "Raspberry Pi data",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"example",
"data",
"raspberry",
"pi"
],
"author": "John Kaberakis",
"license": "ISC",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"nedb": "^1.8.0",
"nodemailer": "^6.4.17",
"socket.io-client": "^3.1.0"
}
}

64
page.html

@ -0,0 +1,64 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Raspberry Pi Measures</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<div id="myDiv"></div> <!-- Presenting the graph at localhost:3000/ -->
<script>
const socket = io(); //connecting with server
var data = []; //initializing list data, we will put the
//different traces (trace0, trace1, trace2, trace3) in order to plot them
var trace0 = {}; //trace of temperature
var trace1 = {}; //trace of air humidity
var trace2 = {}; //trace of ground humidity
var trace3 = {}; //trace of beaufort
socket.on('graph', (value) => { //on event with name 'graph' get the values from the server
if(value[10] == 0){ //if the id = 0 then it's temperature values
trace0 = { //update the trace of temperature
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9]],
type: 'scatter',
mode: 'lines',
name: 'Temperature'
};
} else if(value[10] == 1){ //if the id = 1 then it's air humidity values
trace1 = { //update the trace of air humidity
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9]],
type: 'scatter',
mode: 'lines',
name: 'Air humidity'
};
}else if(value[10] == 2){ //if the id = 2 then it's ground humidity values
trace2 = { //update the trace of ground humidity
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9]],
type: 'scatter',
mode: 'lines',
name: 'Ground humidity'
};
}else if(value[10] == 3){ //if the id = 3 then it's beaufort values
trace3 = { //update the trace of beaufort
x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
y: [value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8],value[9]],
type: 'scatter',
mode: 'lines',
name: 'Beaufort'
};
}
data = [trace0, trace1, trace2, trace3]; //inserting in list data the traces
console.log(data); //printing the traces for debugging reasons
Plotly.newPlot('myDiv', data); //plotting the traces
});
</script>
</body>
</html>
Loading…
Cancel
Save