Skip to content

Instantly share code, notes, and snippets.

@mkhorasani
Last active March 9, 2022 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhorasani/a546121e546b4d4ce59c03a24eaa4883 to your computer and use it in GitHub Desktop.
Save mkhorasani/a546121e546b4d4ce59c03a24eaa4883 to your computer and use it in GitHub Desktop.
import serial
import time
import streamlit as st
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime
import pandas as pd
arduino = serial.Serial(port='COM5', baudrate=9600, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS) #Change the COM port to whichever port your arduino is in
gauge_placeholder = st.empty()
chart_placeholder = st.empty()
def temp_gauge(temp,previous_temp,gauge_placeholder):
fig = go.Figure(go.Indicator(
domain = {'x': [0, 1], 'y': [0, 1]},
value = temp,
mode = "gauge+number+delta",
title = {'text': "Temperature (°C)"},
delta = {'reference': previous_temp},
gauge = {'axis': {'range': [0, 40]}}))
gauge_placeholder.write(fig)
def temp_chart(df,chart_placeholder):
fig = px.line(df, x="Time", y="Temperature (°C)", title='Temperature vs. time')
chart_placeholder.write(fig)
if arduino.isOpen() == False:
arduino.open()
i = 0
previous_temp = 0
temp_record = pd.DataFrame(data=[],columns=['Time','Temperature (°C)'])
while i < 500: #Change number of iterations to as many as you need
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
try:
temp = round(float(arduino.readline().decode().strip('\r\n')),1)
except:
pass
temp_record.loc[i,'Time'] = current_time
temp_record.loc[i,'Temperature (°C)'] = temp
temp_gauge(temp,previous_temp,gauge_placeholder)
temp_chart(temp_record,chart_placeholder)
time.sleep(1)
i += 1
previous_temp = temp
temp_record.to_csv('temperature_record.csv',index=False)
if arduino.isOpen() == True:
arduino.close()
@ACSiskin
Copy link

ACSiskin commented Jan 6, 2022

Hello there,
every time when I try to start it Its showing "NameError: name 'temp' is not defined", Can u tell me what im doing wrong ?
Best regards

@mkhorasani
Copy link
Author

Hi,

Seems like you're not receiving anything from the Arduino. Can you try commenting out the try and except block and just keep the 'temp = round(float....' to diagnose the issue. Also try printing the input of the Arduino to see if you're receiving anything.

Cheers.

@jhamzah
Copy link

jhamzah commented Mar 9, 2022

i am also get same problem..

streamlit run [FILE_NAME] [ARGUMENTS]
Traceback (most recent call last):
File "C:/Python310/Scripts/SCADA.py", line 45, in
temp_record.loc[i,'Temperature (°C)'] = temp
NameError: name 'temp' is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment