|
"""Based on |
|
https://github.com/AccelerationConsortium/ac-training-lab/blob/main/src/ac_training_lab/picow/digital-pipette/scripts/streamlit_webapp.py |
|
|
|
permalink: |
|
https://github.com/AccelerationConsortium/ac-training-lab/blob/230c72f3d9d6e8a5d0b9cece044515dfd386acdc/src/ac_training_lab/picow/digital-pipette/scripts/streamlit_webapp.py] |
|
|
|
Streamlit was preferred over the gradio implementation |
|
""" |
|
|
|
import json |
|
|
|
import paho.mqtt.client as mqtt |
|
import streamlit as st |
|
|
|
|
|
st.title("Digital Pipette Control Panel") |
|
|
|
|
|
HIVEMQ_HOST = st.text_input("Enter your HiveMQ host:", "248cc294c37642359297f75b7b023374.s2.eu.hivemq.cloud", type="password") |
|
HIVEMQ_USERNAME = st.text_input("Enter your HiveMQ username:", "sgbaird") |
|
HIVEMQ_PASSWORD = st.text_input("Enter your HiveMQ password:", "D.Pq5gYtejYbU#L", type="password") |
|
PORT = st.number_input( |
|
"Enter the port number:", min_value=1, max_value=65535, value=8883 |
|
) |
|
|
|
|
|
pico_id = st.text_input("Enter your Pico ID:", "test-pipette", type="password") |
|
|
|
|
|
position = st.slider( |
|
"Select the position value:", min_value=1.1, max_value=1.9, value=1.5 |
|
) |
|
|
|
|
|
|
|
@st.cache_resource |
|
def get_paho_client(hostname, username, password=None, port=8883, tls=True): |
|
|
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv5) |
|
|
|
|
|
def on_connect(client, userdata, flags, rc, properties=None): |
|
if rc != 0: |
|
print("Connected with result code " + str(rc)) |
|
|
|
client.on_connect = on_connect |
|
|
|
|
|
if tls: |
|
client.tls_set() |
|
|
|
client.username_pw_set(username, password) |
|
|
|
client.connect(hostname, port) |
|
client.loop_start() |
|
|
|
return client |
|
|
|
|
|
def send_command(client, pico_id, position): |
|
|
|
command_topic = f"digital-pipette/picow/{pico_id}/L16-R" |
|
|
|
|
|
command = {"position": position} |
|
|
|
try: |
|
result = client.publish(command_topic, json.dumps(command), qos=1) |
|
result.wait_for_publish() |
|
if result.rc == mqtt.MQTT_ERR_SUCCESS: |
|
return f"Command sent: {command} to topic {command_topic}" |
|
else: |
|
return f"Failed to send command: {result.rc}" |
|
except Exception as e: |
|
return f"An error occurred: {e}" |
|
|
|
|
|
|
|
if st.button("Send Command"): |
|
if not pico_id or not HIVEMQ_HOST or not HIVEMQ_USERNAME or not HIVEMQ_PASSWORD: |
|
st.error("Please enter all required fields.") |
|
else: |
|
client = get_paho_client( |
|
HIVEMQ_HOST, |
|
HIVEMQ_USERNAME, |
|
password=HIVEMQ_PASSWORD, |
|
port=int(PORT), |
|
tls=True, |
|
) |
|
success_msg = send_command(client, pico_id, position) |
|
st.success(success_msg) |
|
|