-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate24HReportTogetherAndSeparatelyRPI.py
136 lines (107 loc) · 4.19 KB
/
generate24HReportTogetherAndSeparatelyRPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pandas as pd
import matplotlib.pyplot as plt
import time
import os
# Raspberry code to get Full data report, with all (temperature, humidity, and CO2) together and separately without ventilation markings.
# Getting the current directory
current_directory = os.path.dirname(os.path.abspath(__file__))
print(current_directory)
# Reading the CSV file
data = pd.read_csv(f'{current_directory}/sensordata.csv')
# Converting the 'created_at' and 'field1' columns to datetime format
data['created_at'] = pd.to_datetime(data['created_at'])
data['field1'] = pd.to_datetime(data['field1'])
# Setting 'field1' as the index
data.set_index('field1', inplace=True)
# Resampling data to get mean values for each minute for temperature, humidity, and CO2
# We can also sample the data for 2 minutes and more like below.
# It helps when we have huge data, so we can plot by sampling the values
hourly_temperature_means = data['field2'].resample('T').mean()
hourly_humidity_means = data['field3'].resample('T').mean()
hourly_co2_means = data['field4'].resample('T').mean()
def setLabelSize(size):
# to set the label size in graph
for label in plt.legend().get_texts():
label.set_fontsize(size)
def setTicksSize(size):
# to set the Ticks size on each axes in graph
plt.xticks(fontsize=size)
plt.yticks(fontsize=size)
def saveCO2andTemperatureHumiditySeparately():
# Plotting temperature and humidity
plt.figure(figsize=(15, 8))
# Temperature plot
plt.plot(hourly_temperature_means.index, hourly_temperature_means,
label='Temperature', marker='o', color='tomato')
# Humidity plot
plt.plot(hourly_humidity_means.index, hourly_humidity_means,
label='Humidity', marker='o', color='royalblue')
plt.xlabel('Time', fontsize=20)
plt.ylabel('Value', fontsize=20)
plt.title('Temperature and Humidity Data', fontsize=20)
plt.grid(True)
plt.legend()
setLabelSize(20)
setTicksSize(14)
plt.tight_layout()
# Saving the plot as a photo
plt.savefig(
f'{folder_name}/temperature_humidity_data_{getCurrentTime()}.png')
plt.close()
# Plotting CO2 graph
plt.figure(figsize=(15, 8))
# CO2 plot
plt.plot(hourly_co2_means.index, hourly_co2_means,
label='CO2', marker='o', color='orangered')
plt.xlabel('Time', fontsize=20)
plt.ylabel('CO2 Value', fontsize=20)
plt.title('CO2 Trends', fontsize=20)
plt.grid(True)
plt.legend()
setLabelSize(20)
setTicksSize(18)
plt.tight_layout()
# Saving the plot as a photo
plt.savefig(f'{folder_name}/co2_data_{getCurrentTime()}.png')
plt.close()
def saveAllTogether():
# Plotting all the data together in a single graph for 24 hours
plt.figure(figsize=(15, 8))
# Temperature plot
plt.plot(hourly_temperature_means.index, hourly_temperature_means,
label='Temperature', marker='o', color='tomato')
# Humidity plot
plt.plot(hourly_humidity_means.index, hourly_humidity_means,
label='Humidity', marker='o', color='royalblue')
# CO2 plot
plt.plot(hourly_co2_means.index, hourly_co2_means,
label='CO2', marker='o', color='red')
plt.xlabel('Time', fontsize=20)
plt.ylabel('Value', fontsize=20)
plt.title('CO2, Temperature, and Humidity Data Trends', fontsize=20)
plt.grid(True)
plt.legend()
setLabelSize(20)
setTicksSize(18)
plt.tight_layout()
# Saving the plot as a photo
plt.savefig(f'{folder_name}/24_hours_data_{getCurrentTime()}.png')
# plt.show()
plt.close()
def create_folder(base_folder_name):
# To create a folder with time stamp for each time the code executes
folder_name = f"{base_folder_name}_{getCurrentTime()}"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
return folder_name
def getCurrentTime():
# To get current time in a particular format
t = time.localtime()
current_time = time.strftime("%Y-%m-%dT%H:%M:%S%z", t)
return current_time
if __name__ == "__main__":
# Creating the folder at first and saving all files into it
folder_name = create_folder(
f"{current_directory}/24hrsDataPlots")
saveCO2andTemperatureHumiditySeparately()
saveAllTogether()