Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,115 @@
|
|
| 1 |
-
import serial
|
| 2 |
-
import serial.tools.list_ports
|
| 3 |
-
import time
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import threading
|
| 6 |
-
|
| 7 |
-
# Global variables to store bus information and hardware statuses
|
| 8 |
-
bus_info = {
|
| 9 |
-
'bus_name': 'N/A',
|
| 10 |
-
'seat_status': 'N/A',
|
| 11 |
-
'distance': 'N/A',
|
| 12 |
-
'led_status': 'OFF',
|
| 13 |
-
'buzzer_status': 'OFF',
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
# Function to identify the correct COM port
|
| 17 |
-
def get_serial_port():
|
| 18 |
-
ports = serial.tools.list_ports.comports()
|
| 19 |
-
if not ports:
|
| 20 |
-
print("No available ports found.")
|
| 21 |
-
return None
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
st.subheader('
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import serial
|
| 2 |
+
import serial.tools.list_ports
|
| 3 |
+
import time
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import threading
|
| 6 |
+
|
| 7 |
+
# Global variables to store bus information and hardware statuses
|
| 8 |
+
bus_info = {
|
| 9 |
+
'bus_name': 'N/A',
|
| 10 |
+
'seat_status': 'N/A',
|
| 11 |
+
'distance': 'N/A',
|
| 12 |
+
'led_status': 'OFF',
|
| 13 |
+
'buzzer_status': 'OFF',
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Function to identify the correct COM port (Arduino-like device) automatically
|
| 17 |
+
def get_serial_port():
|
| 18 |
+
ports = serial.tools.list_ports.comports()
|
| 19 |
+
if not ports:
|
| 20 |
+
print("No available ports found.")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
# Search for Arduino-like device based on description
|
| 24 |
+
for port in ports:
|
| 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 |
+
# Open serial connection to Arduino (use the correct COM port)
|
| 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 |
+
global bus_info
|
| 50 |
+
try:
|
| 51 |
+
while True:
|
| 52 |
+
if ser.in_waiting > 0:
|
| 53 |
+
# Read data from serial port
|
| 54 |
+
data = ser.readline().decode('utf-8', errors='ignore').strip() # Decode and remove extra spaces
|
| 55 |
+
|
| 56 |
+
# Debug print to see the raw serial data
|
| 57 |
+
print(f"Raw Data: {data}")
|
| 58 |
+
|
| 59 |
+
if "Bus Name:" in data:
|
| 60 |
+
bus_info['bus_name'] = data.split("Bus Name:")[1].strip()
|
| 61 |
+
|
| 62 |
+
elif "Seat Status:" in data:
|
| 63 |
+
bus_info['seat_status'] = data.split("Seat Status:")[1].strip()
|
| 64 |
+
|
| 65 |
+
elif "Distance:" in data:
|
| 66 |
+
bus_info['distance'] = data.split("Distance:")[1].strip()
|
| 67 |
+
|
| 68 |
+
elif "LED:" in data:
|
| 69 |
+
bus_info['led_status'] = data.split("LED:")[1].strip()
|
| 70 |
+
|
| 71 |
+
elif "Buzzer:" in data:
|
| 72 |
+
bus_info['buzzer_status'] = data.split("Buzzer:")[1].strip()
|
| 73 |
+
|
| 74 |
+
time.sleep(1) # Small delay to prevent flooding the terminal
|
| 75 |
+
|
| 76 |
+
except KeyboardInterrupt:
|
| 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()
|
| 97 |
+
|
| 98 |
+
st.subheader('Distance to Bus')
|
| 99 |
+
distance_display = st.empty()
|
| 100 |
+
|
| 101 |
+
# Display LED and Buzzer status
|
| 102 |
+
st.subheader('LED Status')
|
| 103 |
+
led_status_display = st.empty()
|
| 104 |
+
|
| 105 |
+
st.subheader('Buzzer Status')
|
| 106 |
+
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")
|
| 113 |
+
led_status_display.text(f"LED: {bus_info['led_status']}")
|
| 114 |
+
buzzer_status_display.text(f"Buzzer: {bus_info['buzzer_status']}")
|
| 115 |
+
time.sleep(1)
|