Muthuraja18 commited on
Commit
ae4643a
·
verified ·
1 Parent(s): 6b6d93c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -105
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 on Windows
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
- for port in ports:
23
- print(f"Port: {port.device}, Description: {port.description}")
24
- com_port = input("Enter the correct COM port (e.g., COM3): ")
25
- return com_port
26
-
27
- # Open serial connection to Arduino (use the correct COM port)
28
- try:
29
- com_port = get_serial_port() # Prompt user to select the correct COM port
30
- ser = serial.Serial(com_port, 9600) # Open serial port with the selected COM port
31
- time.sleep(2) # Wait for the serial connection to initialize
32
- print(f"Connected to {com_port}")
33
- except serial.SerialException as e:
34
- print(f"Error opening serial port: {e}")
35
- exit()
36
-
37
- # Function to update the bus information based on received serial data
38
- def display_bus_info():
39
- global bus_info
40
- try:
41
- while True:
42
- if ser.in_waiting > 0:
43
- # Read data from serial port
44
- data = ser.readline().decode('utf-8', errors='ignore').strip() # Decode and remove extra spaces
45
-
46
- # Debug print to see the raw serial data
47
- print(f"Raw Data: {data}")
48
-
49
- if "Bus Name:" in data:
50
- bus_info['bus_name'] = data.split("Bus Name:")[1].strip()
51
-
52
- elif "Seat Status:" in data:
53
- bus_info['seat_status'] = data.split("Seat Status:")[1].strip()
54
-
55
- elif "Distance:" in data:
56
- bus_info['distance'] = data.split("Distance:")[1].strip()
57
-
58
- elif "LED:" in data:
59
- bus_info['led_status'] = data.split("LED:")[1].strip()
60
-
61
- elif "Buzzer:" in data:
62
- bus_info['buzzer_status'] = data.split("Buzzer:")[1].strip()
63
-
64
- time.sleep(1) # Small delay to prevent flooding the terminal
65
-
66
- except KeyboardInterrupt:
67
- print("Exiting...")
68
-
69
- # Start the bus information display in a separate thread
70
- bus_info_thread = threading.Thread(target=display_bus_info)
71
- bus_info_thread.daemon = True
72
- bus_info_thread.start()
73
-
74
- # Streamlit Web Interface
75
- st.title('Bus System Status')
76
-
77
- # Display the bus information
78
- col1, col2 = st.columns(2)
79
-
80
- with col1:
81
- st.subheader('Bus Name')
82
- bus_name = st.empty()
83
-
84
- with col2:
85
- st.subheader('Seat Status')
86
- seat_status = st.empty()
87
-
88
- st.subheader('Distance to Bus')
89
- distance_display = st.empty()
90
-
91
- # Display LED and Buzzer status
92
- st.subheader('LED Status')
93
- led_status_display = st.empty()
94
-
95
- st.subheader('Buzzer Status')
96
- buzzer_status_display = st.empty()
97
-
98
- # Update the information on the web interface every second
99
- while True:
100
- bus_name.text(bus_info['bus_name'])
101
- seat_status.text(bus_info['seat_status'])
102
- distance_display.text(f"Distance: {bus_info['distance']} cm")
103
- led_status_display.text(f"LED: {bus_info['led_status']}")
104
- buzzer_status_display.text(f"Buzzer: {bus_info['buzzer_status']}")
105
- time.sleep(1)
 
 
 
 
 
 
 
 
 
 
 
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)