Skip to content

retromonos/SwampHacks11FIH

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

130 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwampHacks11 - Pi Video Streaming

A simple WebSocket-based video streaming system from Raspberry Pi to a web interface.

Architecture

  • Backend: FastAPI WebSocket server that receives video from Pi and broadcasts to viewers
  • Pi Connection: Python client that captures video from Pi Camera and streams via WebSocket
  • Frontend: Simple HTML/JS viewer embedded in the backend

Setup Instructions

Backend Server Setup

  1. Install dependencies:
cd backend
pip install -r requirements.txt
  1. Run the server:
python main.py

The server will start on http://0.0.0.0:8000

  1. Open a browser and go to http://localhost:8000 to view the stream

Raspberry Pi Setup

  1. Install system dependencies (if not already installed):
sudo apt-get update
sudo apt-get install -y python3-picamera2 python3-libcamera python3-pip libcap-dev
  1. Create virtual environment with system-site-packages (required for libcamera access):
python3 -m venv --system-site-packages .venv
source .venv/bin/activate
  1. Install Python dependencies:
pip install -r requirements.txt
  1. Set up SSH tunnel from Pi to your desktop:
# On the Raspberry Pi, create SSH tunnel to desktop
ssh -L 8000:localhost:8000 your_username@your_desktop_ip

Keep this SSH connection open in a separate terminal.

  1. Enable the camera:
sudo raspi-config
# Navigate to: Interface Options -> Camera -> Enable
  1. Run the stream client (in a new terminal on the Pi):
python3 stream.py

The client will connect to localhost:8000 which tunnels to your desktop.

How It Works

  1. Pi Side:

    • Captures video frames from the Pi Camera
    • Encodes frames as JPEG
    • Sends frames via WebSocket to /ws/pi endpoint
  2. Backend Side:

    • Receives frames from Pi at /ws/pi
    • Broadcasts frames to all connected viewers at /ws/viewer
    • Serves a simple HTML viewer at the root URL
  3. Viewer Side:

    • Connects to /ws/viewer via WebSocket
    • Receives JPEG frames as binary data
    • Displays frames as images in the browser

Endpoints

  • GET / - HTML viewer interface
  • WS /ws/pi - WebSocket endpoint for Pi to send stream
  • WS /ws/viewer - WebSocket endpoint for viewers to receive stream
  • GET /health - Health check endpoint

Configuration

Backend (backend/main.py)

  • Default port: 8000
  • Change in the uvicorn.run() call

Pi Client (pi_connection/stream.py)

  • SERVER_HOST: localhost (via SSH tunnel)
  • WIDTH/HEIGHT: Video resolution (default: 640x480)
  • FPS: Frame rate (default: 20 fps)

SSH Tunnel Setup

The Pi connects to your desktop via SSH tunnel:

# From Pi to Desktop
ssh -L 8000:localhost:8000 user@desktop_ip

This forwards port 8000 on the Pi to port 8000 on your desktop.

Troubleshooting

Pi Camera Not Working

  • Ensure camera is enabled: sudo raspi-config
  • Check camera connection: libcamera-hello
  • Verify picamera2 installation: python3 -c "import picamera2"

Connection Issues

  • Ensure SSH tunnel is active and running
  • Check that backend server is running on desktop
  • Verify SSH connection: ssh user@desktop_ip
  • Test tunnel: curl http://localhost:8000/health (from Pi)

Performance Issues

  • Lower FPS in stream.py
  • Reduce resolution (e.g., 320x240)
  • Check network bandwidth

Future Enhancements

  • Add audio streaming
  • Multiple camera support
  • Recording functionality
  • Authentication
  • Better error handling
  • Stream quality controls

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors