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

Write data to csv

parent 7d84d0a0
from sense_hat import SenseHat from sense_hat import SenseHat
from datetime import datetime
import time
from pathlib import Path
import csv
sense = SenseHat() sense = SenseHat()
humidity = sense.get_humidity() def float_format(f):
temp_humidity = sense.get_temperature_from_humidity() return '{:.2f}'.format(f)
print("Humidity sensor : {:.2f} %rH , {:.2f} °C".format(humidity, temp_humidity))
pressure = sense.get_pressure() def get_data():
temp_pressure = sense.get_temperature_from_pressure() humidity = float_format(sense.get_humidity())
print("Pressure sensor : {:.2f} Millibars , {:.2f} °C".format(pressure, temp_pressure)) temp_humidity = float_format(sense.get_temperature_from_humidity())
#print("Humidity sensor : {} %rH , {} °C".format(humidity, temp_humidity))
pressure = float_format(sense.get_pressure())
temp_pressure = float_format(sense.get_temperature_from_pressure())
#print("Pressure sensor : {} Millibars , {} °C".format(pressure, temp_pressure))
return [datetime.now().timestamp(), humidity, temp_humidity, pressure, temp_pressure]
# Create folder to store the data
data_folder = Path('./data')
data_folder.mkdir(parents=True, exist_ok=True)
try:
while(True):
current_minute = datetime.now().minute
file_date = datetime.now().strftime('%Y_%m_%d_%H_%M')
file_name = data_folder/str('sense_hat_data_' + file_date + '.csv')
with file_name.open('w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['timestamp', 'humidity', 'temp_humidity', 'pressure', 'temp_pressure'])
while(True):
writer.writerow(get_data())
time.sleep(1)
if(current_minute != datetime.now().minute):
break
except KeyboardInterrupt:
pass
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