Browse Source

Upload files to 'IoT_Project'

master
JohnKampe 4 years ago
parent
commit
92883eae17
  1. 2
      IoT_Project/.env
  2. 1
      IoT_Project/.gitignore
  3. 64
      IoT_Project/page.html
  4. BIN
      IoT_Project/rasp_measurements.db
  5. 101
      IoT_Project/raspberry_pi_code.py

2
IoT_Project/.env

@ -0,0 +1,2 @@
EMAIL=kampe.test@gmail.com
PASSWORD=12345^&*()test

1
IoT_Project/.gitignore

@ -0,0 +1 @@
.env

64
IoT_Project/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>

BIN
IoT_Project/rasp_measurements.db

Binary file not shown.

101
IoT_Project/raspberry_pi_code.py

@ -0,0 +1,101 @@
import requests, json, random
from time import sleep
url = 'http://localhost:3000'
old = [0, 0, 0, 0] #list to hold the previous values
new = [0, 0, 0, 0] #list to hold the new values
names = ["temperature", "air humidity", "ground humidity", "beaufort"]
flag = 0
diff = 0
timer = 0
while True:
temp = random.randint(15,45) #Temperature
ahum = random.randint(0,100) #Air humidity
ghum = random.randint(0,100) #Ground humidity
wind = random.randint(0,17) #Beaufort (Bofor)
print("Temperature:",temp)
print("Air humidity:",ahum)
print("Ground humidity:",ghum)
print("Beaufort:",wind,"\n")
new[0]=temp #assigning to list new the new values
new[1]=ahum
new[2]=ghum
new[3]=wind
if flag == 1: #we don't want to do calculations with zeros
print("New temperature: ",new[0], ", Old temperature: ", old[0])
print("New air humidity: ",new[1], ", Old air humidity: ", old[1])
print("New ground humidity: ",new[2], ", Old ground humidity: ", old[2])
print("New beaufort: ",new[3], ", Old beaufort: ", old[3],"\n")
if timer==10: #if 5 minutes have passed then send the new values to server (10 x 30secs = 300 secs = 5 mins)
print("Send to server: 5 mins passed!")#send all the new data to server
data = {0:new[i]} #sending the new value of temperature
r = requests.post(url, data) #sending a json object to the specified url
data = {1:new[i]} #sending the new value of air humidity
r = requests.post(url, data) #sending a json object to the specified url
data = {2:new[i]} #sending the new value of ground humidity
r = requests.post(url, data) #sending a json object to the specified url
data = {3:new[i]} #sending the new value of beaufort
r = requests.post(url, data) #sending a json object to the specified url
else:
for i in range(4):
if (new[i] == 0) and (old[i] == 0): #if the previous value and the new one are 0 then continue
continue
elif old[i] == 0: #we can't devide a number with zero
if new[i] > 2: #check if the new value is above 2 (10%)
diff = new[i]
elif new[i] == 0: #if the new value is 0 then
if old[i] > 2: #check if the old value is above 2 (10%)
diff = old[i]
else:
diff = round((abs((new[i]-old[i]))/old[i])*100,2) #calculating percentage difference
print("Percentage difference of",names[i],":",diff)
if diff > 10: #if the new and old values differ by 10 then send the new value to server
timer = 0
print('Sending to server (10%)...')
if(i == 0):
data = {0:old[i]} #sending the old value
r = requests.post(url, data)
data = {0:new[i]} #sending the new value
elif(i == 1):
data = {1:old[i]} #sending the old value
r = requests.post(url, data)
data = {1:new[i]} #sending the new value
elif(i == 2):
data = {2:new[i]} #sending the old value
elif(i == 3):
data = {3:new[i]} #sending the old value
r = requests.post(url, data) #sending a json object to the specified url
print('\n')
sleep(5) #produce data every 30 seconds
timer+=1
old[0]=temp #assigning to list old the old values
old[1]=ahum
old[2]=ghum
old[3]=wind
flag = 1
Loading…
Cancel
Save