Spaces:
Sleeping
Sleeping
Update app.py (#2)
Browse files- Update app.py (260a33a272fe3b40b98bc22cba2cbed314232980)
app.py
CHANGED
|
@@ -1,96 +1,64 @@
|
|
|
|
|
| 1 |
import serial
|
| 2 |
-
import serial.tools.list_ports
|
| 3 |
import time
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import threading
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
return None
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if 'Arduino' in port.description: # Matching the description of Arduino devices
|
| 26 |
-
print(f"Automatically selected port: {port.device} ({port.description})")
|
| 27 |
-
return port.device
|
| 28 |
-
|
| 29 |
-
# If no Arduino-like device found, return None
|
| 30 |
-
print("No Arduino device found.")
|
| 31 |
-
return None
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
com_port = get_serial_port()
|
| 35 |
-
if com_port:
|
| 36 |
-
try:
|
| 37 |
-
ser = serial.Serial(com_port, 9600) # Open serial port with the selected COM port
|
| 38 |
-
time.sleep(2) # Wait for the serial connection to initialize
|
| 39 |
-
print(f"Connected to {com_port}")
|
| 40 |
-
except serial.SerialException as e:
|
| 41 |
-
print(f"Error opening serial port: {e}")
|
| 42 |
-
st.error("Error opening serial port.")
|
| 43 |
-
else:
|
| 44 |
-
st.error("Failed to detect Arduino serial port.")
|
| 45 |
-
exit()
|
| 46 |
-
|
| 47 |
-
# Function to update the bus information based on received serial data
|
| 48 |
def display_bus_info():
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
print("Exiting...")
|
| 78 |
-
|
| 79 |
-
# Start the bus information display in a separate thread
|
| 80 |
-
bus_info_thread = threading.Thread(target=display_bus_info)
|
| 81 |
-
bus_info_thread.daemon = True
|
| 82 |
-
bus_info_thread.start()
|
| 83 |
|
| 84 |
# Streamlit Web Interface
|
| 85 |
st.title('Bus System Status')
|
| 86 |
|
| 87 |
-
# Display the bus information
|
| 88 |
col1, col2 = st.columns(2)
|
| 89 |
-
|
| 90 |
with col1:
|
| 91 |
st.subheader('Bus Name')
|
| 92 |
bus_name = st.empty()
|
| 93 |
-
|
| 94 |
with col2:
|
| 95 |
st.subheader('Seat Status')
|
| 96 |
seat_status = st.empty()
|
|
@@ -107,6 +75,7 @@ buzzer_status_display = st.empty()
|
|
| 107 |
|
| 108 |
# Update the information on the web interface every second
|
| 109 |
while True:
|
|
|
|
| 110 |
bus_name.text(bus_info['bus_name'])
|
| 111 |
seat_status.text(bus_info['seat_status'])
|
| 112 |
distance_display.text(f"Distance: {bus_info['distance']} cm")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import serial
|
|
|
|
| 3 |
import time
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Function to initialize serial connection
|
| 6 |
+
def initialize_serial_connection():
|
| 7 |
+
com_port = predict_serial_port(collect_serial_ports()) # Get the correct port from the AI model
|
| 8 |
+
if com_port:
|
| 9 |
+
try:
|
| 10 |
+
ser = serial.Serial(com_port, 9600)
|
| 11 |
+
time.sleep(2) # Wait for the connection to initialize
|
| 12 |
+
print(f"Connected to {com_port}")
|
| 13 |
+
return ser
|
| 14 |
+
except serial.SerialException as e:
|
| 15 |
+
st.error(f"Error opening serial port: {e}")
|
| 16 |
+
return None
|
| 17 |
+
else:
|
| 18 |
+
st.error("No Arduino device found.")
|
| 19 |
return None
|
| 20 |
|
| 21 |
+
# Initialize serial connection
|
| 22 |
+
ser = initialize_serial_connection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Function to update bus information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def display_bus_info():
|
| 26 |
+
bus_info = {
|
| 27 |
+
'bus_name': 'N/A',
|
| 28 |
+
'seat_status': 'N/A',
|
| 29 |
+
'distance': 'N/A',
|
| 30 |
+
'led_status': 'OFF',
|
| 31 |
+
'buzzer_status': 'OFF',
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
while True:
|
| 35 |
+
if ser.in_waiting > 0:
|
| 36 |
+
# Read data from serial port
|
| 37 |
+
data = ser.readline().decode('utf-8', errors='ignore').strip()
|
| 38 |
+
|
| 39 |
+
# Process the incoming data
|
| 40 |
+
if "Bus Name:" in data:
|
| 41 |
+
bus_info['bus_name'] = data.split("Bus Name:")[1].strip()
|
| 42 |
+
elif "Seat Status:" in data:
|
| 43 |
+
bus_info['seat_status'] = data.split("Seat Status:")[1].strip()
|
| 44 |
+
elif "Distance:" in data:
|
| 45 |
+
bus_info['distance'] = data.split("Distance:")[1].strip()
|
| 46 |
+
elif "LED:" in data:
|
| 47 |
+
bus_info['led_status'] = data.split("LED:")[1].strip()
|
| 48 |
+
elif "Buzzer:" in data:
|
| 49 |
+
bus_info['buzzer_status'] = data.split("Buzzer:")[1].strip()
|
| 50 |
+
|
| 51 |
+
time.sleep(1) # Delay to avoid flooding the interface
|
| 52 |
+
|
| 53 |
+
return bus_info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Streamlit Web Interface
|
| 56 |
st.title('Bus System Status')
|
| 57 |
|
|
|
|
| 58 |
col1, col2 = st.columns(2)
|
|
|
|
| 59 |
with col1:
|
| 60 |
st.subheader('Bus Name')
|
| 61 |
bus_name = st.empty()
|
|
|
|
| 62 |
with col2:
|
| 63 |
st.subheader('Seat Status')
|
| 64 |
seat_status = st.empty()
|
|
|
|
| 75 |
|
| 76 |
# Update the information on the web interface every second
|
| 77 |
while True:
|
| 78 |
+
bus_info = display_bus_info()
|
| 79 |
bus_name.text(bus_info['bus_name'])
|
| 80 |
seat_status.text(bus_info['seat_status'])
|
| 81 |
distance_display.text(f"Distance: {bus_info['distance']} cm")
|