0% found this document useful (0 votes)
2 views

WirelessAPandClient

This document provides a detailed tutorial on enabling simultaneous Access Point (AP) and Managed Mode WiFi on the Raspberry Pi Zero W using Raspbian Stretch. It covers prerequisites, installation of necessary software (Dnsmasq and Hostapd), configuration of network interfaces, and troubleshooting tips to ensure both interfaces function correctly. The guide also includes commands for setting up IP forwarding to allow devices connected to the AP to access the internet.

Uploaded by

Texashinobu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

WirelessAPandClient

This document provides a detailed tutorial on enabling simultaneous Access Point (AP) and Managed Mode WiFi on the Raspberry Pi Zero W using Raspbian Stretch. It covers prerequisites, installation of necessary software (Dnsmasq and Hostapd), configuration of network interfaces, and troubleshooting tips to ensure both interfaces function correctly. The guide also includes commands for setting up IP forwarding to allow devices connected to the AP to access the internet.

Uploaded by

Texashinobu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

TheWalrus Home Tags Categories Search  

Home Tags Categories

Raspberry Pi Zero W Simultaneous AP and Managed Mode Wi


 2017-09-26 · 2225 WORDS · 11 MINUTE READ  HOW-TO  HOW-TO · RASPBERRY-PI

Table Of Contents

Enabling Simultaneous AP and Managed Mode WiFi on Raspberry Pi Zero W (Raspbian Stretch)
Prerequisites
Adding Udev Rule To Add A Virtual AP Device At Boot Time
Installing Dnsmasq and Hostapd
Modify Our Interfaces File
We’re Done! Or Are We…
Automate The Workaround
Special Thanks

Enabling Simultaneous AP and Managed Mode WiFi on Raspberry Pi Zero W (Raspbian Stretch)

I recently purchased a pair of Raspberry Pi Zero W boards, and plan to use them for some home automation / IoT-type work. One of my requirements is that the WiFi on this board be able to run as
both a “managed” device (also known as “client” mode) and as an access point, preferrably at the same time. After looking around a bit online, I found several people who claimed to have gotten
this working, as well as posts saying it should work, based on the chipset. Despite my best e orts, I was unable to get any of those tutorials to work reliably on their own. By combining some
information garnered from each one, along with some trial and error, I was nally able to get AP/Manged mode working, as described below.

Prerequisites

This tutorial assumes you’ve already created a bootable MicroSD card running the latest Raspbian Stretch image and have some way of accessing Linux, whether via serial interface, or a monitor
and keyboard. If you haven’t, a couple of great resources I found online are:

Raspberry Pi Zero W “headless” Setup - This great tutorial walks you through getting WiFi client mode enabled on your RPi Zero W, without need for an attached monitor or keyboard. You will
need a way to modify the contents of the MicroSD card’s lesystem, such as a Linux machine, Chromebook with crouton, or VM. There are also ways to do this from Windows, but I’ll leave
that as an exercise for the reader.
Raspberry Pi Zero OTG Mode - Another awesome tutorial, via GitHub gist, which explains how to enable OTG / USB Gadget mode on the RPi Zero / Zero W. OTG mode lets you both power and
communicate with the RPi Zero W as a virtual serial device, ethernet device, or mass storage device, among other things, with nothing more than a standard (read: non-OTG) USB cable. I used
this to directly access the Raspbian Linux terminal via USB while trying to tweak the WiFi settings so that I wouldn’t drop my ssh session from headless mode.

Adding Udev Rule To Add A Virtual AP Device At Boot Time

Before we can operate our access point, we need a device allocated for it, similar to how systemd allocates a wlan0 device at boot time. On the build of Raspbian Stretch I’m using, only wlan0 is
available automatically. We create a le caled /etc/udev/rules.d/70-persistent-net.rules which contains the following:

SUBSYSTEM=="ieee80211", ACTION=="add|change", ATTR{macaddress}=="b8:27:eb:ff:ff:ff", KERNEL=="phy0", \


RUN+="/sbin/iw phy phy0 interface add ap0 type __ap", \
RUN+="/bin/ip link set ap0 address b8:27:eb:ff:ff:ff"

Note that you must replace both MAC addresses above with that of your own RPi. You can nd yours in various ways, such as from your router’s client list, or from the RPi’s command line via iw

dev :

pi@raspberrypi:~$ iw dev
phy#0
Unnamed/non-netdev interface
wdev 0x4
addr ba:27:eb:07:28:1f
type P2P-device
txpower 31.00 dBm
Interface wlan0
ifindex 2
wdev 0x1
addr b8:27:eb:ff:ff:ff
ssid <YOUR HOME SSID>
type managed
channel 6 (2437 MHz), width: 20 MHz, center1: 2437 MHz
txpower 31.00 dBm

https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 1/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

A number of tutorials I found claimed that the address for the virtual AP must be di erent from the primary MAC address. I found there to be no di erence in behavior, but you should be able to
change the last byte, for example, to give your client and AP di erent MACs.

The device we created above is called ap0 . We will refer to this elsewhere, so if you decide to change the name, make sure to use the new name everywhere else I reference it, or things won’t work
correctly.

Installing Dnsmasq and Hostapd

Dnsmasq - This program has extensive features, but for our purposes we are using it as a DHCP server for our WiFi AP.
Hostapd - This program de nes our AP’s physical operation based on driver con guration.

Installing these is an easy a air:

$ sudo apt-get install dnsmasq hostapd

Wait awhile, and this process should complete. The install process may automatically start the dnsmasq.service in systemd right after installation and it will probably fail, since ap0 does not exist
until we reboot. Disregard this for now.

Next, we need to modify 3 les. First we modify /etc/dnsmasq.conf by adding the following lines at the end of the le:

interface=lo,ap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.10.50,192.168.10.150,12h

Once again, notice we reference ap0 above, so use your device name here if you changed it earlier. I’ve added Google’s DNS server IP here (8.8.8.8) but feel free to use one from your router/ISP/or
whatever. I’ve also made the assumption that our DHCP server will give out addresses on the 192.168.10.0/24 subnet, ranging from .50 to .150. You can substiute your own subnet here, but be sure
to remember it for later, as it should match the static IP we assign your AP. The 12 hour lease time can also be arbitrarily changed to suit your needs.

Next, we need to modify the le at /etc/hostapd/hostapd.conf . I found many di erent parameters that can go in here, but this is what worked for me. Feel free to experiment further by poking
around online. Lets do this for now:

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
interface=ap0
driver=nl80211
ssid=YourApNameHere
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
wpa=2
wpa_passphrase=YourPassPhraseHere
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

A couple things to note here:

1. Replace YourApNameHere and YourPassPhraseHere with the SSID and Passphrase you wish to use.
2. I read multiple sources claiming that the channel you use here must match the channel that your wlan0 iterface is using for its WiFi connection, as reported by iw dev . In my testing, it looks
like the RPi’s AP will dynamically change channels to match whatever channel the wlan0 interface is currently using. I watched this happen in real time by rebooting the WiFi AP the RPi was
using, forcing it to roam and switch to another AP in my house. In the process, wlan0 switched from channel 11 to channel 6, and ap0 did the same, without losing connectivity.

Finally, we modify /etc/default/hostapd like so:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

This tells the hostpad daemon to use our new conf le. (To be honest, I’m not sure if this matters since we will be launching hostapd manually and pointing to the proper con g le, but it shouldn’t
hurt anything.)

Modify Our Interfaces File

Next, we need to de ne our WiFi network interfaces, both for our managed access ( wlan0 ) and for our access point ( ap0 ). We will also use wpa_supplicant to assist with connecting to WPA-
encrypted WiFi networks. If you followed the “headless” bring-up tutorial I mentioned in the prereqs, you will have already touched both of the following les and con gured wlan0 as required. In
that case, we’ll be adding a static IP de nition for ap0 . First, we modify /etc/wpa_supplicant/wpa_supplicant.conf as so:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YourSSID1"
psk="YourPassphrase1"
id_str="AP1"
}

network={
ssid="YourSSID2"
psk="YourPassphrase2"
id_str="AP2"
}

Again, you should replace the SSIDs and Passphrases above with your own. You aren’t required to name multiple SSIDs here, but if you add more, the RPi can roam between networks if con gured
correctly, as we will do below. The id_str eld can be used as a quick reference name in our interfaces le. You should also change the country code to whatever is appropriate for your region.

Next, we modify to support our new AP:


/etc/network/interfaces

https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 2/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd


# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:


source-directory /etc/network/interfaces.d

auto lo
auto ap0
auto wlan0
iface lo inet loopback

allow-hotplug ap0
iface ap0 inet static
address 192.168.10.1
netmask 255.255.255.0
hostapd /etc/hostapd/hostapd.conf

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface AP1 inet dhcp
iface AP2 inet dhcp

As you can see, we want both ap0 and wlan0 to start up automatically, with ap0 de ned to have a static IP of 192.168.10.1 on the 192.168.10.0/24 subnet. Recall, the DHCP address range we
de ned in /etc/dnsmasq.conf matched this subnet. Adjust accordingly if you made changes. We also reference our hostapd con g le here, to direct the AP con guration for ap0 .

For wlan0 , we start it using manual mode and point it to our /etc/wpa_supplicant/wpa_supplicant.conf le for our WiFi network de nitions. The wpa-roam designator will allow the interface to
move freely between our de ned networks. Finally, the last two lines use our friendly names from wpa_supplicant.conf to refer to our available networks, and indicate this interface should use
DHCP supplied by those APs to assign an address to wlan0 . Please note the order here: ap0 must come up before wlan0, or they won’t both work at the same time.

We’re Done! Or Are We…

At this point, I expect everything to work after a reboot. Instead, I end up with wlan0 UP and ap0 DOWN (or vice-versa) when checking via ip addr , and dmesg indicates some errors in the
Broadcom driver. After some tinkering, I discovered the following sequence of commands after a reboot would get both interfaces up and working simultaneously, like we wanted all along:

$ sudo ifdown --force wlan0


$ sudo ifdown --force ap0
$ sudo ifup ap0
$ sudo ifup wlan0

I had to add --force because sometimes hostapd would complain about having a lock on wlan0 and fail to proceed, so it ensures we get what we want. After bringing down both interfaces,
again, with ap0 going rst, followed by wlan0 , they should both come up:

pi@raspberrypi:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:ff:ff:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.43.37/24 brd 192.168.43.255 scope global wlan0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fede:3a79/64 scope link
valid_lft forever preferred_lft forever
3: ap0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:ff:ff:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.10.1/24 brd 192.168.10.255 scope global ap0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fede:3a79/64 scope link
valid_lft forever preferred_lft forever

Great! But what about bridging tra c between my AP and client sides to allow a device to access the internet through my RPi? Let’s do that:

$ sudo sysctl -w net.ipv4.ip_forward=1


$ sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 ! -d 192.168.10.0/24 -j MASQUERADE
$ sudo systemctl restart dnsmasq

Here, we enable ip forwarding to allow packets to be forwarded between interfaces, and we add a postrouting rule to IP tables which routes all packets from the AP-side interface to the client-side
interface (where MASQUERADE is necessary, since the IP is dynamic and unde ned until wlan0 connects to an AP). Finally, we restart dnsmasq for good measure, since we previously brought
down ap0 with ifdown .

At this point, we should be able to connect a device to our Raspberry Pi Zero W’s AP SSID, while we also have our RPi connect to another access point for internet access, and we should be able to
access the internet from that device through our RPi. Only one thing left to do…

Automate The Workaround

Try as I might to debug the issues I was having before manaully reseting the interfaces, I was unable to get the RPi to simply boot up working correctly. So, I decided to script the steps to “ x”
everything and set them to run as a root cron job with a 30s delay… (Yes, I know this is horrible practice, but I’m open to better suggestions.) I intially tried setting up a systemd service to call my
script instead of cron with a delay, but it didn’t seem to work or ran at the wrong time, requiring manual intervention.

Here’s the script in it’s entirety:

pi@raspberrypi:~$ cat ./start-ap-managed-wifi.sh


#!/bin/bash
sleep 30
sudo ifdown --force wlan0 && sudo ifdown --force ap0 && sudo ifup ap0 && sudo ifup wlan0
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 ! -d 192.168.10.0/24 -j MASQUERADE
sudo systemctl restart dnsmasq 
https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 3/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

I simply left the le in my default “pi” user’s home directory, but you can move it someplace more appropriate, such as /sbin . I then added a cron job like so:

$ sudo crontab -e

And in there I added the line:

@reboot /home/pi/start-ap-managed-wifi.sh

This causes the script to run at every reboot. In testing, I foud the RPi Zero W boots in under 20s. Systemd runs the cron service somewhere in that range of time, which eventually runs the script
above. To be safe, you can change the 30s delay to something longer, but I found this to be pretty reliable.

Now, after every reboot, even though ap0 and wlan0 don’t initally play well together, the cron job kicks o my script after 30s and magically xes everything. We have AP and Managed mode
running at the same time, and we’re bridging tra c between the interfaces to allow for internet sharing through the Raspberry Pi Zero W.

Special Thanks

The following links were pretty instrumental in getting things moving in the right direction, so please check them out:

https://raspberrypi.stackexchange.com/questions/63841/rpi-zero-w-as-both-wi -client-and-access-point
RASPBERRY PI 3 - WIFI STATION+AP
Using your Raspberry Pi Zero’s USB wi adapter as both Wi client and access point

Please comment below if this helped you, or if you have any suggestions for improvements!

 Comments  Share

OLDER
Hello

NEWER
Using Esp8266 As An IoT Endpoint With Encrypted MQTT Transport

Featured Comment

Darko Lukic • a year ago


This scripts configures AP + Client Wifi mode (based on the tutorial):
https://gist.github.com/luk...
4△ ▽ • Share ›
Show more replies

124 Comments TheWalrus 


1 Login

 Recommend 8 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Darko Lukic • a year ago


🏆 Featured by TheWalrus
This scripts configures AP + Client Wifi mode (based on the tutorial):
https://gist.github.com/luk...
4△ ▽ • Reply • Share ›

Darko Lukic > Darko Lukic • a year ago


Here is an improved version of the script:
https://github.com/lukicdar...
2△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Darko Lukic • a year ago

This is excellent! Thanks for throwing that together. Anyone who doesn't feel like manually going through the tutorial should be able to give that a run and get it working.
2△ ▽ • Reply • Share ›

Show more replies

nedl • 2 years ago


Thank you for posting this, it was great! I found I had to 'apt-get install rng-tools', otherwise hostapd complained about the entropy pool being empty and would kick clients as soon as
they connected.

I hacked together the following in my /etc/network/interfaces, which seems to replace your udev changes and various startup scripts, and is probably terribly offensive to anyone who
understands raspbian-debian's crazy spaghetti setup of network-interfaces/dhcpcd/wpa_supplicant/wpa_actions/and-probably-some-systemd-thrown-in-too (if there is anyone out there
who actually does):

allow-hotplug wlan0
iface wlan0 inet manual
pre-up ifup ap0
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
pre-down ifdown ap0
iface AP1 inet dhcp
iface AP2 inet dhcp

allow-hotplug ap0
iface ap0 inet static
pre-up iw phy phy0 interface add ap0 type __ap
pre-up ip link set ap0 address b8:27:eb:ff:ff:ff
post-down iw dev ap0 del
address 192.168.100.1
netmask 255.255.255.0
hostapd /etc/hostapd/hostapd.conf

Id 't k if it' b f diff t d i /d d i f th d j tb


see more
it' i t d i l d l b t I fi d it t t t d t j t fi
ith t

4△ ▽ • Reply • Share ›
https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 4/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus
ep y S ae

Muhammet Ilendemli > nedl • 2 years ago


i now have a interfaces file based on your file, i am restarting dnsmasq using post-up of ap0, removed iface AP inet dhcp, all i needed was to get devices connected to the pi
without internet connection, where the pi is connected to a network with internet connection, also disabled dhcpcd
2△ ▽ • Reply • Share ›

BraD Fang > Muhammet Ilendemli • a year ago


Could you share the file for me ? I'm looking for what you done. Thanks
email : [email protected]
△ ▽ • Reply • Share ›

SlowBro > nedl • 2 years ago


nedl unfortunately your instructions do not work for me. My ap0 goes offline.
△ ▽ • Reply • Share ›

Show more replies

SlowBro • a year ago


FYI on Raspbian Stretch I had to also comment out these lines at the bottom of /etc/dnsmasq.conf before adding the entries in the article.

# Delays sending DHCPOFFER and proxydhcp replies for at least the specified number of seconds.
#dhcp-mac=set:client_is_a_pi,B8:27:EB:*:*:*
#dhcp-reply-delay=tag:client_is_a_pi,2

Albert, the instructions and updated recommendations are getting quite fragmented in the comments section. As mentioned elsewhere, please consider either updating your post
(preferable) or creating a new one with the new information gleaned from the comments :-) Updating this one seems a better option since it's one of the first hits on Google for Pi Zero
W access point.
2△ ▽ • Reply • Share ›

SlowBro • 2 years ago


I had an issue in that I neither could ping the wlan0 default gateway nor I get an IP address on wlan0 if I ran the ifdown --force.

Solved it by disabling dhcpcd:


sudo update-rc.d dhcpcd disable
2△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > SlowBro • a year ago


Thanks for that info!
△ ▽ • Reply • Share ›

Aaron • 2 years ago


Instead of a cron job, try adding the commands to /etc/rc.local (chmod it +x, too, check systemd docs on enabling it) and removing the 'auto' lines for ap0 and wlan0 from
/etc/network/interfaces. You'll get a quicker boot-to-usefulness since rc.local is already part of init, and won't have to remember to look for boot config in a weird place like cron. Hope
this helps!
2△ ▽ • Reply • Share ›

SlowBro > Aaron • 2 years ago


Works great for me. Here's my script, called simply /etc/init.d/ap. I chmod +x the script then ran sudo update-rc.d ap defaults. Rebooted and everything is pinging happy without
cron hacks.

#!/bin/bash

### BEGIN INIT INFO


# Provides: ap
# Required-Start:
# Required-Stop:
# Should-Start: $network
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Networking for the access point
# Description: Networking for the access point
### END INIT INFO

ifdown --force wlan0


ifdown --force ap0
ifup ap0
ifup wlan0
3△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Aaron • 2 years ago


For some reason I thought systemd and rc.local were mutually-exclusive (from a philosophical standpoint.) I originally tried to add a systemd service to run my script, but that
didn't seem to run at the right time, leading to both interfaces being down after boot.

Given that using cron is a huge hack anyway, I'll try out your suggestion and see how it goes. Removing 'auto' should also speed up initial boot, but I'll have to make sure that
first connection and subsequent down/up from my script aren't both required to make it work (magically). I haven't had a chance to make a optimization pass yet. Thanks!
△ ▽ • Reply • Share ›

Matt Dunstall > Albert Chaharbakhshi • 2 years ago


Tried the crontab script which didn't work and then rc.local with the full scripts (the down and up commands and ip forward) but also didn't work - received error failed to
start /etc/rc.local compatibility. (I did perform a chmod +x).

I can get the ifdown and ifup statements to work via Slowbro's script as above - but cannot get the IP forwarding to work (disabled auto commands in
/etc/network/interfaces also as described above).

Any ideas? I'm keen for the IP forwarding to work for my project with the PiZW

I'm a bit new to linux and pi's in general so bear with me.
△ ▽ • Reply • Share ›

Show more replies

Camkadev • 4 months ago


Hey

Nice work, it works!

As a workaround i put
sudo ifdown --force wlan0 && sudo ifdown --force ap0 && sudo ifup ap0 && sudo ifup wlan0
at "/etc/rc.local" before "exit" command and after reboot it works great!
did that at image "2018-11-13-raspbian-stretch-lite.img"
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Camkadev • 4 months ago



G t ti ! Th k !
https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 5/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus
Great tip! Thanks!
△ ▽ • Reply • Share ›

Ingo • a year ago


I'm just also working on this issue and stumbled also over "this horrible practice" with repairing the boot setup. So I decided to use a clean systemd setup. With it we can use extended
dependencies to start and execute units. It is a complete different approach and I'm just fighting with the setup of the real layer 2 bridge. But you may have a look at
https://raspberrypi.stackex... for getting some new ideas, e.g. for using a drop-in file for the hostapd unit or editing /etc/init.d/hostapd.
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Ingo • a year ago

Thanks for that detailed post! There might be some useful information there for myself or other readers below.
△ ▽ • Reply • Share ›

Satoshi Sugimura • a year ago


Thank you so much for this post. This was exactly what I was looking for. I did this with Raspberry Pi 3 B+ and worked perfectly. Kudos for you!
1△ ▽ • Reply • Share ›

Thomas Buyukkilic > Satoshi Sugimura • a year ago


Update: never mind, I forgot the "We’re Done! Or Are We…" part. It works now! On a Model B+ btw.
Did you do anything else than the instructions? I followed to the letter, but I don't see the ap0 appearing from other devices. Can you share your settings maybe?
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Satoshi Sugimura • a year ago

Glad to hear this works on RPi 3 B+ as well!


△ ▽ • Reply • Share ›

shomasha • a year ago


Thank you for an excellent post which worked as intended. Like you I went through (& rolled back) so many other approaches so can't tell you how great it was to see this working.
I have a noob question im hoping you can help me with as im learning linux as I go along. If I want to redirect all connected client browser traffic to a specific Pi-hosted webpage, how
would I do that? My Pi address is 192.168.10.1 & I don't need to bridge the connection to external internet at this point.
thank you,
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > shomasha • a year ago


Glad I could help!

If you're already using dnsmasq as per my instructions above, I think this might be a simple config line to do what you want: https://serverfault.com/que...
△ ▽ • Reply • Share ›

Mehmet Sutas • a year ago


Thank you. Excellent work.
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Mehmet Sutas • a year ago


Appreciate it!
△ ▽ • Reply • Share ›

sam • 2 years ago


Did you have to disable the dhcpcd service? My understanding is that Raspbian Stretch uses the dhcpcd service to configure networking by default instead of /etc/network/interfaces.
Thanks for this. I've already bookmarked it.
1△ ▽ • Reply • Share ›

SlowBro > sam • 2 years ago


FYI for those of you who aren't sure how to disable it:
sudo update-rc.d dhcpcd disable
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > sam • 2 years ago


Good question. I did disable it, but only because it was failing to load at startup anyway, so I figured it was unnecessary to keep it enabled. I think the presence of
/etc/network/interfaces might be what actually caused dhcpd to start failing. You're probably right that it should be manually disabled to keep it from interfering.
△ ▽ • Reply • Share ›

Bill Stephenson • 2 years ago


Cool!!! Thank you for sharing and all the hard work you've done. I'm going to try this when I get a chance.
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Bill Stephenson • 2 years ago

No problem! Let me know if it works for you.


△ ▽ • Reply • Share ›

Antonio Dávila • a year ago


Your tutorial was the only one that worked for me every single time, thank you very much!!
1△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Antonio Dávila • a year ago

Glad it helped you out! Thanks!


△ ▽ • Reply • Share ›

JenRellias • 9 days ago


Thanks for the write up. While this worked for me, I wanted to be able to have a generic setup which didn't require hard-coding the MAC into the udev rule. I tried to edit the 70-
persistent-net.rules to include a PROGRAM and use the output %c in place of the MAC address. I couldn't get that to work (knowing next to nothing about writing udev rules).

I finally found in another article from Craig Johnston where he added the ap device in the start script. So I just added the line iw dev wlan0 interface add ap0 type __ap to that script, and
completely removed the udev rule. It seems to work fine, and though I do get a "Failed to start Raise network interfaces" warning at boot. I don't know why, because all the interfaces
are present and working.

Thanks again!
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > JenRellias • 9 days ago

Glad it helped! If you don't mind sharing the link you mentioned, please do, as it will probably be useful to anyone else reading this. Thanks!
△ ▽ • Reply • Share ›

Vin Shahrdar • 22 days ago


Will This work if I want to connect the Pi to two networks simultaneously?

Basically, I want to implement the below architecture with my three raspberry Pis:

Pi 1 joins Pi 2 and Pi 3's networks


Pi 2 joins Pi 1 and Pi 3's networks
Pi 3 joins Pi 2 and Pi 1's networks

https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 6/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

Is something like this possible?


△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Vin Shahrdar • 21 days ago


You could set up each Pi to detect and connect to multiple networks, but not simultaneously (that I know of.) In other words, you could add the SSID for both Pi 2 and Pi 3 to Pi
1's wpa_supplicant.conf file, so that it could connect to either one, but I don't think you could do both at the same time.

Technically, if you configure the network appropriately, you'd only need one Pi to be an AP, and the others could be clients, and they could all talk to each other. Furthermore,
you could configure each one to run as an AP if they're unable to connect to another AP as a fallback option. I assume you're looking for some kind of redundancy?
△ ▽ • Reply • Share ›

Vin Shahrdar > Albert Chaharbakhshi • 21 days ago


Well, the requirement of my project is to have three seperate raspberry pis with three seperate networks that can talk to each other.

According to this thread, if I get USB Wifi adapters, that could work.
△ ▽ • Reply • Share ›

Show more replies

isa_m • 5 months ago


Hey, i followed your tutorial and the script by Darko Lukic but both failed.
The wlan0 interface doesn't work, it doesn't show up in the wifi menu in the gui and doesn't connect to any wifi. but with the script the AP is showing and working correctly but still no
internet connection.
can someone help? i can provide more information if you want to help.

Using the script:


The wifi AP works and allows ssh from the ap
The interface wlan0 doesn't work, it shows up in ifconfig but not the wifi menu in the gui. and it doesn't connect to any wifi networks
△ ▽ • Reply • Share ›

isa_m > isa_m • 5 months ago


can someone help me? most people seems to be able to get it working... and other websites just links this article.... and i cant use the only usb port on the pi zero because i will
because i will be using it for something else...
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > isa_m • 5 months ago


I'm sorry, I haven't done anything with RPi that uses the GUI, so I can't really comment on that. I would suggest trying to get it to work from the command line first, and if
that succeeds, move on to GUI configuration.

Depending on how old your hardware is, it might be worth making sure your RPi is up to date by following the instructions here: https://raspberrypi.stackex...
△ ▽ • Reply • Share ›

Show more replies

Ankit Rana • 6 months ago


i'm trying to make sensor nodes and gateway type mechanism, as shown in below

figure:

see more

△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Ankit Rana • 6 months ago

Cool project! I'm not sure if this project does AP+STA mode, but it provides a GUI for configuring a Raspberry Pi as a router: https://github.com/billz/ra...

My tutorial above should accomplish the same thing, but does not provide a GUI, as it's all config-file-based.
△ ▽ • Reply • Share ›

Ankit Rana > Albert Chaharbakhshi • 6 months ago


yeah the main thing a nice GUi so you can change network configurations.
i think i have to move with espress if ESP32 and fortunately its only take 3 hours.

but below scenario is possible with your project? please give me guidance:
now just consider RASPBERRY PI Zero in AP+STA forget the GUI interface
only one Esp8266 is connected to Rpi's access point and our RpI running TCP server and receives ESP8266'S data without internet?
△ ▽ • Reply • Share ›

Show more replies

Marc Elser • 7 months ago


Did this setup and connected with my laptop to do a speed test which is about 8Mbps down/8 Mbps Up vs 40down/22 Mbit up. when the laptop is directly connected to the wlan. I read
that the Pi Zero W should be able to do up to ~40Mbps, at at least 20Mbps with it's 20 Mhz Chip so 8Mbps is not even half of it? And Idea on how to boost this?
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Marc Elser • 7 months ago


Unfortunately, I don't think there's much you can do. The RPi is doing double-duty as an AP and Client and is linux is responsible for bridging packets between the 2 interfaces. I
think the only solution would be faster hardware.
△ ▽ • Reply • Share ›

Oliver Kurth • 10 months ago


Did you (or anyone) manage to get the same thing working with another USB WiFi device, for example the Alfa AWUS 036nha (which uses the ath9k driver)? I tried the same approach
- the AP works, but the managed device is not able to connect (it gets auth failures). I only tried an encrypted network. The built-in device works great, but the Alfa has more range.
△ ▽ • Reply • Share ›

WimJanse • 10 months ago


Thanks for this excellent description.
For your information, I managed to set this up on a RP3B, did not need the workaround anymore. Just the first part of the above did the job for me. 
Thanks again great
https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 7/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus
Thanks again, great
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > WimJanse • 10 months ago


Glad I could help!
△ ▽ • Reply • Share ›

Tribhuvan Reddy • a year ago


How to set static ip with managed network wlan0 while acting as ap+station?
△ ▽ • Reply • Share ›

Albert Chaharbakhshi Mod > Tribhuvan Reddy • a year ago


I don't understand the question. Which device's IP are you trying to make static? Something connected to the RPi AP or the RPi's own IP connected to another access point?
△ ▽ • Reply • Share ›

Load more comments

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy

RECENTS

HOW-TO
USING ESP8266 AS AN IOT ENDPOINT WITH ENCRYPTED MQTT TRANSPORT
2019-03-27

HOW-TO
RASPBERRY PI ZERO W SIMULTANEOUS AP AND MANAGED MODE WIFI

https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 8/9
06/06/2019 Raspberry Pi Zero W Simultaneous AP and Managed Mode Wifi · TheWalrus

2017-09-26

BLOG
HELLO
2016-05-26

CATEGORIES

 blog (1)

 how-to (2)

TAGS

 arduino (1)

 certs (1)

 esp8266 (1)

 hello (1)

 how-to (2)

 mqtt (1)

 raspberry-pi (1)

TAG CLOUD

arduino certs esp8266 hello how-to mqtt raspberry-pi

© 2019 Powered by Hugo. Theme by PPO ce.


https://blog.thewalr.us/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ 9/9

You might also like