WxBox/logplotter.py
2024-04-05 18:51:21 +03:00

102 lines
3.4 KiB
Python

import re
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
def parse_sensor_log(filename):
timestamps = []
temperatures = []
humidities = []
eco2_concentrations = []
tvoc_concentrations = []
# Extract start time from the filename
# start_time_str = re.search(r'(\d{6})', filename).group(1)
start_time_str = re.search(r'-(\d{6})\.', filename).group(1)
print(start_time_str)
start_time = datetime.strptime(start_time_str, '%H%M%S')
with open(filename, 'r') as file:
lines = file.readlines()
for line in lines:
if "Waiting" in line:
# Increment the current time by one minute
start_time += timedelta(minutes=1)
continue
match = re.match(r'^temperature: (.+) C$', line)
if match:
temperatures.append(float(match.group(1)))
timestamps.append(start_time)
continue
match = re.match(r'^humidity: (.+) %$', line)
if match:
humidities.append(float(match.group(1)))
continue
match = re.match(r'^eCO2 concentration: (.+) ppm$', line)
if match:
eco2_concentrations.append(float(match.group(1)))
continue
match = re.match(r'^TVOC concentration: (.+) ppb$', line)
if match:
tvoc_concentrations.append(float(match.group(1)))
continue
return timestamps, temperatures, humidities, eco2_concentrations, tvoc_concentrations
# Multiline
def plot_sensor_data(timestamps, temperatures, humidities, eco2_concentrations, tvoc_concentrations):
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.plot(timestamps, temperatures, 'r.-')
plt.xlabel('Time')
plt.ylabel('Temperature (C)')
plt.title('Temperature over Time')
plt.subplot(2, 2, 2)
plt.plot(timestamps, humidities, 'g.-')
plt.xlabel('Time')
plt.ylabel('Humidity (%)')
plt.title('Humidity over Time')
plt.subplot(2, 2, 3)
plt.plot(timestamps, eco2_concentrations, 'b.-')
plt.xlabel('Time')
plt.ylabel('eCO2 Concentration (ppm)')
plt.title('eCO2 Concentration over Time')
plt.subplot(2, 2, 4)
plt.plot(timestamps, tvoc_concentrations, 'm.-')
plt.xlabel('Time')
plt.ylabel('TVOC Concentration (ppb)')
plt.title('TVOC Concentration over Time')
plt.tight_layout()
plt.show()
# def plot_sensor_data(timestamps, temperatures, humidities, eco2_concentrations, tvoc_concentrations):
# plt.figure(figsize=(12, 8))
# plt.plot(timestamps, temperatures, 'r.-', label='Temperature (C)')
# plt.plot(timestamps, humidities, 'g.-', label='Humidity (%)')
# plt.plot(timestamps, eco2_concentrations, 'b.-', label='eCO2 Concentration (ppm)')
# plt.plot(timestamps, tvoc_concentrations, 'm.-', label='TVOC Concentration (ppb)')
# plt.xlabel('Time')
# plt.ylabel('Sensor Readings')
# plt.title('Sensor Readings over Time')
# plt.legend()
# plt.grid(True)
# plt.tight_layout()
# plt.show()
if __name__ == "__main__":
filename = './logs/device-monitor-240403-030529.log'
timestamps, temperatures, humidities, eco2_concentrations, tvoc_concentrations = parse_sensor_log(filename)
plot_sensor_data(timestamps, temperatures, humidities, eco2_concentrations, tvoc_concentrations)