| Crates.io | rpi-host |
| lib.rs | rpi-host |
| version | 0.1.0 |
| created_at | 2026-01-22 23:00:09.416275+00 |
| updated_at | 2026-01-22 23:00:09.416275+00 |
| description | A library to control Raspberry Pi 5 wireless interface for switching between hotspot and client modes |
| homepage | |
| repository | https://github.com/jmcguigs/rpi-host |
| max_upload_size | |
| id | 2062854 |
| size | 71,612 |
A Rust library for controlling the Raspberry Pi wireless interface, enabling easy switching between WiFi client mode and hotspot (access point) mode.
NetworkManager is included by default. Verify it's running:
systemctl status NetworkManager
If not installed (older versions):
sudo apt update
sudo apt install network-manager
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager
Ubuntu Server uses netplan with systemd-networkd by default, not NetworkManager. You'll need to install and configure NetworkManager:
# Install NetworkManager
sudo apt update
sudo apt install network-manager
# Disable systemd-networkd (optional, but recommended to avoid conflicts)
sudo systemctl disable systemd-networkd
sudo systemctl stop systemd-networkd
# Enable NetworkManager
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager
Configure netplan to use NetworkManager by editing /etc/netplan/50-cloud-init.yaml (or similar):
network:
version: 2
renderer: NetworkManager
Apply the changes:
sudo netplan generate
sudo netplan apply
Verify NetworkManager is managing your WiFi interface:
nmcli device status
# Should show wlan0 as "wifi" with state "connected" or "disconnected"
# If it shows "unmanaged", reboot or check netplan configuration
# List wireless interfaces
nmcli device status
# Should show something like:
# DEVICE TYPE STATE CONNECTION
# wlan0 wifi connected MyNetwork
Add to your Cargo.toml:
[dependencies]
rpi-host = "0.1"
use rpi_host::WifiManager;
fn main() -> Result<(), rpi_host::WifiError> {
let wifi = WifiManager::new()?;
// Connect to a secured network
wifi.connect("MyNetwork", Some("password123"))?;
// Or connect to an open network
wifi.connect("OpenNetwork", None)?;
// Check if we have internet
if wifi.has_internet()? {
println!("Connected to the internet!");
}
Ok(())
}
use rpi_host::WifiManager;
fn main() -> Result<(), rpi_host::WifiError> {
let wifi = WifiManager::new()?;
// Start a secured hotspot (password must be 8+ characters)
wifi.start_hotspot("Pi-Hotspot", Some("mypassword"))?;
// Or an open hotspot
wifi.start_hotspot("Pi-Hotspot-Open", None)?;
println!("Hotspot is running!");
Ok(())
}
use rpi_host::WifiManager;
fn main() -> Result<(), rpi_host::WifiError> {
let wifi = WifiManager::new()?;
for network in wifi.scan()? {
println!(
"{:3}% | {:20} | {} {}",
network.signal_strength,
network.ssid,
network.security,
if network.is_connected { "(connected)" } else { "" }
);
}
Ok(())
}
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 band
.with_channel(36);
wifi.start_hotspot_with_config(config)?;
Ok(())
}
use rpi_host::WifiManager;
fn main() -> Result<(), rpi_host::WifiError> {
let wifi = WifiManager::new()?;
let status = wifi.status()?;
println!("Mode: {}", status.mode);
println!("Connection: {:?}", status.connection_name);
println!("IP Address: {:?}", status.ip_address);
println!("Has Internet: {}", status.has_internet);
Ok(())
}
use rpi_host::WifiManager;
use std::time::Duration;
fn main() -> Result<(), rpi_host::WifiError> {
let wifi = WifiManager::new()?;
// Connect and wait up to 30 seconds for internet
wifi.connect_with_internet(
"MyNetwork",
Some("password"),
Duration::from_secs(30)
)?;
println!("Connected with internet access!");
Ok(())
}
| 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 |
WifiMode: Client, Hotspot, or DisconnectedNetworkInfo: Scanned network details (SSID, signal, security, etc.)HotspotConfig: Hotspot configuration builderHotspotBand: Bg (2.4GHz) or A (5GHz)SecurityType: Network security (Open, WPA2, WPA3, etc.)ConnectionStatus: Current connection detailsConnectivityResult: Internet check results with latency# Build the example
cargo build --example basic
# Run with sudo (required for WiFi operations)
sudo ./target/debug/examples/basic scan
sudo ./target/debug/examples/basic connect MyNetwork mypassword
sudo ./target/debug/examples/basic hotspot PiAP password123
sudo ./target/debug/examples/basic status
sudo ./target/debug/examples/basic stop
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 outThe library uses the log crate. Enable logging with env_logger or your preferred logger:
fn main() {
env_logger::init();
// ... your code
}
RUST_LOG=debug sudo ./your_program
Ensure your wireless interface exists:
nmcli device status
ip link show
WiFi operations require root permissions:
sudo ./your_program
On Raspberry Pi OS: WiFi might be managed by wpa_supplicant instead:
# Check what's managing WiFi
nmcli device status
# If it shows "unmanaged", edit /etc/NetworkManager/NetworkManager.conf
# and ensure [ifupdown] managed=true
sudo systemctl restart NetworkManager
On Ubuntu Server: Ensure netplan is configured to use NetworkManager:
# Check current renderer
cat /etc/netplan/*.yaml
# If renderer is not NetworkManager, update the file:
# renderer: NetworkManager
# Then apply
sudo netplan apply
sudo reboot
HotspotBand::Bg) for better compatibilityMIT License - see LICENSE for details.
Contributions are welcome! Please feel free to submit issues and pull requests.