Commit fb1278bc authored by Rousseau Vincent's avatar Rousseau Vincent
Browse files

Filter values every hour

parent e118dcfa
from sense_hat import SenseHat
from datetime import datetime
from datetime import timedelta
import time
from pathlib import Path
import csv
import numpy as np
sense = SenseHat()
def float_format(f):
return '{:.2f}'.format(f)
def float_mean(values):
return float_format(np.mean(values))
def get_data():
humidity = float_format(sense.get_humidity())
temp_humidity = float_format(sense.get_temperature_from_humidity())
#print("Humidity sensor : {} %rH , {} °C".format(humidity, temp_humidity))
humidity = []
temp_humidity = []
pressure = []
temp_pressure = []
# Store sensor values every second during one hour
for _ in range(3600):
humidity.append(sense.get_humidity())
temp_humidity.append(sense.get_temperature_from_humidity())
pressure.append(sense.get_pressure())
temp_pressure.append(sense.get_temperature_from_pressure())
time.sleep(1)
pressure = float_format(sense.get_pressure())
temp_pressure = float_format(sense.get_temperature_from_pressure())
#print("Humidity sensor : {} %rH , {} °C".format(humidity, temp_humidity))
#print("Pressure sensor : {} Millibars , {} °C".format(pressure, temp_pressure))
return [datetime.now().timestamp(), humidity, temp_humidity, pressure, temp_pressure]
# Get the mean value during the hour
return [(datetime.now()-timedelta(minutes=30)).timestamp(),
float_mean(humidity),
float_mean(temp_humidity),
float_mean(pressure),
float_mean(temp_pressure)]
# Create folder to store the data
data_folder = Path('./data')
......@@ -26,7 +42,7 @@ data_folder.mkdir(parents=True, exist_ok=True)
try:
while(True):
current_month = datetime.now().month
file_date = datetime.now().strftime('%Y_%m_%d_%H_%M')
file_date = datetime.now().strftime('%Y_%m_%d_%H')
file_name = data_folder/str('sense_hat_data_' + file_date + '.csv')
with file_name.open('w') as csvfile:
writer = csv.writer(csvfile)
......@@ -34,7 +50,6 @@ try:
while(True):
writer.writerow(get_data())
time.sleep(1)
if(current_month != datetime.now().month):
break
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment