Spaces:
Sleeping
Sleeping
added temp graph which auto reloads after set time, improvements to the handling of data and differnt time intervals needs to be added
Browse files
app.py
CHANGED
|
@@ -3,10 +3,18 @@ import paho.mqtt.client as mqtt
|
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
experiment = None
|
| 9 |
running = []
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
if "experiment" not in st.session_state:
|
| 12 |
st.session_state["experiment"] = None
|
|
@@ -56,7 +64,7 @@ else:
|
|
| 56 |
|
| 57 |
|
| 58 |
# Create the MQTT client
|
| 59 |
-
@st.cache_resource
|
| 60 |
def create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password):
|
| 61 |
client = mqtt.Client()
|
| 62 |
|
|
@@ -83,6 +91,40 @@ def on_message_worker(client, userdata, message):
|
|
| 83 |
experiment = data.get("experiment", None)
|
| 84 |
running = data.get("running", [])
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if submit_button:
|
| 87 |
client = create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password)
|
| 88 |
|
|
@@ -315,7 +357,7 @@ if st.session_state["experiment"] is not None:
|
|
| 315 |
if temp_auto == "Heat To Temp":
|
| 316 |
if st.session_state.get("temp", None) is not None:
|
| 317 |
st.write(f"Current Temperature: {st.session_state['temp']} °C")
|
| 318 |
-
temp = st.slider("Temperature", min_value=0
|
| 319 |
cols = st.columns(2)
|
| 320 |
with cols[0]:
|
| 321 |
temp_submit = st.form_submit_button("Update Temperature Automation")
|
|
@@ -329,10 +371,11 @@ if st.session_state["experiment"] is not None:
|
|
| 329 |
temp_stop = st.form_submit_button("Stop Temperature Automation")
|
| 330 |
|
| 331 |
else:
|
|
|
|
| 332 |
temp_auto = st.selectbox("Temperature Automation", ["Record Temp Only", "Heat To Temp"], placeholder="Select an option")
|
| 333 |
with st.form("temperature_form"):
|
| 334 |
if temp_auto == "Heat To Temp":
|
| 335 |
-
temp = st.slider("Temperature", min_value=0
|
| 336 |
temp_submit = st.form_submit_button("Start Temperature Automation")
|
| 337 |
temp_stop = False
|
| 338 |
elif temp_auto == "Record Temp Only":
|
|
@@ -475,4 +518,60 @@ if st.session_state["experiment"] is not None:
|
|
| 475 |
time.sleep(3)
|
| 476 |
st.rerun()
|
| 477 |
|
| 478 |
-
st.divider()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import time
|
| 5 |
import os
|
| 6 |
+
import asyncio
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import pytz
|
| 11 |
|
| 12 |
|
| 13 |
experiment = None
|
| 14 |
running = []
|
| 15 |
+
temp_graph = None
|
| 16 |
+
|
| 17 |
+
REQUEST_INTERVAL = 60
|
| 18 |
|
| 19 |
if "experiment" not in st.session_state:
|
| 20 |
st.session_state["experiment"] = None
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
# Create the MQTT client
|
| 67 |
+
# @st.cache_resource
|
| 68 |
def create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password):
|
| 69 |
client = mqtt.Client()
|
| 70 |
|
|
|
|
| 91 |
experiment = data.get("experiment", None)
|
| 92 |
running = data.get("running", [])
|
| 93 |
|
| 94 |
+
def on_message(client, userdata, message):
|
| 95 |
+
payload = message.payload.decode("utf-8")
|
| 96 |
+
data = json.loads(payload)
|
| 97 |
+
global temp_graph
|
| 98 |
+
temp_graph = data.get("data", None)
|
| 99 |
+
|
| 100 |
+
async def publish_temperature_request(client, experiment, filter_mod_N, lookback, reactor):
|
| 101 |
+
"""Publish a temperature request to the request topic."""
|
| 102 |
+
payload = {
|
| 103 |
+
"command": "get_temperature_readings",
|
| 104 |
+
"experiment": experiment,
|
| 105 |
+
"filter_mod": filter_mod_N,
|
| 106 |
+
"lookback": lookback,
|
| 107 |
+
"reactor": reactor
|
| 108 |
+
}
|
| 109 |
+
payload_str = json.dumps(payload)
|
| 110 |
+
client.publish("pioreactor/control", payload_str)
|
| 111 |
+
|
| 112 |
+
async def periodic_temp_reading(client, reactor, experiment):
|
| 113 |
+
try:
|
| 114 |
+
await publish_temperature_request(client, experiment, 1, 100000, reactor)
|
| 115 |
+
except asyncio.CancelledError:
|
| 116 |
+
pass
|
| 117 |
+
|
| 118 |
+
async def run_mqtt_client(client):
|
| 119 |
+
client.subscribe(f"pioreactor/{reactor}/temp_reading")
|
| 120 |
+
client.subscribe(f"pioreactor/{reactor}/od_reading")
|
| 121 |
+
client.subscribe(f"pioreactor/{reactor}/growth_rate")
|
| 122 |
+
|
| 123 |
+
client.on_message = on_message
|
| 124 |
+
|
| 125 |
+
client.loop_start()
|
| 126 |
+
await periodic_temp_reading(client, reactor, st.session_state["experiment"])
|
| 127 |
+
|
| 128 |
if submit_button:
|
| 129 |
client = create_mqtt_client(hivemq_host, hivemq_port, hivemq_username, hivemq_password)
|
| 130 |
|
|
|
|
| 357 |
if temp_auto == "Heat To Temp":
|
| 358 |
if st.session_state.get("temp", None) is not None:
|
| 359 |
st.write(f"Current Temperature: {st.session_state['temp']} °C")
|
| 360 |
+
temp = st.slider("Temperature", min_value=0, max_value=60, step=1, value=st.session_state["temp"] if st.session_state["temp"] is not None else 30)
|
| 361 |
cols = st.columns(2)
|
| 362 |
with cols[0]:
|
| 363 |
temp_submit = st.form_submit_button("Update Temperature Automation")
|
|
|
|
| 371 |
temp_stop = st.form_submit_button("Stop Temperature Automation")
|
| 372 |
|
| 373 |
else:
|
| 374 |
+
temp = None
|
| 375 |
temp_auto = st.selectbox("Temperature Automation", ["Record Temp Only", "Heat To Temp"], placeholder="Select an option")
|
| 376 |
with st.form("temperature_form"):
|
| 377 |
if temp_auto == "Heat To Temp":
|
| 378 |
+
temp = st.slider("Temperature", min_value=0, max_value=60, step=1, value=30)
|
| 379 |
temp_submit = st.form_submit_button("Start Temperature Automation")
|
| 380 |
temp_stop = False
|
| 381 |
elif temp_auto == "Record Temp Only":
|
|
|
|
| 518 |
time.sleep(3)
|
| 519 |
st.rerun()
|
| 520 |
|
| 521 |
+
st.divider()
|
| 522 |
+
|
| 523 |
+
# Display the temperature graph
|
| 524 |
+
# Using asyncio to run the loop in the background and periodically update the graph
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
st.header("Graphs")
|
| 531 |
+
|
| 532 |
+
payload = {
|
| 533 |
+
"command": "get_temperature_readings",
|
| 534 |
+
"experiment": st.session_state["experiment"],
|
| 535 |
+
"filter_mod": 1,
|
| 536 |
+
"lookback": 100,
|
| 537 |
+
"reactor": reactor
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
st.session_state["client"].loop_start()
|
| 541 |
+
st.session_state["client"].on_message = on_message
|
| 542 |
+
payload_str = json.dumps(payload)
|
| 543 |
+
st.session_state["client"].subscribe(f"pioreactor/{reactor}/temperature")
|
| 544 |
+
|
| 545 |
+
temp_graph_actual = []
|
| 546 |
+
|
| 547 |
+
st.subheader("Temperature Graph")
|
| 548 |
+
placeholder = st.empty()
|
| 549 |
+
|
| 550 |
+
while True:
|
| 551 |
+
temp_graph = None
|
| 552 |
+
|
| 553 |
+
st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
|
| 554 |
+
|
| 555 |
+
timeout = 30
|
| 556 |
+
start_time = time.time()
|
| 557 |
+
|
| 558 |
+
while temp_graph is None and (time.time() - start_time) < timeout:
|
| 559 |
+
time.sleep(1)
|
| 560 |
+
|
| 561 |
+
temp_graph = temp_graph[0]
|
| 562 |
+
|
| 563 |
+
for temp in temp_graph:
|
| 564 |
+
utc_time = datetime.strptime(temp["x"], "%Y-%m-%dT%H:%M:%S.%fZ")
|
| 565 |
+
local_tz = pytz.timezone("America/New_York")
|
| 566 |
+
utc_time = utc_time.replace(tzinfo=pytz.utc)
|
| 567 |
+
local_time = utc_time.astimezone(local_tz)
|
| 568 |
+
temp["x"] = local_time.strftime("%Y-%m-%d %H:%M:%S")
|
| 569 |
+
|
| 570 |
+
df = pd.DataFrame(temp_graph)
|
| 571 |
+
df = df.set_index("x")
|
| 572 |
+
|
| 573 |
+
placeholder.line_chart(df, x_label="Time", y_label="Temperature (°C)")
|
| 574 |
+
|
| 575 |
+
time.sleep(REQUEST_INTERVAL)
|
| 576 |
+
|
| 577 |
+
|