Skip to content

Commit

Permalink
metrics/dashboard continued
Browse files Browse the repository at this point in the history
  • Loading branch information
s-t-a-n committed Jun 14, 2024
1 parent a2aa147 commit 7b7d3db
Show file tree
Hide file tree
Showing 4 changed files with 69,537 additions and 4,615 deletions.
4 changes: 2 additions & 2 deletions src/python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


# read csv from a github repo
df = pd.read_csv("/home/stan/projects/KasKas/src/python/kaskas_data.cv")
df = pd.read_csv("/home/stan/projects/KasKas/src/python/kaskas_data.csv")


st.set_page_config(
Expand Down Expand Up @@ -57,7 +57,7 @@
fig_col1, fig_col2 = st.columns(2)
with fig_col1:
st.markdown("### Element surface temperature")
fig1 = px.line(data_frame = df, y = 'HEATER_SURFACE_TEMP', x = 'TIMESTAMP')
fig1 = px.scatter(data_frame = df, y = 'HEATER_SURFACE_TEMP', x = 'TIMESTAMP')
st.write(fig1)
with fig_col2:
st.markdown("### Climate temperature")
Expand Down
57 changes: 24 additions & 33 deletions src/python/datacollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
from datetime import datetime
import numpy as np
import os


def is_float(string: str) -> bool:
Expand All @@ -12,29 +13,30 @@ def epoch(dt64) -> int:
return dt64.astype('datetime64[s]').astype('int')


timestamps: list[np.datetime64] = []
collector: list[list[float]] = []
# timestamps: list[np.datetime64] = []
# collector: list[list[float]] = []

epoch_start = epoch(np.datetime64('now'))

def new_datapoint(value: float, idx: int ):
global timestamps
global collector
global epoch_start
# def new_datapoint(value: float, idx: int ):
# global timestamps
# global collector
# global epoch_start

while len(collector) < idx + 1:
collector.append([])
# while len(collector) < idx + 1:
# collector.append([])

if(idx == 0):
timestamp = epoch(np.datetime64('now')) - epoch_start
timestamps.append(timestamp)
# if(idx == 0):
# timestamp = epoch(np.datetime64('now')) - epoch_start
# timestamps.append(timestamp)

collector[idx].append(value)
# collector[idx].append(value)

import glob
import serial

def available_serial_ports():
""" Find available serial ports"""
def find_serial_ports():
ports = glob.glob('/dev/ttyACM[0-9]*')

res = []
Expand All @@ -47,22 +49,16 @@ def available_serial_ports():
pass
return res

ports = available_serial_ports()
ports = find_serial_ports()
assert len(ports) > 0, "No serial ports available"
ser = serial.Serial(ports[0], 115200, timeout=1.5)
ser = serial.Serial(ports[0], 115200, timeout=3)
assert ser.is_open, "Serial connection could not be made"

def collect_to(csv_writer) -> None:
global timestamps
global collector

line = ser.readline().decode("ascii").replace('\r', '').replace('\n', '') # read a '\n' terminated line
values = line.split('|')[:-1]
# print(values)
if (len(values) > 0 and is_float(values[0])):
for idx, v in enumerate(values):
new_datapoint(float(v), idx)

writer.writerow([datetime.now()] + values);
csv_writer.writerow([datetime.now()] + values);
print(f"{datetime.now()}: {line}")

else:
Expand All @@ -72,27 +68,22 @@ def collect_to(csv_writer) -> None:

import csv

with open('kaskas_data.csv', 'w', newline='') as file:
writer = csv.writer(file)
fname = 'kaskas_data.csv'

# ser.write(bytearray("",'ascii'))
# ser.read_all()
with open(fname, 'a+', newline='') as file:
writer = csv.writer(file)
# ser.write(bytearray("MTC!stopDatadump\n",'ascii'))
# time.sleep(1)
# ser.read_all()
ser.write(bytearray("MTC!startDatadump\n",'ascii'))
time.sleep(1)

line = ser.readline().decode("utf-8").replace('\r', '').replace('\n', '')
field_names = line.split('|')[:-1]
field_names = ["TIMESTAMP"] + field_names
print(field_names)
writer.writerow(field_names)
if os.stat(fname).st_size == 0: # only write headers when empty
writer.writerow(field_names)

while True:
collect_to(writer)
# writer.writerow(["Alina Hricko", "23", "Ukraine"])
# writer.writerow(["Isabel Walter", "50", "United Kingdom"])
file.flush()


Expand Down
Loading

0 comments on commit 7b7d3db

Please sign in to comment.