Muthuraja18 commited on
Commit
98639b7
·
verified ·
1 Parent(s): 81a456f
Files changed (1) hide show
  1. app.py +47 -78
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
- # 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()
@@ -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")