PiZeroW AP + Wifi Client

So I wanted to be able to set the Wifi details of a Pi without connecting a monitor and keyboard. Easy, right? No. RaspAP almost does it out-of-the-box, but I couldn’t quite crack it. It’s the best solution once it’s working. Insetad I use “manual method”.

Onboard Single WiFi Adaptor for AP and Station

This is known as “Managed Mode” and uses the PiZero W’s onbard wifi for both AP and connecting to an AP (Station).

See below two wifi adaptors (a USB WiFi dongle) method.

Manual Method

After burning a fresh Raspbian Lite to the PiZeroW:

Use this short script to setup up the Pi as a AP and connects to a AP as a client, simultaneously:

wget -O rpi-wifi.sh https://raw.githubusercontent.com/brettbeeson/rpi-wifi/master/configure 
chmod 755 rpi-wifi.sh
./rpi-wifi.sh  -a tmv-$HOSTNAME-ap imagines -c "NetComm 0405" 12345678

RaspAP

Update May 2020: RaspAP has been slightly updated and install process is clarified for the PiZeroW. But …

Update June 2020: RaspAP installs and runs ok, but it (or something else) is flaky: wlan0 connection is repeatedly dropped.

Onboard WiFi wlan0 as AP, USB Wifi dongle as Station on wlan1

The setup here is:

Internet     <----->    WiFi Dongle | RPi | WiFi On-board    <----> clients

This is useful to add a high-powered WiFi dongle [https://thepihut.com/products/wifi-dongle-ultra-long-range-high-gain-w-5dbi-antenna]. A local device (e.g. IotaWatt) can use the AP of the Pi, and the Pi can use the cable to move the antenna to a good spot to get WiFi.

  1. Setup pi with SSH, WiFi (on wlan0 internal), etc.
  2. Plug in the USB Dongle. It will get a (second) IP address as it will use wpa_supplicant.conf file too. Don’t worry!
# 3. Install RaspAP
curl -sL https://install.raspap.com | bash
# Select default except don't install adblock or openvpn

4. Browse to localhost (or hostname.local) and set Hotspot -> select wlan0 -> Start Hotspot. Configure AP (“hotspot”) ssid and password. Save and resetart hotspot. You should see the hotspot visible on a phone, etc.

! make sure wlan0 (not wlan1) is specified as the AP in /etc/hostapd/hostapd.conf

# 5 - tell wlan1 (but not wlan0) to use wpa_supplicant.conf to connect as Station
sudo mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan1.conf
# make sure wlan0 (not wlan1) is specified as the AP in /etc/hostapd/hostapd.conf

5. Reboot.

6. You’ll find that internet is unavailable. It’s trying to go through the AP network on wlan0, not wlan1

>route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.3.141.1      0.0.0.0         UG    302    0        0 wlan0
0.0.0.0         192.168.0.1     0.0.0.0         UG    303    0        0 wlan1
10.3.141.0      0.0.0.0         255.255.255.0   U     302    0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan1

To fix this (source), edit /etc/dhcpcd.conf, adding:

interface wlan0;
metric 200;
# lower value wins, so use wlan1 first
interface wlan1;
metric 100;

… and reboot and check `route-n` and check ping www.google.com to see you have internet connection.

7. Check AP internet connection (e.g. on a phone). If it’s not working check /etc/sysctl.conf and iptables -L -t nat to ensure that masquerading is set.

One thought on “PiZeroW AP + Wifi Client

Leave a Reply

Your email address will not be published. Required fields are marked *