web interface for controlling remote control car, view live footage from embedded mediamtx webrtc viewer. Capture joystick input from usb joystick. A panel on the side for a list of clickable buttons to trigger miscellaneous actions on the car. under the buttons should be a text box and submit button for sending text. a bar at the top that shows metrics from the car like voltage and speed, etc...
To upload files, please first save the app
import streamlit as st
import pygame
import time
from streamlit_webrtc import webrtc_streamer
from streamlit_autorefresh import st_autorefresh
# Initialize pygame for joystick
pygame.init()
pygame.joystick.init()
# Initialize session state for metrics
if 'voltage' not in st.session_state:
st.session_state.voltage = 12.0
if 'speed' not in st.session_state:
st.session_state.speed = 0.0
if 'messages' not in st.session_state:
st.session_state.messages = []
# Auto refresh every 1 second for metrics updates
st_autorefresh(interval=1000, key="metrics_refresh")
# Layout setup - Create three columns for metrics
st.markdown("### Vehicle Metrics")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Voltage", f"{st.session_state.voltage:.1f}V")
with col2:
st.metric("Speed", f"{st.session_state.speed:.1f} km/h")
with col3:
st.metric("Signal Strength", "85%")
# Main content area with two columns
main_col1, main_col2 = st.columns([3, 1])
with main_col1:
# WebRTC video stream
st.markdown("### Live Camera Feed")
webrtc_streamer(
key="example",
video_frame_callback=None,
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={"video": True, "audio": False},
)
# Joystick status
st.markdown("### Joystick Status")
try:
joystick = pygame.joystick.Joystick(0)
joystick.init()
st.success(f"Joystick detected: {joystick.get_name()}")
# Get joystick position (example values)
x_axis = joystick.get_axis(0)
y_axis = joystick.get_axis(1)
# Display joystick position
st.write(f"X-Axis: {x_axis:.2f}")
st.write(f"Y-Axis: {y_axis:.2f}")
except pygame.error:
st.error("No joystick detected. Please connect a controller.")
with main_col2:
st.markdown("### Controls")
# Control buttons
if st.button("🚨 Emergency Stop", key="stop"):
st.warning("Emergency Stop Activated!")
if st.button("📸 Take Photo", key="photo"):
st.info("Photo captured!")
if st.button("🔦 Toggle Lights", key="lights"):
st.info("Lights toggled!")
if st.button("🔊 Horn", key="horn"):
st.info("Horn activated!")
# Text command section
st.markdown("### Send Command")
command = st.text_input("Enter command:")
if st.button("Send", key="send_command"):
if command:
st.session_state.messages.append(command)
st.info(f"Command sent: {command}")
# Command history
st.markdown("### Command History")
for msg in reversed(st.session_state.messages[-5:]):
st.text(msg)
# Cleanup
pygame.quit()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?