Introduction
Setting up a robust WiFi system for your drone's companion computer is crucial for seamless field operations. This guide will walk you through creating a "best of both worlds" setup that allows your Raspberry Pi to:
- Automatically connect to your home WiFi when available (for easy development and updates)
- Create its own access point when no known networks are available (for reliable field operations)
Note: This guide assumes you're using a Raspberry Pi with Ubuntu 22.04, but the concepts apply to other Linux-based systems as well.
Understanding the Two Modes
Client Mode
In this mode, your Pi connects to an existing WiFi network as a client. This is useful when you're at home or in a location with internet access.
IP: Dynamically assigned by router (e.g., 192.168.10.32)
Access Point (AP) Mode
In this mode, your Pi creates its own WiFi network that other devices can connect to. This is essential for field operations where no infrastructure exists.
IP: Statically set to 10.42.0.1
Prerequisites
Before beginning, ensure you have:
- A Raspberry Pi with Ubuntu 22.04 installed
- SSH access to your Pi
- Basic familiarity with the command line
- Knowledge of your home WiFi credentials
Step-by-Step Implementation
1. Install Required Packages
First, install the necessary software for creating an access point:
sudo apt update
sudo apt install hostapd dnsmasq
Important: If you encounter dependency issues (common with Node.js conflicts), resolve them with:
sudo apt --fix-broken install
2. Configure dnsmasq
Configure dnsmasq to handle DHCP services on the access point interface:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
sudo nano /etc/dnsmasq.conf
Add the following configuration:
# Listen on the hotspot interface
interface=uap0
# Enable DHCP
dhcp-range=10.42.0.10,10.42.0.100,255.255.255.0,24h
# DNS server to offer to clients
dhcp-option=6,1.1.1.1
# Log for debugging
log-dhcp
3. Configure hostapd
Create the hostapd configuration file to define your access point:
sudo nano /etc/hostapd/hostapd.conf
Add the following configuration (customize the SSID and password):
# Network name
ssid=MyDroneAP
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
# Network password
wpa_passphrase=MySecurePassword123
# The interface to use
interface=uap0
# Driver interface type
driver=nl80211
Tell the system where to find this config file:
sudo nano /etc/default/hostapd
Find the line #DAEMON_CONF="" and change it to:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
4. Create the Autohotspot Script
This script automatically switches between client and AP modes:
sudo nano /usr/bin/autohotspot
Add the following code (replace "Your Home WiFi Name" with your actual network name):
#!/bin/bash
# Your home WiFi SSID
HOME_SSID="Your Home WiFi Name"
# Check if the home WiFi is available and connected
if iwgetid -r | grep -q "$HOME_SSID"; then
echo "Connected to home network. Exiting."
exit 0
else
echo "Home network not found. Starting Access Point."
# Create the virtual interface for the AP
sudo iw dev wlan0 interface add uap0 type __ap
# Bring up the interface with the static IP
sudo ip link set uap0 up
sudo ip addr add 10.42.0.1/24 dev uap0
# Start the required services
sudo systemctl enable dnsmasq
sudo systemctl enable hostapd
sudo systemctl start dnsmasq
sudo systemctl start hostapd
fi
Make the script executable:
sudo chmod +x /usr/bin/autohotspot
5. Create a Systemd Service
Create a service to run the autohotspot script on boot:
sudo nano /etc/systemd/system/autohotspot.service
Add the following content:
[Unit]
Description=AutoHotspot Service
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/autohotspot
ExecStop=/bin/true
[Install]
WantedBy=multi-user.target
Enable the service:
sudo systemctl enable autohotspot.service
6. Add Your Home WiFi Credentials
Add your home network to the WiFi configuration:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Add your network configuration at the bottom of the file:
network={
ssid="Your Home WiFi Name"
psk="Your Home WiFi Password"
key_mgmt=WPA-PSK
}
7. Resolve Common Issues
ModemManager Conflict
ModemManager can interfere with serial ports used for drone communication:
sudo apt remove modemmanager
systemd-resolved Conflict
systemd-resolved can conflict with dnsmasq for port 53:
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved
Unmask hostapd
If hostapd is masked (disabled), unmask it:
sudo systemctl unmask hostapd
8. Final Testing
Reboot your Pi to test the configuration:
sudo reboot
Test both scenarios:
- Client Mode: Ensure your home WiFi is available. The Pi should connect to it automatically.
- AP Mode: Disable your home WiFi. The Pi should create the "MyDroneAP" network after reboot.
Troubleshooting
Check Service Status
sudo systemctl status hostapd
sudo systemctl status dnsmasq
sudo systemctl status autohotspot.service
Check Interface Status
ip addr show uap0
You should see the uap0 interface with the IP address 10.42.0.1.
Check Logs
sudo journalctl -u hostapd -n 50 --no-pager
sudo journalctl -u dnsmasq -n 50 --no-pager
Conclusion
You've now set up a robust dual-mode WiFi system for your drone companion computer. This setup provides:
- Convenient access to your Pi via your home network during development
- Reliable field connectivity through the Pi's own access point
- A consistent IP address (10.42.0.1) for connecting ground control software
- Automatic fallback between modes based on network availability
With this configuration, you can take your drone anywhere and always have a reliable connection to its companion computer for mission control, telemetry monitoring, and configuration changes.