Maria_Christoforidou
4 years ago
3 changed files with 70 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,66 @@ |
|||||
|
import random, requests, json |
||||
|
from time import sleep |
||||
|
|
||||
|
url = 'http://localhost:3000' |
||||
|
previous = [0, 0, 0, 0] #holding the previous values |
||||
|
current = [0, 0, 0, 0] #holding the current values |
||||
|
data_names = ["temperature", "air humidity", "ground humidity", "beaufort"] |
||||
|
first = 0 |
||||
|
percentage_dif = 0 |
||||
|
timer = 0 |
||||
|
|
||||
|
while True: |
||||
|
temperature = random.randint(-100,100) #temperature |
||||
|
air_hum = random.randint(0,100) #air humidity |
||||
|
ground_hum = random.randint(0,100) #ground humidity |
||||
|
wind = random.randint(0,12) #Beaufort scale for wind |
||||
|
|
||||
|
print("Temperature:", temperature, "C") |
||||
|
print("Air humidity:", air_hum, "%") |
||||
|
print("Ground humidity:", ground_hum, "%") |
||||
|
print("Wind:", wind,"Beaufort\n") |
||||
|
|
||||
|
current[0]=temperature #putting the current values in the list |
||||
|
current[1]=air_hum |
||||
|
current[2]=ground_hum |
||||
|
current[3]=wind |
||||
|
|
||||
|
if first == 1: #this code works for the second time so as to have current and previous values in the lists |
||||
|
print("Current temperature: ",current[0], ", Previous temperature: ", previous[0]) |
||||
|
print("Current air humidity: ",current[1], ", Previous air humidity: ", previous[1]) |
||||
|
print("Current ground humidity: ",current[2], ", Previous ground humidity: ", previous[2]) |
||||
|
print("Current wind: ",current[3], ", Previous wind: ", previous[3],"\n") |
||||
|
|
||||
|
if timer==10: #sending current data after 5 minutes of not sending anything |
||||
|
print("Sending everything to server...") |
||||
|
else: |
||||
|
for i in range(4): |
||||
|
if (current[i] == 0) and (previous[i] == 0): #if both values are 0, move on to the next element |
||||
|
continue |
||||
|
elif previous[i] == 0: #cannot have division by 0 |
||||
|
percentage_dif = 11 #giving a random value >10 so as to send the current value to the server, |
||||
|
#since the previous one was 0 and any change is important to send |
||||
|
else: #calculating percentage difference of values |
||||
|
percentage_dif = round(((current[i]-previous[i])/abs(previous[i]))*100,2) |
||||
|
print("Percentage difference between previous and current value of", data_names[i], ":", percentage_dif) |
||||
|
|
||||
|
if (percentage_dif > 10) or (percentage_dif < -10): #if the diffenece is more than 10%, current value is sent to server |
||||
|
print("Sending current value to server...") |
||||
|
|
||||
|
data = {0:current[i]} #sending previous value |
||||
|
r = requests.post(url, data) #sending json object to specified url |
||||
|
timer = 0 #when data is sent to server, make timer 0 |
||||
|
|
||||
|
print("\n\n") |
||||
|
|
||||
|
sleep(30) #to generate the data every 30 seconds |
||||
|
timer+=1 |
||||
|
|
||||
|
|
||||
|
previous[0]=temperature #putting the current values in the list holding the previous values |
||||
|
previous[1]=air_hum |
||||
|
previous[2]=ground_hum |
||||
|
previous[3]=wind |
||||
|
|
||||
|
first = 1 |
||||
|
|
@ -0,0 +1,4 @@ |
|||||
|
const express = require('express'); |
||||
|
const app = express(); |
||||
|
|
||||
|
app.listen(3000, () => console.log('Server listening on port 3000')); |
Loading…
Reference in new issue