Fully working plot, server, and sensor with timestamps

This commit is contained in:
2025-12-20 21:10:43 +01:00
parent 552055fd0a
commit ac32052a41
2 changed files with 39 additions and 18 deletions

31
plot.py
View File

@@ -1,7 +1,10 @@
import os
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from datetime import datetime
DATA_DIR = "data"
# Find the latest data file
@@ -13,18 +16,30 @@ latest_file = max(files, key=lambda f: os.path.getmtime(os.path.join(DATA_DIR, f
filepath = os.path.join(DATA_DIR, latest_file)
print(f"Plotting data from {filepath}")
# Load CSV data (skip header)
data = np.loadtxt(filepath, delimiter=",", skiprows=1)
times = data[:, 0]
voltages = data[:, 1]
# Load CSV data
timestamps = []
voltages = []
with open(filepath, "r") as f:
next(f) # skip header
for line in f:
ts_str, v_str = line.strip().split(",")
ts = datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S.%f")
timestamps.append(ts)
voltages.append(float(v_str))
# Plot
plt.figure(figsize=(10, 5))
plt.plot(times, voltages, marker='o', linestyle='-')
plt.xlabel("Time (s)")
plt.figure(figsize=(12, 5))
plt.plot(timestamps, voltages, marker='o', linestyle='-')
plt.xlabel("Time (CET)")
plt.ylabel("Voltage (V)")
plt.title(f"Voltage Measurements ({latest_file})")
plt.grid(True)
# Format x-axis for readable time
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
plt.tight_layout()
plt.show()