Luthira A
commited on
Commit
·
bcf4441
1
Parent(s):
4640057
Add MQTT configuration form to app.py and create requirements.txt
Browse files- app.py +13 -9
- requirements.txt +3 -0
app.py
CHANGED
|
@@ -8,15 +8,20 @@ import time
|
|
| 8 |
|
| 9 |
st.title("Real-Time Sensor Data Dashboard")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
message_queue = Queue()
|
| 18 |
|
| 19 |
-
|
| 20 |
def on_connect(client, userdata, flags, rc):
|
| 21 |
if rc == 0:
|
| 22 |
print("Connected to MQTT broker")
|
|
@@ -25,7 +30,6 @@ def on_connect(client, userdata, flags, rc):
|
|
| 25 |
print(f"Failed to connect, return code {rc}")
|
| 26 |
|
| 27 |
def on_message(client, userdata, msg):
|
| 28 |
-
|
| 29 |
try:
|
| 30 |
message = msg.payload.decode()
|
| 31 |
message_queue.put(message)
|
|
@@ -37,11 +41,11 @@ def start_mqtt_client():
|
|
| 37 |
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
| 38 |
client.on_connect = on_connect
|
| 39 |
client.on_message = on_message
|
| 40 |
-
client.tls_set()
|
| 41 |
|
| 42 |
try:
|
| 43 |
client.connect(MQTT_HOST, MQTT_PORT, keepalive=60)
|
| 44 |
-
client.loop_start()
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Failed to connect to MQTT broker: {e}")
|
| 47 |
|
|
|
|
| 8 |
|
| 9 |
st.title("Real-Time Sensor Data Dashboard")
|
| 10 |
|
| 11 |
+
# Form for MQTT Configuration
|
| 12 |
+
with st.form("mqtt_form"):
|
| 13 |
+
MQTT_HOST = st.text_input("Enter your MQTT host:", "b6bdb89571144b3d8e5ca4bbe666ddb5.s1.eu.hivemq.cloud")
|
| 14 |
+
MQTT_PORT = st.number_input("Enter the port number:", min_value=1, max_value=65535, value=8883)
|
| 15 |
+
MQTT_USERNAME = st.text_input("Enter your MQTT username:", "Luthiraa")
|
| 16 |
+
MQTT_PASSWORD = st.text_input("Enter your MQTT password:", "theboss1010", type="password")
|
| 17 |
+
MQTT_TOPIC = st.text_input("Enter your MQTT topic:", "sensors/bme680/data")
|
| 18 |
+
submit_button = st.form_submit_button(label="Submit")
|
| 19 |
+
|
| 20 |
+
if submit_button:
|
| 21 |
+
st.success("MQTT Configuration Submitted")
|
| 22 |
|
| 23 |
message_queue = Queue()
|
| 24 |
|
|
|
|
| 25 |
def on_connect(client, userdata, flags, rc):
|
| 26 |
if rc == 0:
|
| 27 |
print("Connected to MQTT broker")
|
|
|
|
| 30 |
print(f"Failed to connect, return code {rc}")
|
| 31 |
|
| 32 |
def on_message(client, userdata, msg):
|
|
|
|
| 33 |
try:
|
| 34 |
message = msg.payload.decode()
|
| 35 |
message_queue.put(message)
|
|
|
|
| 41 |
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
| 42 |
client.on_connect = on_connect
|
| 43 |
client.on_message = on_message
|
| 44 |
+
client.tls_set()
|
| 45 |
|
| 46 |
try:
|
| 47 |
client.connect(MQTT_HOST, MQTT_PORT, keepalive=60)
|
| 48 |
+
client.loop_start()
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Failed to connect to MQTT broker: {e}")
|
| 51 |
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
paho-mqtt
|
| 2 |
+
matplotlib
|
| 3 |
+
streamlit
|