environmental_sensors.py 1.84 KB
Newer Older
1
from sense_hat import SenseHat
Rousseau Vincent's avatar
Rousseau Vincent committed
2
from datetime import datetime
Rousseau Vincent's avatar
Rousseau Vincent committed
3
from datetime import timedelta
Rousseau Vincent's avatar
Rousseau Vincent committed
4
5
6
import time
from pathlib import Path
import csv
Rousseau Vincent's avatar
Rousseau Vincent committed
7
import numpy as np
8
9
10

sense = SenseHat()

Rousseau Vincent's avatar
Rousseau Vincent committed
11
12
def float_format(f):
    return '{:.2f}'.format(f)
13

Rousseau Vincent's avatar
Rousseau Vincent committed
14
15
16
def float_mean(values):
    return float_format(np.mean(values))

Rousseau Vincent's avatar
Rousseau Vincent committed
17
def get_data():
Rousseau Vincent's avatar
Rousseau Vincent committed
18
19
20
21
22
23
24
25
26
27
28
    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)
Rousseau Vincent's avatar
Rousseau Vincent committed
29

Rousseau Vincent's avatar
Rousseau Vincent committed
30
    #print("Humidity sensor : {} %rH , {} °C".format(humidity, temp_humidity))
Rousseau Vincent's avatar
Rousseau Vincent committed
31
    #print("Pressure sensor : {} Millibars , {} °C".format(pressure, temp_pressure))
Rousseau Vincent's avatar
Rousseau Vincent committed
32
33
34
35
36
37
    # 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)]
Rousseau Vincent's avatar
Rousseau Vincent committed
38
39
40
41
42
43

# Create folder to store the data
data_folder = Path('./data')
data_folder.mkdir(parents=True, exist_ok=True)
try:
    while(True):
Rousseau Vincent's avatar
Rousseau Vincent committed
44
        current_month = datetime.now().month
Rousseau Vincent's avatar
Rousseau Vincent committed
45
        file_date = datetime.now().strftime('%Y_%m_%d_%H')
Rousseau Vincent's avatar
Rousseau Vincent committed
46
47
48
49
50
51
52
        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())
Rousseau Vincent's avatar
Rousseau Vincent committed
53
                csvfile.flush()
Rousseau Vincent's avatar
Rousseau Vincent committed
54
                if(current_month != datetime.now().month):
Rousseau Vincent's avatar
Rousseau Vincent committed
55
56
57
58
                    break

except KeyboardInterrupt:
    pass