How to display data from Arduino UNO to streamlit app in table format?

214 Views Asked by At

I am using Arduino UNO and streamlit for streaming data on a dashboard. and below is my code:

import serial
import streamlit as st
import pandas as pd

# Create a serial connection to Arduino
arduino = serial.Serial('COM5', 9600)  # Replace 'COM5' with the appropriate port

# Create an empty dictionary to store the data
data = {'Sensor1': [], 'Sensor2': [], 'Sensor3': [], 'Sensor4': [], 'Sensor5': [], 'Sensor6': []}

# Create Streamlit app
st.title('Arduino Data Table')

# Continuously read data from Arduino and update the DataFrame
while True:
    line = arduino.readline().decode().strip()  # Read a line from Arduino

    if line.startswith("Current"):
        current = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor1'].append(current)
    elif line.startswith("Temperature 1"):
        temp1 = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor2'].append(temp1)
    elif line.startswith("Temperature 2"):
        temp2 = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor3'].append(temp2)
    elif line.startswith("Total Voltage"):
        total_volt = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor4'].append(total_volt)
    elif line.startswith("Maximum Voltage"):
        max_volt = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor5'].append(max_volt)
    elif line.startswith("Minimum Voltage"):
        min_volt = float(line.split(':')[1].strip().split(' ')[0])
        data['Sensor6'].append(min_volt)

    # Check if any column has a missing value and append None to maintain equal lengths
    max_len = max(len(data[col]) for col in data)
    for col in data:
        if len(data[col]) < max_len:
            data[col].append(None)

    # Create DataFrame from the data dictionary
    table = pd.DataFrame(data)
st.write(table)

But this code is displaying data in diagonal form rather than proper table form. Can someone help me to achieve my desired data ? here is my output which is not in a single table, my code is creating multiple tables for every entry.

enter image description here

0

There are 0 best solutions below