Skip to content

VarunChopra11/pi_connect

Repository files navigation

Pi-Connect 2.0 πŸ”Œ

Production-grade, zero-interference WiFi configuration system for headless Raspberry Pi via Bluetooth Low Energy

License: MIT Platform: Raspberry Pi Python: 3.7+


🎯 Overview

Pi-Connect 2.0 is a sophisticated IoT solution that enables WiFi configuration on headless Raspberry Pi devices through a secure Bluetooth Low Energy (BLE) interface. Designed with a "set-and-forget" philosophy, it operates as an intelligent system service that minimizes resource consumption while maximizing reliability.

The Problem It Solves

Configuring WiFi on a Raspberry Pi without a display, keyboard, or Ethernet connection is notoriously difficult. Traditional solutions require:

  • Pre-configuration of WiFi credentials before deployment
  • Physical access to edit files on the SD card
  • Temporary Ethernet connections
  • Complex command-line operations

The Pi-Connect 2.0 Solution

  • βœ… Configure WiFi from any smartphone or laptop with Bluetooth
  • βœ… No display, keyboard, or Ethernet required
  • βœ… Secure HMAC-SHA256 authentication
  • βœ… Automatic power management (BLE turns off when connected)
  • βœ… Hardware rescue button for field recovery
  • βœ… Zero interference with primary Pi applications

✨ Key Features

πŸ” Enterprise-Grade Security

  • HMAC-SHA256 authentication with nonce-based challenge-response
  • Replay protection prevents credential theft
  • Configurable shared secrets via environment variables
  • No plaintext transmission of sensitive data

πŸ€– Intelligent State Machine

  • BOOT: Auto-detects internet connectivity
  • IDLE: Sleeps when WiFi is connected (60sec check intervals)
  • ADVERTISING: Activates BLE only when needed
  • CONNECTING: Pauses watchdog during credential processing

⚑ Zero-Interference Design

  • CPU usage: <1% when idle
  • Memory: <100MB max
  • Aggressive sleep intervals
  • Low process priority (Nice: 10)
  • No conflicts with robotics, AI, or IoT workloads

πŸ›‘οΈ Hardware Rescue Button

  • Force configuration mode with 5-second long press
  • Uses safe GPIO 27 (avoids HAT conflicts)
  • Perfect for field deployments
  • Optional LED status indicator

πŸ“± Modern Web Interface

  • Single-file HTML deployment (works on Vercel, GitHub Pages, or locally)
  • Web Bluetooth API - no app installation required
  • Dark mode UI with real-time status updates
  • Cross-platform: Works on Chrome (desktop/Android), Edge, Opera

πŸ”„ Robust Network Management

  • Uses NetworkManager (nmcli) for reliable connections
  • Internet connectivity verification (ping 8.8.8.8)
  • Gateway detection for local networks
  • Automatic retry logic
  • Clean connection state management

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Pi-Connect 2.0 System                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ State Machine│◄──────   Watchdog    │────►│ Network  β”‚ β”‚
β”‚  β”‚   (Reactive) β”‚      β”‚   (Thread)    β”‚     β”‚ Manager  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                                           β”‚      β”‚
β”‚         β–Ό                                           β–Ό      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ BLE GATT     │◄────►│   Security    β”‚     β”‚  GPIO    β”‚ β”‚
β”‚  β”‚ Server       β”‚      β”‚   Manager     β”‚     β”‚  Button  β”‚ β”‚
β”‚  β”‚ (BlueZ/D-Bus)β”‚      β”‚   (HMAC)      β”‚     β”‚ (Thread) β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                      β–²                           β”‚
β”‚         β”‚                      β”‚                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                      β”‚
          β”‚ BLE Connection       β”‚ Credentials
          β–Ό                      β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚   Web Client (Vite App)              β”‚
    β”‚   - Web Bluetooth API                β”‚
    β”‚   - HMAC-SHA256 Calculation          β”‚
    β”‚   - Real-time Status Updates         β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ What's Included

1. Backend Service (pi_connect.py)

  • 650+ lines of production-ready Python
  • Complete state machine implementation
  • BlueZ GATT server via D-Bus
  • NetworkManager integration
  • GPIO button handler with debouncing
  • Rotating file logger (1MB max, 5 backups)
  • Comprehensive error handling

2. Web Client (Vite + ES Modules)

  • Modern build tooling with Vite
  • Web Bluetooth API integration
  • Crypto-JS for HMAC calculation
  • Secure environment variables (.env)
  • Modern dark mode interface
  • Real-time BLE notifications
  • Optimized production builds

3. System Service (pi-connect.service)

  • Systemd unit file
  • Auto-start on boot
  • Resource limits (CPU quota, memory cap)
  • Security hardening options
  • Environment variable support

4. Documentation

  • HARDWARE_GUIDE.md: Complete GPIO wiring instructions
  • INSTALLATION.md: Step-by-step setup guide
  • README.md: Project overview and architecture
  • QUICK_REFERENCE.md: Command cheat sheet

5. Web Client Files

  • src/main.js: Application logic (ES modules)
  • index.html: Main HTML template
  • package.json: Dependencies and build scripts
  • vite.config.js: Vite configuration
  • .env.example: Environment variable template

πŸš€ Quick Start

1. Install on Raspberry Pi

# Clone or download the project
cd /tmp

# Run installation
sudo apt update
sudo apt install -y python3 python3-pip python3-dbus python3-gi \
    bluetooth bluez network-manager gpiozero

pip3 install pydbus --break-system-packages

# Deploy files
sudo mkdir -p /opt/pi-connect
sudo cp pi_connect.py /opt/pi-connect/
sudo cp pi-connect.service /etc/systemd/system/

# Generate and set shared secret
SECRET=$(openssl rand -base64 32)
echo "Your secret: $SECRET"
# Update pi-connect.service with this secret

# Start service
sudo systemctl daemon-reload
sudo systemctl enable pi-connect
sudo systemctl start pi-connect

2. Deploy Web Interface

# Install dependencies
npm install

# Create .env file and set your shared secret
cp .env.example .env
# Edit .env: VITE_SHARED_SECRET=your_secret_here

# Development (with hot reload)
npm run dev

# Production build
npm run build

# Deploy to Vercel: Push to GitHub, import to vercel.com
# Set VITE_SHARED_SECRET in Vercel environment variables

3. Connect and Configure

  1. Open web interface on phone/laptop
  2. Enable Bluetooth
  3. Click "Connect to Pi"
  4. Enter WiFi credentials
  5. Click "Configure WiFi"
  6. Done! πŸŽ‰

Detailed instructions: See INSTALLATION.md


πŸ”§ Hardware Requirements

Minimum Requirements

  • Raspberry Pi 3B+ or newer (built-in Bluetooth)
  • Raspberry Pi OS Bookworm or Bullseye
  • 8GB+ SD card
  • Power supply (5V 2.5A minimum)

Optional Components

  • Momentary push button (for rescue mode)
  • LED indicator (for visual feedback)
  • 10kΞ© resistor (if not using internal pull-up)

Wiring Guide: See HARDWARE_GUIDE.md


πŸ”’ Security Model

Authentication Flow

Client                          Pi
  β”‚                              β”‚
  │──── Connect to BLE ─────────>β”‚
  β”‚                              β”‚
  β”‚<──── Read Nonce ─────────────│ (32-byte random)
  β”‚                              β”‚
  β”‚  Calculate HMAC:             β”‚
  β”‚  H = HMAC-SHA256(            β”‚
  β”‚    secret,                   β”‚
  β”‚    nonce + ssid + password   β”‚
  β”‚  )                           β”‚
  β”‚                              β”‚
  │──── Write HMAC ─────────────>β”‚
  β”‚                              β”‚ Verify HMAC
  β”‚                              β”‚ βœ“ Valid? Continue
  β”‚                              β”‚ βœ— Invalid? Disconnect
  │──── Write SSID ─────────────>β”‚
  │──── Write Password ─────────>β”‚
  β”‚                              β”‚
  β”‚<──── Status Updates ─────────│ (via BLE Notify)

Security Features

  • βœ… Nonce expires after 60 seconds
  • βœ… One-time use (replay protection)
  • βœ… Constant-time HMAC comparison
  • βœ… No credential logging
  • βœ… Secure credential clearing after use
  • βœ… TLS for web interface (if hosted on Vercel)

⚠️ Important Security Notes

  1. Change the default shared secret immediately
  2. Use a cryptographically random secret (32+ characters)
  3. Keep secret confidential - never commit to public repos
  4. Same secret must be configured on both Pi and web client
  5. Consider physical security of GPIO button access

πŸ“Š Performance Characteristics

Resource Usage (Measured on Pi 3B+)

State CPU Usage Memory BLE Status
IDLE (WiFi connected) <0.5% ~30MB OFF
ADVERTISING (No WiFi) ~1% ~35MB ON
CONNECTING ~2% ~40MB ON

Timing

  • Boot detection: ~2 seconds
  • State transition: ~1-2 seconds
  • WiFi connection: 10-30 seconds (network dependent)
  • IDLE check interval: 60 seconds
  • Advertising check interval: 10 seconds

Why Zero-Interference?

  • Uses time.sleep() aggressively
  • Low process priority (Nice: 10)
  • SystemD CPU quota: 10%
  • Memory limit: 100MB
  • No polling loops in critical paths
  • BLE radio OFF when not needed

πŸ§ͺ Testing

Unit Tests

# Test network connectivity
python3 -c "from pi_connect import NetworkManager; print(NetworkManager.is_connected())"

# Test GPIO button
python3 -c "from gpiozero import Button; b = Button(27); print('Press button...'); b.wait_for_press(); print('Detected!')"

Integration Tests

# Monitor service logs
sudo journalctl -u pi-connect -f

# Test state transitions
# 1. Disable WiFi β†’ Should enter ADVERTISING
# 2. Enable WiFi β†’ Should return to IDLE
# 3. Press button 5s β†’ Should force ADVERTISING

End-to-End Test

  1. Deploy fresh Raspberry Pi OS
  2. Run installation script
  3. Boot without WiFi
  4. Connect via web interface
  5. Configure WiFi
  6. Verify automatic IDLE transition
  7. Test button rescue mode

πŸ› Troubleshooting

Service won't start

# Check detailed logs
sudo journalctl -u pi-connect -n 100 --no-pager

# Common fixes:
sudo systemctl restart bluetooth
sudo rfkill unblock bluetooth
pip3 install pydbus --break-system-packages

Can't see Pi in Bluetooth scan

# Verify advertising
sudo hcitool lescan

# Check state
sudo journalctl -u pi-connect | grep "State transition"

Web Bluetooth not working

  • βœ… Use Chrome, Edge, or Opera (NOT Safari/Firefox)
  • βœ… Enable Bluetooth on device
  • βœ… Use HTTPS (required by Web Bluetooth API)
  • βœ… Grant Bluetooth permissions when prompted

Authentication fails

  • Check shared secret matches in both:
    • /etc/systemd/system/pi-connect.service
    • index.html CONFIG.SHARED_SECRET
  • Regenerate secret and update both sides

Full troubleshooting guide: See INSTALLATION.md


πŸ›£οΈ Roadmap

Version 2.1 (Planned)

  • Multiple WiFi network profiles
  • Network priority/fallback
  • WPA Enterprise support
  • Mobile app (React Native)

Version 2.2 (Future)

  • Web dashboard for Pi status
  • Remote SSH tunnel setup
  • Automatic backup credentials
  • OTA (Over-The-Air) updates

Contributions Welcome!

See CONTRIBUTING.md for guidelines (submit PRs, report issues)


πŸ“„ License

MIT License - See LICENSE file for details


πŸ™ Acknowledgments

  • BlueZ Project - Linux Bluetooth stack
  • NetworkManager - WiFi connection management
  • gpiozero - GPIO control library
  • Web Bluetooth Community - Browser API standardization
  • Raspberry Pi Foundation - Amazing hardware platform

πŸ“ž Support


πŸŽ“ Use Cases

Perfect for:

  • πŸ€– Robotics projects
  • 🏠 Home automation (Home Assistant)
  • πŸ“‘ IoT sensor networks
  • πŸŽ“ Educational projects
  • πŸ”¬ Research equipment
  • πŸ“Ή Headless camera systems
  • 🎡 Audio streaming devices

Made with ❀️ for the Raspberry Pi Community

If this project helped you, consider starring it on GitHub! ⭐

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors