Spaces:
Sleeping
Sleeping
Added custom cred and stirring automation
Browse files
app.py
CHANGED
|
@@ -2,28 +2,58 @@ import streamlit as st
|
|
| 2 |
import paho.mqtt.client as mqtt
|
| 3 |
import json
|
| 4 |
import time
|
|
|
|
| 5 |
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Create a form to get the MQTT broker details
|
| 8 |
st.title("PioReactor MQTT Client")
|
| 9 |
-
with st.form("mqtt_form"):
|
| 10 |
-
hivemq_host = st.text_input("HiveMQ Host", "9bb69fc9bf6b4092abc4c92d76e602cd.s1.eu.hivemq.cloud", type="password")
|
| 11 |
-
hivemq_username = st.text_input("HiveMQ Username", "Website", type="password")
|
| 12 |
-
hivemq_password = st.text_input("HiveMQ Password", "Abcabcabc1", type="password")
|
| 13 |
-
hivemq_port = st.number_input("HiveMQ Port", min_value=1, max_value=65535, step=1, value=8883)
|
| 14 |
-
reactor = st.text_input("PioReactor", "pio1")
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
experiment = None
|
| 19 |
-
running = []
|
| 20 |
-
jobs = {
|
| 21 |
-
"temperature_automation": False,
|
| 22 |
-
"growth_rate_calculating": False,
|
| 23 |
-
"stirring": False,
|
| 24 |
-
"od_reading": False,
|
| 25 |
-
"led_automation": False
|
| 26 |
-
}
|
| 27 |
|
| 28 |
# Create the MQTT client
|
| 29 |
@st.cache_resource
|
|
@@ -78,37 +108,175 @@ if submit_button:
|
|
| 78 |
while experiment is None and (time.time() - start_time) < timeout:
|
| 79 |
time.sleep(1)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
client.loop_stop()
|
| 82 |
-
client.unsubscribe(f"pioreactor/{reactor}/worker")
|
| 83 |
|
| 84 |
-
if experiment is None:
|
| 85 |
st.error("No experiment assigned to the reactor.")
|
| 86 |
st.stop()
|
| 87 |
|
| 88 |
for run in running:
|
| 89 |
-
jobs[run] = True
|
| 90 |
-
|
| 91 |
-
if experiment is not None:
|
| 92 |
-
st.title(f"Experiment: {experiment}")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
cols = st.columns(5)
|
| 95 |
|
| 96 |
with cols[0]:
|
| 97 |
st.write("Temperature")
|
| 98 |
-
st.write(jobs["temperature_automation"])
|
| 99 |
|
| 100 |
with cols[1]:
|
| 101 |
st.write("Growth Rate")
|
| 102 |
-
st.write(jobs["growth_rate_calculating"])
|
| 103 |
|
| 104 |
with cols[2]:
|
| 105 |
st.write("Stirring")
|
| 106 |
-
st.write(jobs["stirring"])
|
| 107 |
|
| 108 |
with cols[3]:
|
| 109 |
st.write("OD Reading")
|
| 110 |
-
st.write(jobs["od_reading"])
|
| 111 |
|
| 112 |
with cols[4]:
|
| 113 |
st.write("LED")
|
| 114 |
-
st.write(jobs["led_automation"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
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
|
| 13 |
+
st.session_state["jobs"] = {
|
| 14 |
+
"temperature_automation": False,
|
| 15 |
+
"growth_rate_calculating": False,
|
| 16 |
+
"stirring": False,
|
| 17 |
+
"od_reading": False,
|
| 18 |
+
"led_automation": False
|
| 19 |
+
}
|
| 20 |
+
st.session_state["client"] = None
|
| 21 |
+
|
| 22 |
# Create a form to get the MQTT broker details
|
| 23 |
st.title("PioReactor MQTT Client")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Create a button to choose whether or not to use custom credentials
|
| 26 |
+
use_custom_credentials = st.checkbox("Use Custom Credentials")
|
| 27 |
+
|
| 28 |
+
if use_custom_credentials:
|
| 29 |
+
with st.form("mqtt_form"):
|
| 30 |
+
# hivemq_host = st.text_input("HiveMQ Host", type="password")
|
| 31 |
+
# hivemq_username = st.text_input("HiveMQ Username", type="password")
|
| 32 |
+
# hivemq_password = st.text_input("HiveMQ Password", type="password")
|
| 33 |
+
# hivemq_port = st.number_input("HiveMQ Port", min_value=1, max_value=65535, step=1, value=8883)
|
| 34 |
+
# reactor = st.text_input("PioReactor")
|
| 35 |
+
|
| 36 |
+
hivemq_host = st.text_input("HiveMQ Host", "9bb69fc9bf6b4092abc4c92d76e602cd.s1.eu.hivemq.cloud",type="password")
|
| 37 |
+
hivemq_username = st.text_input("HiveMQ Username", "Website",type="password")
|
| 38 |
+
hivemq_password = st.text_input("HiveMQ Password", "Abcabcabc1",type="password")
|
| 39 |
+
hivemq_port = st.number_input("HiveMQ Port", min_value=1, max_value=65535, step=1, value=8883)
|
| 40 |
+
reactor = st.text_input("PioReactor", "pio1")
|
| 41 |
+
|
| 42 |
+
submit_button = st.form_submit_button("Connect")
|
| 43 |
+
else:
|
| 44 |
+
hivemq_host = os.getenv("HIVEMQ_HOST")
|
| 45 |
+
hivemq_username = os.getenv("HIVEMQ_USERNAME")
|
| 46 |
+
hivemq_password = os.getenv("HIVEMQ_PASSWORD")
|
| 47 |
+
hivemq_port = os.getenv("HIVEMQ_PORT")
|
| 48 |
+
reactor = os.getenv("REACTOR")
|
| 49 |
+
|
| 50 |
+
# button to submit the form
|
| 51 |
+
if st.session_state["experiment"] is None:
|
| 52 |
+
submit_button = st.button("Connect")
|
| 53 |
+
else:
|
| 54 |
+
submit_button = st.button("Reconnect")
|
| 55 |
+
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Create the MQTT client
|
| 59 |
@st.cache_resource
|
|
|
|
| 108 |
while experiment is None and (time.time() - start_time) < timeout:
|
| 109 |
time.sleep(1)
|
| 110 |
|
| 111 |
+
st.session_state["experiment"] = experiment
|
| 112 |
+
|
| 113 |
+
# Unsubscribe from the worker topic
|
| 114 |
+
# client.unsubscribe(f"pioreactor/{reactor}/worker")
|
| 115 |
+
# client.message_callback_remove(f"pioreactor/{reactor}/worker")
|
| 116 |
client.loop_stop()
|
|
|
|
| 117 |
|
| 118 |
+
if st.session_state["experiment"] is None:
|
| 119 |
st.error("No experiment assigned to the reactor.")
|
| 120 |
st.stop()
|
| 121 |
|
| 122 |
for run in running:
|
| 123 |
+
st.session_state["jobs"][run] = True
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Save the client to the session state
|
| 126 |
+
st.session_state["client"] = client
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Display the experiment and running jobs if available
|
| 130 |
+
if st.session_state["experiment"] is not None:
|
| 131 |
+
st.title(f"Experiment: {st.session_state['experiment']}")
|
| 132 |
+
|
| 133 |
+
st.header("Running Jobs")
|
| 134 |
+
|
| 135 |
cols = st.columns(5)
|
| 136 |
|
| 137 |
with cols[0]:
|
| 138 |
st.write("Temperature")
|
| 139 |
+
st.write(st.session_state["jobs"]["temperature_automation"])
|
| 140 |
|
| 141 |
with cols[1]:
|
| 142 |
st.write("Growth Rate")
|
| 143 |
+
st.write(st.session_state["jobs"]["growth_rate_calculating"])
|
| 144 |
|
| 145 |
with cols[2]:
|
| 146 |
st.write("Stirring")
|
| 147 |
+
st.write(st.session_state["jobs"]["stirring"])
|
| 148 |
|
| 149 |
with cols[3]:
|
| 150 |
st.write("OD Reading")
|
| 151 |
+
st.write(st.session_state["jobs"]["od_reading"])
|
| 152 |
|
| 153 |
with cols[4]:
|
| 154 |
st.write("LED")
|
| 155 |
+
st.write(st.session_state["jobs"]["led_automation"])
|
| 156 |
+
|
| 157 |
+
st.divider()
|
| 158 |
+
|
| 159 |
+
# Display the job details
|
| 160 |
+
st.header("Stirring")
|
| 161 |
+
if st.session_state["jobs"]["stirring"]:
|
| 162 |
+
with st.form("stirring_form"):
|
| 163 |
+
if "stirring_speed" not in st.session_state:
|
| 164 |
+
st.write("Current Stirring Speed Unknown")
|
| 165 |
+
else:
|
| 166 |
+
st.write(f"Current Stirring Speed: {st.session_state['stirring_speed']} RPM")
|
| 167 |
+
stirring_speed = st.slider("Stirring Speed", min_value=100, max_value=3000, step=50, value=st.session_state.get("stirring_speed", 500))
|
| 168 |
+
|
| 169 |
+
cols = st.columns(2)
|
| 170 |
+
with cols[0]:
|
| 171 |
+
stirring = st.form_submit_button("Update Stirring Speed")
|
| 172 |
+
with cols[1]:
|
| 173 |
+
stop_stirring = st.form_submit_button("Stop Stirring")
|
| 174 |
+
else:
|
| 175 |
+
with st.form("stirring_form"):
|
| 176 |
+
stirring_speed = st.slider("Stirring Speed", min_value=100, max_value=3000, step=50, value=500)
|
| 177 |
+
stirring = st.form_submit_button("Start Stirring")
|
| 178 |
+
|
| 179 |
+
stop_stirring = False
|
| 180 |
+
|
| 181 |
+
if stirring:
|
| 182 |
+
if st.session_state["jobs"]["stirring"]:
|
| 183 |
+
payload = {
|
| 184 |
+
"command": "update_stirring_rpm",
|
| 185 |
+
"reactor": reactor,
|
| 186 |
+
"experiment": st.session_state["experiment"],
|
| 187 |
+
"rpm": stirring_speed
|
| 188 |
+
}
|
| 189 |
+
else:
|
| 190 |
+
payload = {
|
| 191 |
+
"command": "start_stirring",
|
| 192 |
+
"experiment": st.session_state["experiment"],
|
| 193 |
+
"reactor": reactor,
|
| 194 |
+
"rpm": stirring_speed
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
payload_str = json.dumps(payload)
|
| 198 |
+
|
| 199 |
+
# Start looping
|
| 200 |
+
st.session_state["client"].loop_start()
|
| 201 |
+
st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
|
| 202 |
+
|
| 203 |
+
time.sleep(1)
|
| 204 |
+
|
| 205 |
+
# Check if the stirring job is running
|
| 206 |
+
payload = {
|
| 207 |
+
"command": "get_worker",
|
| 208 |
+
"reactor": reactor
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
experiment = None
|
| 212 |
+
running = []
|
| 213 |
+
|
| 214 |
+
st.session_state["client"].subscribe(f"pioreactor/{reactor}/worker")
|
| 215 |
+
st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
|
| 216 |
+
|
| 217 |
+
payload_str = json.dumps(payload)
|
| 218 |
+
st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
|
| 219 |
+
|
| 220 |
+
timeout = 10
|
| 221 |
+
start_time = time.time()
|
| 222 |
+
|
| 223 |
+
while experiment is None and (time.time() - start_time) < timeout:
|
| 224 |
+
time.sleep(1)
|
| 225 |
+
|
| 226 |
+
st.session_state["client"].loop_stop()
|
| 227 |
+
|
| 228 |
+
if "stirring" in running and st.session_state["jobs"]["stirring"]:
|
| 229 |
+
st.success("Stirring speed updated successfully!")
|
| 230 |
+
st.session_state["stirring_speed"] = stirring_speed
|
| 231 |
+
elif "stirring" in running:
|
| 232 |
+
st.success("Stirring started successfully!")
|
| 233 |
+
st.session_state["jobs"]["stirring"] = True
|
| 234 |
+
st.session_state["stirring_speed"] = stirring_speed
|
| 235 |
+
else:
|
| 236 |
+
st.error("Failed to update stirring speed.")
|
| 237 |
+
|
| 238 |
+
time.sleep(3)
|
| 239 |
+
st.rerun()
|
| 240 |
+
|
| 241 |
+
if stop_stirring:
|
| 242 |
+
payload = {
|
| 243 |
+
"command": "stop_stirring",
|
| 244 |
+
"reactor": reactor,
|
| 245 |
+
"experiment": st.session_state["experiment"]
|
| 246 |
+
}
|
| 247 |
+
payload_str = json.dumps(payload)
|
| 248 |
+
|
| 249 |
+
st.session_state["client"].loop_start()
|
| 250 |
+
st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
|
| 251 |
+
st.session_state["client"].loop_stop()
|
| 252 |
+
|
| 253 |
+
# Check if the stirring job is running
|
| 254 |
+
payload = {
|
| 255 |
+
"command": "get_worker",
|
| 256 |
+
"reactor": reactor
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
st.session_state["client"].subscribe(f"pioreactor/{reactor}/worker")
|
| 260 |
+
st.session_state["client"].message_callback_add(f"pioreactor/{reactor}/worker", on_message_worker)
|
| 261 |
+
|
| 262 |
+
experiment = None
|
| 263 |
+
running = []
|
| 264 |
+
|
| 265 |
+
payload_str = json.dumps(payload)
|
| 266 |
+
st.session_state["client"].publish("pioreactor/control", payload_str, qos=1)
|
| 267 |
+
|
| 268 |
+
timeout = 10
|
| 269 |
+
start_time = time.time()
|
| 270 |
+
|
| 271 |
+
while experiment is None and (time.time() - start_time) < timeout:
|
| 272 |
+
time.sleep(1)
|
| 273 |
+
|
| 274 |
+
if "stirring" not in running:
|
| 275 |
+
st.success("Stirring stopped successfully!")
|
| 276 |
+
st.session_state["stirring_speed"] = None
|
| 277 |
+
st.session_state["jobs"]["stirring"] = False
|
| 278 |
+
else:
|
| 279 |
+
st.error("Failed to stop stirring.")
|
| 280 |
+
|
| 281 |
+
time.sleep(3)
|
| 282 |
+
st.rerun()
|