Skip to content

Instantly share code, notes, and snippets.

@tcg
Created June 9, 2017 16:25
Embed
What would you like to do?
Raspberry Pi Zero W as Internet connected Access Point *and* Wireless Client

I wanted this capability for various reasons. It's not necessarily stable, because uap0 has to use the same channel as wlan0, and if you're traveling/mobile, wifi channels aren't necessarily predictable.

This is not a guide. This is just so I remember how to get back where I started, in case I wipe this thing. =)

Extremely helpful guides that got me here:

In my particular configuration, the Pi was set up with a hostname of altoid. With the avahi tweak, I am now consistently able to access the Pi via altoid.local from machines on the same network (or connected directly, via the AP).

Out of the box, I used the "NOOBS" that comes preloaded, got internet via wifi, and selected to install Raspbian Lite. After that, I installed dnsmasq, and hostapd. Not sure if anything else was required.

/etc/network/interfaces

source-directory /etc/network/interfaces.d

auto lo
auto eth0
auto wlan0
auto uap0

iface lo inet loopback

iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

iface uap0 inet static
  address 192.168.50.1
  netmask 255.255.255.0
  network 192.168.50.0
  broadcast 192.168.50.255
  gateway 192.168.50.1

/etc/dnsmasq.conf

# Lots of default stuff commented out. 
# Then this... 
interface=lo,uap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.50.50,192.168.50.150,12h

/etc/rc.local

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# I don't think this part even works...?
printf "Sleeping for 5 seconds befor hostapdstart"
sleep 5
hostapdstart >1&

exit 0

/usr/local/bin/hostapdstart

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
iw dev wlan0 interface add uap0 type __ap
service dnsmasq restart
sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 ! -d 192.168.50.0/24 -j MASQUERADE
ifup uap0
hostapd /etc/hostapd/hostapd.conf

/etc/hostapd/hostapd.conf

interface=uap0
#driver=
# The name for the AP. Can include spaces, like "Inconspicuous Rock".
ssid=your_ap_wifi_network_name
#country_code=US
hw_mode=g
channel=11
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
# Comment out the wpa* group below if you 
# want to run an open network. 
wpa=2
wpa_passphrase=the_password_for_this_network
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_group_rekey=86400
ieee80211n=1
wme_enabled=1

/etc/default/avahi-daemon

# I had to change this from 1 to 0, because avahi 
# wasn't running on start (probably failing early). 
# This was key to be able to access the Pi via 
# its "{hostname}.local" address on the network.
AVAHI_DAEMON_DETECT_LOCAL=0
@Oddnoun
Copy link

Oddnoun commented Sep 21, 2017

Did you implement these scripts under Jessie or Stretch version of Raspbian?

@tcg
Copy link
Author

tcg commented Sep 25, 2017

@Oddnoun I think Jessie? I'll have to fire it up and check. It was whatever was most current (and stable) on June 9th, 2017.

@dbarrerap
Copy link

Jessie, for sure! Stretch has made some changes on networking, so establishing a static IP is somewhat different

@pvanh80
Copy link

pvanh80 commented Oct 23, 2017

is this configuration still working on Raspberry Pi 3 model B? I have struggled for this for a weeks until now

@tcg
Copy link
Author

tcg commented Dec 19, 2017

@pvanh80 I've never owned or used a Pi 3, so I don't know. Sorry.

@mahesh2000
Copy link

i used the script by @lukicdarkoo at https://github.com/lukicdarkoo/rpi-wifi with minor modifications. It worked perfectly with Raspbian Buster (Dec 2019).

Change the script as below (per comments by @david712 on Mar 6 2019 at https://gist.github.com/lukicdarkoo/6b92d182d37d0a10400060d8344f86e4):


wpa=2PASSPHRASE
to
wpa=2

next, insert #!/bin/bash as below

# Populate `/bin/start_wifi.sh
sudo bash -c 'cat > /bin/rpi-wifi.sh' << EOF
#!/bin/bash
echo 'Starting Wifi AP and client...'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment