Luthira A
commited on
Commit
·
4640057
1
Parent(s):
9059aca
updated app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import paho.mqtt.client as mqtt
|
| 3 |
+
import json
|
| 4 |
+
from queue import Queue
|
| 5 |
+
from threading import Thread
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
st.title("Real-Time Sensor Data Dashboard")
|
| 10 |
+
|
| 11 |
+
MQTT_HOST = "b6bdb89571144b3d8e5ca4bbe666ddb5.s1.eu.hivemq.cloud"
|
| 12 |
+
MQTT_PORT = 8883
|
| 13 |
+
MQTT_USERNAME = "Luthiraa"
|
| 14 |
+
MQTT_PASSWORD = "theboss1010"
|
| 15 |
+
MQTT_TOPIC = "sensors/bme680/data"
|
| 16 |
+
|
| 17 |
+
message_queue = Queue()
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def on_connect(client, userdata, flags, rc):
|
| 21 |
+
if rc == 0:
|
| 22 |
+
print("Connected to MQTT broker")
|
| 23 |
+
client.subscribe(MQTT_TOPIC)
|
| 24 |
+
else:
|
| 25 |
+
print(f"Failed to connect, return code {rc}")
|
| 26 |
+
|
| 27 |
+
def on_message(client, userdata, msg):
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
message = msg.payload.decode()
|
| 31 |
+
message_queue.put(message)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error decoding message: {e}")
|
| 34 |
+
|
| 35 |
+
def start_mqtt_client():
|
| 36 |
+
client = mqtt.Client()
|
| 37 |
+
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
| 38 |
+
client.on_connect = on_connect
|
| 39 |
+
client.on_message = on_message
|
| 40 |
+
client.tls_set()
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
client.connect(MQTT_HOST, MQTT_PORT, keepalive=60)
|
| 44 |
+
client.loop_start()
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Failed to connect to MQTT broker: {e}")
|
| 47 |
+
|
| 48 |
+
Thread(target=start_mqtt_client, daemon=True).start()
|
| 49 |
+
|
| 50 |
+
st.subheader("Live Sensor Data")
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
with col1:
|
| 53 |
+
st.markdown("### Temperature Over Time")
|
| 54 |
+
temperature_chart = st.line_chart([])
|
| 55 |
+
with col2:
|
| 56 |
+
st.markdown("### Humidity Over Time")
|
| 57 |
+
humidity_chart = st.line_chart([])
|
| 58 |
+
|
| 59 |
+
pressure_gauge = st.empty()
|
| 60 |
+
gas_gauge = st.empty()
|
| 61 |
+
|
| 62 |
+
sensor_data = pd.DataFrame(columns=["timestamp", "temperature", "humidity", "pressure", "gas"])
|
| 63 |
+
|
| 64 |
+
while True:
|
| 65 |
+
if not message_queue.empty():
|
| 66 |
+
message = message_queue.get()
|
| 67 |
+
try:
|
| 68 |
+
parsed_message = json.loads(message)
|
| 69 |
+
|
| 70 |
+
# Extract sensor values
|
| 71 |
+
temperature = parsed_message.get("temperature")
|
| 72 |
+
humidity = parsed_message.get("humidity")
|
| 73 |
+
pressure = parsed_message.get("pressure")
|
| 74 |
+
gas = parsed_message.get("gas")
|
| 75 |
+
timestamp = time.time()
|
| 76 |
+
|
| 77 |
+
# Add new data to the DataFrame
|
| 78 |
+
new_row = pd.DataFrame([{
|
| 79 |
+
"timestamp": timestamp,
|
| 80 |
+
"temperature": temperature,
|
| 81 |
+
"humidity": humidity,
|
| 82 |
+
"pressure": pressure,
|
| 83 |
+
"gas": gas,
|
| 84 |
+
}])
|
| 85 |
+
sensor_data = pd.concat([sensor_data, new_row], ignore_index=True)
|
| 86 |
+
temperature_chart.line_chart(sensor_data[["temperature"]])
|
| 87 |
+
humidity_chart.line_chart(sensor_data[["humidity"]])
|
| 88 |
+
pressure_gauge.metric(
|
| 89 |
+
label="Pressure (hPa)",
|
| 90 |
+
value=f"{pressure:.2f}" if pressure else "N/A",
|
| 91 |
+
)
|
| 92 |
+
gas_gauge.metric(
|
| 93 |
+
label="Gas (ohms)",
|
| 94 |
+
value=f"{gas:.2f}" if gas else "N/A",
|
| 95 |
+
)
|
| 96 |
+
except json.JSONDecodeError:
|
| 97 |
+
st.error("Failed to parse JSON payload")
|
| 98 |
+
|
| 99 |
+
time.sleep(0.1)
|