Spaces:
Sleeping
Sleeping
Mistake in my previous code
Browse files
app.py
CHANGED
|
@@ -45,4 +45,43 @@ def connect_mqtt(hivemq_host, username, password):
|
|
| 45 |
client.tls_set() # Ensure secure connection with TLS
|
| 46 |
client.on_connect = on_connect
|
| 47 |
client.on_message = on_message
|
| 48 |
-
client.connect(MQTT_BROKER, MQTT_PORT,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
client.tls_set() # Ensure secure connection with TLS
|
| 46 |
client.on_connect = on_connect
|
| 47 |
client.on_message = on_message
|
| 48 |
+
client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
| 49 |
+
client.loop_start()
|
| 50 |
+
|
| 51 |
+
return on_connect(client, None, None, 0) # Trigger to check connection status
|
| 52 |
+
|
| 53 |
+
def update_graph():
|
| 54 |
+
if not data_buffer:
|
| 55 |
+
return px.line(title="Waiting for Data...")
|
| 56 |
+
|
| 57 |
+
df = pd.DataFrame(list(data_buffer))
|
| 58 |
+
return px.line(df, x='timestamp', y='weight', title="Weight Over Time")
|
| 59 |
+
|
| 60 |
+
# Build Gradio interface
|
| 61 |
+
with gr.Blocks() as app:
|
| 62 |
+
gr.Markdown("# Weight Monitor")
|
| 63 |
+
|
| 64 |
+
# Connect to MQTT section
|
| 65 |
+
gr.Markdown("## Connect to MQTT")
|
| 66 |
+
|
| 67 |
+
# Input fields for HiveMQ connection with initial values
|
| 68 |
+
with gr.Row():
|
| 69 |
+
hivemq_host = gr.Textbox(label="HiveMQ Host:", value="f74268785e954de0970c4bd66f31b0a1.s1.eu.hivemq.cloud")
|
| 70 |
+
mqtt_username = gr.Textbox(label="HiveMQ Username:", value="evnesher")
|
| 71 |
+
mqtt_password = gr.Textbox(label="HiveMQ Password:", value="Gfx8360", type="password")
|
| 72 |
+
|
| 73 |
+
# Connect button
|
| 74 |
+
connect_button = gr.Button("Connect")
|
| 75 |
+
|
| 76 |
+
# Connection status display
|
| 77 |
+
connection_status = gr.Textbox(label="Connection Status:", interactive=False)
|
| 78 |
+
|
| 79 |
+
# Define the button click action
|
| 80 |
+
connect_button.click(fn=connect_mqtt, inputs=[hivemq_host, mqtt_username, mqtt_password], outputs=connection_status)
|
| 81 |
+
|
| 82 |
+
# Graph display
|
| 83 |
+
plot = gr.Plot(update_graph)
|
| 84 |
+
gr.Button("Refresh").click(fn=update_graph, outputs=plot)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
app.launch()
|