chore: Update RGB control panel with Streamlit and MQTT implementation
Browse files
app.py
CHANGED
@@ -53,11 +53,17 @@ R = st.slider("Select the Red value:", min_value=0, max_value=max_value, value=0
|
|
53 |
G = st.slider("Select the Green value:", min_value=0, max_value=max_value, value=0)
|
54 |
B = st.slider("Select the Blue value:", min_value=0, max_value=max_value, value=0)
|
55 |
|
56 |
-
# Initialize session state for messages and lock state
|
57 |
if "messages" not in st.session_state:
|
58 |
st.session_state.messages = []
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Queue to hold sensor data
|
63 |
sensor_data_queue = queue.Queue()
|
@@ -69,7 +75,9 @@ def get_paho_client(
|
|
69 |
sensor_data_topic, hostname, username, password=None, port=8883, tls=True
|
70 |
):
|
71 |
|
72 |
-
client = mqtt.Client(
|
|
|
|
|
73 |
|
74 |
def on_message(client, userdata, msg):
|
75 |
sensor_data_queue.put(json.loads(msg.payload))
|
@@ -92,14 +100,24 @@ def get_paho_client(
|
|
92 |
|
93 |
|
94 |
def send_and_receive(client, command_topic, msg, queue_timeout=60):
|
95 |
-
|
|
|
|
|
96 |
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
98 |
sensor_data = sensor_data_queue.get(True, queue_timeout)
|
99 |
return sensor_data
|
|
|
|
|
|
|
100 |
|
101 |
|
102 |
-
#
|
103 |
def plot_spectra(sensor_data):
|
104 |
"""https://chatgpt.com/share/210d2fee-ca64-45a5-866e-e6df6e56bd1c"""
|
105 |
wavelengths = np.array([410, 440, 470, 510, 550, 583, 620, 670])
|
@@ -148,23 +166,28 @@ def plot_spectra(sensor_data):
|
|
148 |
for wavelength, intensity in zip(wavelengths, intensities):
|
149 |
ax.vlines(wavelength, 0, intensity, color="gray", linestyle="--", linewidth=1)
|
150 |
|
|
|
151 |
ax.set_xlim(wavelengths.min() - 10, wavelengths.max() + 10)
|
152 |
-
ax.set_ylim(
|
|
|
|
|
153 |
ax.set_xticks(wavelengths)
|
154 |
-
ax.set_xlabel("Wavelength (nm)")
|
155 |
-
ax.set_ylabel("Intensity")
|
156 |
-
ax.set_title("Spectral Intensity vs. Wavelength")
|
|
|
157 |
|
158 |
st.pyplot(fig)
|
159 |
|
160 |
|
161 |
# Publish button
|
162 |
-
if st.button("Send RGB Command", disabled=st.session_state.locked):
|
|
|
163 |
if not PICO_ID or not HIVEMQ_HOST or not HIVEMQ_USERNAME or not HIVEMQ_PASSWORD:
|
164 |
st.error("Please enter all required fields.")
|
165 |
else:
|
166 |
-
st.session_state.locked = True
|
167 |
-
st.
|
168 |
|
169 |
client = get_paho_client(
|
170 |
sensor_data_topic,
|
@@ -177,14 +200,14 @@ if st.button("Send RGB Command", disabled=st.session_state.locked):
|
|
177 |
|
178 |
command_msg = {"R": R, "G": G, "B": B}
|
179 |
sensor_data = send_and_receive(
|
180 |
-
client, command_topic, command_msg, queue_timeout=
|
181 |
)
|
182 |
|
183 |
-
st.session_state.locked = False
|
184 |
-
|
185 |
-
|
186 |
-
st.write("Sensor Data Received:", sensor_data)
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
|
53 |
G = st.slider("Select the Green value:", min_value=0, max_value=max_value, value=0)
|
54 |
B = st.slider("Select the Blue value:", min_value=0, max_value=max_value, value=0)
|
55 |
|
56 |
+
# # Initialize session state for messages and lock state
|
57 |
if "messages" not in st.session_state:
|
58 |
st.session_state.messages = []
|
59 |
+
|
60 |
+
# if "locked" not in st.session_state:
|
61 |
+
# st.session_state.locked = False
|
62 |
+
|
63 |
+
# if "command_history" not in st.session_state:
|
64 |
+
# st.session_state.command_history = []
|
65 |
+
# if "sensor_data_history" not in st.session_state:
|
66 |
+
# st.session_state.sensor_data_history = []
|
67 |
|
68 |
# Queue to hold sensor data
|
69 |
sensor_data_queue = queue.Queue()
|
|
|
75 |
sensor_data_topic, hostname, username, password=None, port=8883, tls=True
|
76 |
):
|
77 |
|
78 |
+
client = mqtt.Client(
|
79 |
+
mqtt.CallbackAPIVersion.VERSION2, protocol=mqtt.MQTTv5
|
80 |
+
) # create new instance
|
81 |
|
82 |
def on_message(client, userdata, msg):
|
83 |
sensor_data_queue.put(json.loads(msg.payload))
|
|
|
100 |
|
101 |
|
102 |
def send_and_receive(client, command_topic, msg, queue_timeout=60):
|
103 |
+
print("Sending command...")
|
104 |
+
result = client.publish(command_topic, json.dumps(msg), qos=2)
|
105 |
+
result.wait_for_publish() # Ensure the message is sent
|
106 |
|
107 |
+
if result.rc == mqtt.MQTT_ERR_SUCCESS:
|
108 |
+
print(f"Command sent: {msg} to topic {command_topic}")
|
109 |
+
else:
|
110 |
+
print(f"Failed to send command: {result.rc}")
|
111 |
+
|
112 |
+
try:
|
113 |
sensor_data = sensor_data_queue.get(True, queue_timeout)
|
114 |
return sensor_data
|
115 |
+
except queue.Empty:
|
116 |
+
st.error("No sensor data received within the timeout period.")
|
117 |
+
return None
|
118 |
|
119 |
|
120 |
+
# Helper function to plot discrete spectral sensor data
|
121 |
def plot_spectra(sensor_data):
|
122 |
"""https://chatgpt.com/share/210d2fee-ca64-45a5-866e-e6df6e56bd1c"""
|
123 |
wavelengths = np.array([410, 440, 470, 510, 550, 583, 620, 670])
|
|
|
166 |
for wavelength, intensity in zip(wavelengths, intensities):
|
167 |
ax.vlines(wavelength, 0, intensity, color="gray", linestyle="--", linewidth=1)
|
168 |
|
169 |
+
# Adjust limits and labels with larger font size
|
170 |
ax.set_xlim(wavelengths.min() - 10, wavelengths.max() + 10)
|
171 |
+
ax.set_ylim(
|
172 |
+
0, max(intensities) + 15
|
173 |
+
) # Ensure the lower y limit is 0 and add buffer
|
174 |
ax.set_xticks(wavelengths)
|
175 |
+
ax.set_xlabel("Wavelength (nm)", fontsize=14)
|
176 |
+
ax.set_ylabel("Intensity", fontsize=14)
|
177 |
+
ax.set_title("Spectral Intensity vs. Wavelength", fontsize=16)
|
178 |
+
ax.tick_params(axis="both", which="major", labelsize=12)
|
179 |
|
180 |
st.pyplot(fig)
|
181 |
|
182 |
|
183 |
# Publish button
|
184 |
+
# if st.button("Send RGB Command", disabled=st.session_state.locked):
|
185 |
+
if st.button("Send RGB Command"):
|
186 |
if not PICO_ID or not HIVEMQ_HOST or not HIVEMQ_USERNAME or not HIVEMQ_PASSWORD:
|
187 |
st.error("Please enter all required fields.")
|
188 |
else:
|
189 |
+
# st.session_state.locked = True
|
190 |
+
st.info("Please wait while the command is sent...")
|
191 |
|
192 |
client = get_paho_client(
|
193 |
sensor_data_topic,
|
|
|
200 |
|
201 |
command_msg = {"R": R, "G": G, "B": B}
|
202 |
sensor_data = send_and_receive(
|
203 |
+
client, command_topic, command_msg, queue_timeout=15
|
204 |
)
|
205 |
|
206 |
+
# st.session_state.locked = False
|
207 |
+
|
208 |
+
print("Waiting for sensor data...")
|
|
|
209 |
|
210 |
+
if sensor_data:
|
211 |
+
st.success("Command sent successfully!")
|
212 |
+
plot_spectra(sensor_data)
|
213 |
+
st.write("Sensor Data Received:", sensor_data)
|