Spaces:
Sleeping
Sleeping
File size: 1,813 Bytes
830ab0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import streamlit as st # web development
import numpy as np # np mean, np random
import pandas as pd # read csv, df manipulation
import time # to simulate a real time data, time loop
import plotly.express as px # interactive charts
import paho.mqtt.client as mqtt
import json
import warnings
warnings.filterwarnings('ignore')
# df = pd.read_csv('buildingdata.csv')
st.set_page_config(
page_title = 'Real-Time Data Buliding 59',
page_icon = '✅',
layout = 'wide'
)
st.title("Buliding 59 Dashboard")
placeholder = st.empty()
broker_address = "localhost"
broker_port = 1883
topic = "sensor_data"
df = pd.DataFrame({"sa_temp":[60, 56, 66,69,70],"ma_temp":[30,40,50,60,65]})
def on_message(client, userdata, message):
global df
payload = json.loads(message.payload.decode())
sa_temp = payload["sa_temp"]
ma_temp = payload["ma_temp"]
len_df = len(df)
df.loc[len_df] = {"sa_temp": sa_temp, "ma_temp": ma_temp}
if len_df>5:
with placeholder.container():
fig_col1, fig_col2 = st.columns(2)
with fig_col1:
st.markdown("### Supply Air temperature")
fig1 = px.line(data_frame=df[len_df-5:len_df], y = 'sa_temp', x = df[len_df-5:len_df].index)
st.write(fig1)
with fig_col2:
st.markdown("### Mixed Air temperature")
fig2 = px.line(data_frame=df[len_df-5:len_df], y = 'ma_temp', x = df[len_df-5:len_df].index)
st.write(fig2)
st.markdown("### Detailed Data View")
st.dataframe(df[len_df-5:len_df])
# time.sleep(1)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
client.on_message = on_message
client.connect(broker_address, broker_port)
client.subscribe(topic)
client.loop_forever()
|