Skip to main content

Crate rpi_host

Crate rpi_host 

Source
Expand description

§rpi-host

A library for controlling the Raspberry Pi 5 wireless interface, enabling easy switching between WiFi client mode and hotspot (access point) mode.

§Features

  • Client Mode: Connect to WiFi networks with SSID and optional password
  • Hotspot Mode: Create a WiFi access point for other devices to connect to
  • Network Scanning: Discover available WiFi networks with signal strength
  • Internet Connectivity: Validate internet access with multiple fallback targets
  • Easy-to-use API: High-level interface for common WiFi operations

§Requirements

  • Raspberry Pi with wireless interface (tested on Pi 5)
  • NetworkManager installed and running (default on Raspberry Pi OS Bookworm+)
  • Root/sudo permissions for WiFi operations

§Quick Start

§Connect to a WiFi Network

use rpi_host::WifiManager;

fn main() -> Result<(), rpi_host::WifiError> {
    let wifi = WifiManager::new()?;

    // Connect to a network
    wifi.connect("MyNetwork", Some("password123"))?;

    // Check if we have internet
    if wifi.has_internet()? {
        println!("Connected to the internet!");
    }

    Ok(())
}

§Create a Hotspot

use rpi_host::WifiManager;

fn main() -> Result<(), rpi_host::WifiError> {
    let wifi = WifiManager::new()?;

    // Start a hotspot (password must be 8+ characters)
    wifi.start_hotspot("Pi-Hotspot", Some("mypassword"))?;

    // Check current mode
    println!("Current mode: {}", wifi.get_mode()?);

    Ok(())
}

§Scan for Networks

use rpi_host::WifiManager;

fn main() -> Result<(), rpi_host::WifiError> {
    let wifi = WifiManager::new()?;

    // Scan for available networks
    for network in wifi.scan()? {
        println!("{}: {}% signal, {}",
            network.ssid,
            network.signal_strength,
            network.security
        );
    }

    Ok(())
}

§Advanced Hotspot Configuration

use rpi_host::{WifiManager, HotspotConfig, HotspotBand};

fn main() -> Result<(), rpi_host::WifiError> {
    let wifi = WifiManager::new()?;

    // Create a 5GHz hotspot with custom settings
    let config = HotspotConfig::new("My5GHotspot")
        .with_password("securepassword")
        .with_band(HotspotBand::A)  // 5GHz
        .with_channel(36);

    wifi.start_hotspot_with_config(config)?;

    Ok(())
}

§Switching Modes

The library handles mode switching automatically. When you call connect() while in hotspot mode, it will stop the hotspot first. Similarly, start_hotspot() will disconnect from any current network.

use rpi_host::WifiManager;

fn main() -> Result<(), rpi_host::WifiError> {
    let wifi = WifiManager::new()?;

    // Start in client mode
    wifi.connect("HomeNetwork", Some("homepass"))?;

    // ... later, switch to hotspot mode
    wifi.start_hotspot("PiAP", Some("appassword"))?;

    // ... later, switch back to client mode
    wifi.connect("HomeNetwork", Some("homepass"))?;

    Ok(())
}

Structs§

ConnectionStatus
Detailed status of the current WiFi connection.
ConnectivityResult
Result of an internet connectivity check.
HotspotConfig
Configuration for creating a WiFi hotspot.
NetworkInfo
Information about a discovered WiFi network.
WifiManager
High-level WiFi manager for Raspberry Pi

Enums§

HotspotBand
WiFi frequency band for hotspot mode.
SecurityType
Security type of a WiFi network.
WifiError
Errors that can occur when managing WiFi connections.
WifiMode
The current operating mode of the WiFi interface.

Type Aliases§

WifiResult
Result type alias for WiFi operations.