rpi-host
A Rust library for controlling the Raspberry Pi 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 and security info
- Internet Connectivity Validation: Check and wait for internet access
- Automatic Mode Switching: Seamlessly switch between client and hotspot modes
- Easy-to-use API: High-level interface designed for simplicity
Requirements
Hardware
- Raspberry Pi with wireless interface (tested on Pi 5, should work on Pi 3/4/Zero W)
- Built-in WiFi or compatible USB WiFi adapter
Software
- NetworkManager - must be installed and running
- Root/sudo permissions - required for WiFi operations
Raspberry Pi OS (Bookworm+)
NetworkManager is included by default. Verify it's running:
If not installed (older versions):
Ubuntu Server for Raspberry Pi
Ubuntu Server uses netplan with systemd-networkd by default, not NetworkManager. You'll need to install and configure NetworkManager:
# Install NetworkManager
# Disable systemd-networkd (optional, but recommended to avoid conflicts)
# Enable NetworkManager
Configure netplan to use NetworkManager by editing /etc/netplan/50-cloud-init.yaml (or similar):
network:
version: 2
renderer: NetworkManager
Apply the changes:
Verify NetworkManager is managing your WiFi interface:
# Should show wlan0 as "wifi" with state "connected" or "disconnected"
# If it shows "unmanaged", reboot or check netplan configuration
Checking Your Wireless Interface
# List wireless interfaces
# Should show something like:
# DEVICE TYPE STATE CONNECTION
# wlan0 wifi connected MyNetwork
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
Connect to a WiFi Network
use WifiManager;
Create a Hotspot
use WifiManager;
Scan for Networks
use WifiManager;
Advanced Hotspot Configuration
use ;
Check Connection Status
use WifiManager;
Connect and Wait for Internet
use WifiManager;
use Duration;
API Overview
WifiManager Methods
| Method | Description |
|---|---|
new() |
Create manager for default interface (wlan0) |
with_interface(name) |
Create manager for specific interface |
connect(ssid, password) |
Connect to WiFi network as client |
connect_with_internet(ssid, password, timeout) |
Connect and wait for internet |
start_hotspot(ssid, password) |
Start WiFi access point |
start_hotspot_with_config(config) |
Start AP with custom configuration |
stop_hotspot() |
Stop the hotspot |
disconnect() |
Disconnect from current network |
scan() |
Scan for available networks |
get_mode() |
Get current mode (Client/Hotspot/Disconnected) |
status() |
Get detailed connection status |
has_internet() |
Quick internet connectivity check |
check_connectivity() |
Detailed connectivity check with latency |
wait_for_internet(timeout) |
Wait for internet access |
enable_wifi() / disable_wifi() |
Control WiFi radio |
forget_network(ssid) |
Delete saved network profile |
Types
WifiMode:Client,Hotspot, orDisconnectedNetworkInfo: Scanned network details (SSID, signal, security, etc.)HotspotConfig: Hotspot configuration builderHotspotBand:Bg(2.4GHz) orA(5GHz)SecurityType: Network security (Open, WPA2, WPA3, etc.)ConnectionStatus: Current connection detailsConnectivityResult: Internet check results with latency
Running the Example
# Build the example
# Run with sudo (required for WiFi operations)
Error Handling
All operations return Result<T, WifiError>. Common errors include:
WifiError::InterfaceNotFound- Wireless interface doesn't existWifiError::ConnectionFailed- Failed to connect to networkWifiError::HotspotCreationFailed- Failed to create hotspotWifiError::NoInternetConnectivity- No internet accessWifiError::InvalidConfiguration- Invalid settings (e.g., password too short)WifiError::WifiDisabled- WiFi radio is disabledWifiError::Timeout- Operation timed out
Logging
The library uses the log crate. Enable logging with env_logger or your preferred logger:
RUST_LOG=debug
Troubleshooting
"Interface not found"
Ensure your wireless interface exists:
"Permission denied"
WiFi operations require root permissions:
NetworkManager not controlling WiFi
On Raspberry Pi OS: WiFi might be managed by wpa_supplicant instead:
# Check what's managing WiFi
# If it shows "unmanaged", edit /etc/NetworkManager/NetworkManager.conf
# and ensure [ifupdown] managed=true
On Ubuntu Server: Ensure netplan is configured to use NetworkManager:
# Check current renderer
# If renderer is not NetworkManager, update the file:
# renderer: NetworkManager
# Then apply
Hotspot not visible to other devices
- Ensure no other connection is active
- Try 2.4GHz band (
HotspotBand::Bg) for better compatibility - Check that the password is at least 8 characters
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.