Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import serial | |
| import time | |
| import folium | |
| from folium.plugins import MarkerCluster | |
| import io | |
| import base64 | |
| # Setup serial connection | |
| arduino_port = "COM3" # Replace with your Arduino port. If necessary, change to a different port. | |
| baud_rate = 9600 | |
| # Check if the port is available before attempting to open it | |
| try: | |
| ser = serial.Serial(arduino_port, baud_rate) | |
| except serial.SerialException as e: | |
| st.error(f"Error: {e}") | |
| st.stop() # Stop the script if the serial connection fails | |
| # Dummy user credentials (for login) | |
| USER_CREDENTIALS = { | |
| "admin": "password123", # Username: admin, Password: password123 | |
| "user1": "mypassword" # Username: user1, Password: mypassword | |
| } | |
| # Function to display login page | |
| def login_page(): | |
| st.title("Bus System Status Monitoring") | |
| st.subheader("Please login to access the system") | |
| # Input for username and password | |
| username = st.text_input("Username", "") | |
| password = st.text_input("Password", "", type="password") | |
| # Check if the user submits the login form | |
| if st.button("Login"): | |
| if username in USER_CREDENTIALS and USER_CREDENTIALS[username] == password: | |
| st.success("Login Successful!") | |
| return username # return username to proceed to the monitoring page | |
| else: | |
| st.error("Invalid credentials, please try again.") | |
| return None | |
| # Function to read data from Arduino (simulating bus data including GPS location) | |
| def read_data_from_arduino(): | |
| bus_name = "" | |
| seat_status = "" | |
| distance = 0 | |
| latitude = 0.0 | |
| longitude = 0.0 | |
| while True: | |
| if ser.in_waiting > 0: | |
| data = ser.readline().decode('utf-8').strip() | |
| if "Bus Name:" in data: | |
| bus_name = data.split("Bus Name: ")[1] | |
| if "Seat Status:" in data: | |
| seat_status = data.split("Seat Status: ")[1] | |
| if "Distance:" in data: | |
| distance = int(data.split("Distance: ")[1]) | |
| if "Latitude:" in data: | |
| latitude = float(data.split("Latitude: ")[1]) | |
| if "Longitude:" in data: | |
| longitude = float(data.split("Longitude: ")[1]) | |
| # Display data in Streamlit | |
| st.subheader(f"Bus Name: {bus_name}") | |
| st.subheader(f"Seat Status: {seat_status}") | |
| st.subheader(f"Distance to Bus: {distance} cm") | |
| st.subheader(f"Latitude: {latitude}, Longitude: {longitude}") | |
| # Plot Bus on Map | |
| plot_map(latitude, longitude) | |
| time.sleep(1) # Delay for 1 second before reading next set of data | |
| # Function to plot the map with the bus location | |
| def plot_map(latitude, longitude): | |
| # Create a map centered around the bus location | |
| bus_map = folium.Map(location=[latitude, longitude], zoom_start=15) | |
| # Add a marker for the bus location | |
| folium.Marker([latitude, longitude], popup="Bus Location").add_to(bus_map) | |
| # Render the map in Streamlit | |
| st.subheader("Real-Time Bus Location") | |
| # Save the map to a PNG image | |
| map_stream = io.BytesIO() | |
| bus_map.save(map_stream, close_file=False) | |
| # Convert the map to base64 for display in Streamlit | |
| map_stream.seek(0) | |
| map_data = base64.b64encode(map_stream.getvalue()).decode() | |
| st.markdown(f'<img src="data:image/png;base64,{map_data}" width="100%" />', unsafe_allow_html=True) | |
| # Main function for the Streamlit app | |
| def main(): | |
| # Show the login page | |
| username = login_page() | |
| # If the user is logged in, show the bus system page | |
| if username: | |
| st.write("Loading the Bus System Monitoring Dashboard...") | |
| # Run the real-time data reading from Arduino | |
| read_data_from_arduino() | |
| # Run the app | |
| if __name__ == "__main__": | |
| main() | |