The Raspberry Pi Zero W is a great platform for IoT projects, with a smattering of GPIO and onboard WiFi. However, security is an important consideration when it comes to the Internet of Things and it can be beneficial to keep your IoT devices on a separate network for safety’s sake. [Albert] wanted to do this all on board the Pi Zero W, and figured out how to get it acting as an access point and a client all at the same time.
[Albert] starts off with a fresh install of Raspbian Stretch, and sets the Pi up in OTG mode. This allows access to the Pi over a USB serial terminal. This is great for productivity when working on headless networking projects, as it can be frustrating trying to work with an SSH session that keeps dropping out when you change settings.
After creating a second named device (ap0) to go along with the one created automatically by the kernal (wlan0), DNSmasq is installed to act as a DHCP server for the AP. Hostapd is then installed to control the AP settings. Following this, like anything in Linux, a flurry of configuration files are edited to get everything humming along and starting up automatically after a reboot. For some reason, things don’t start up smoothly, so [Albert] has a cron job that fires 30 seconds after bootup and toggles the interfaces off and on again, and that’s done the trick.
It’s a useful hack, as it allows the Pi Zero to act as a hub for IoT devices, while also creating a bridge between them and the internet. Traffic can be managed to stop random internet users flicking your lights on and off and overspeeding your dishwasher.
We’ve seen the Pi Zero used for just about everything under the sun so far. If you’re just starting your own IoT build, perhaps you’d like to use the Pi Zero as a streaming camera?
34 thoughts on “Simultaneous AP & Client On The Pi Zero W”
You ever tried 802.1Q over 802.11?
I imagine this is the one forum on the planet that would make it work. ;-)
Huh, neat ????
Sounds more like bridging data from particular SSIDs to particular 802.1Q VLANs, something my access point does out of the box, but not exactly what I’d call 802.1Q over 802.11.
In the same SSID you can assign clients different VLANs based on authentication (almost every mid to large enterprise does it this way since it’s easy to manager) or if you really want transmit multiple tagged VLANs on a single SSID or to a single client on a single SSID. 802.11 actually has a much large MTU than standard Ethernet so you can get away with a lot. You can also use a single VLAN/SSID in private mode if you don’t need direct peer to peer and want to use security lists for every flow.
Author of the linked article here. While I appreciate the comedy of the snarky comments above, the purpose of my original post was due to the lack of information out there in getting Host+AP mode working properly on a Raspberry Pi Zero W, specifically. There are several existing tutorials out there, all of which have been lamented for not working for most people (myself included), and after figuring out a method that DID work, I decided to post/share it for the benefit of everyone else who had issues making it work as I did.
This place is really getting to have some cynical snarky jerks. I don’t really care is something is or is not a hack, So long as it can somehow be related to my hobby I am happy.
Your article is great, having a working Wi-Fi as both AP and Client is awesome and I can’t wait to mess around with it.
Appreciate it. I was a little reticent to submit this to Hackaday, because it’s not a “hack” as you said, but there are plenty of RPi articles on here and knowing how easy it is to set up AP+Client mode on an ESP8266 compared to the PITA it turned out to be on the RPi, it seemed like information that might be useful to people.
I appreciate the Hacakady blog commentary above delving into uses for this mode, but the primary purpose of my actual post was not that, it was just getting it working, which is harder than it should be, despite my having Linux knowledge and being an embedded programmer by trade.
It’s a good article, albeec13. Anyone who uses Mac or Windows all the time would likely struggle with setting this up, and you have saved these people a lot of trouble.
I think the snark may be due to the popularity of Linux as the fundamental computing base of many HoD readers. AP/client setup is something many people just experiment with out of boredom or to do an emergency caffeine burn, probably while being chewed out for not paying attention during staff meetings. Doing the same thing on an RP0W is no different than doing it on a laptop, so they’re wondering why someone would bother to document it. It’s really just the other side of a coin: excitement on one side, eyerolls on the other.
Don’t worry, the standards for those on a spectrum will never be satisfied.
It’s easy for people to overlook that not everyone has their reserves of knowledge. Sure, I may be able to figure out what needs to be changed from a basic Debian tutorial, but if I didn’t know WHAT to punch into Google… well… I’d be stuck. Where are the logs that tell you what failed to come up? That’s even if you know that you should look for logs in the first place.
I’m sincerely applauding you for writing this up and sharing it. Others should stop complaining about a story on a free website that nobody forced them to read or review. I don’t read everything on here, that’s the point of having summaries.
But, let’s not lose our sense of humor, either. Good snark is indicative of an intelligent audience.
Thanks. I pretty much ran into just that. I saw a lot of people having problems getting this to work, and the only responses linked to 2-3 other posts that claimed to get it working, but they didn’t work for me nor for the people asking. I dug in and made it work, and thought it would be helpful to share the information since it appeared to be a popular problem.
I think the above posters spelled “thank you” incorrectly.
As someone greater before me said: ONE HACK PER DAY, ‘HACK A DAY’.
This is just advertising RPi x, Arduino Article coming up in the next few hours.
I highly doubt the Raspberry Pi needs free advertising at this point.
I’ve considered building a portable WiFi AP/bridge out of a Pi Zero W by combining the built in WiFi and a WiFi USB adapter, powered by a LiPo battery, bookmarking this article for future reference.
And to head off the loony conspiracy theorist “RPi Zero is a scam/vaporware that doesn’t really exist because I can’t figure out how to use a web browser” trolls: in the US you can get a Zero W for $12 shipped without having to buy a pesky kit. (No I’m not affiliated, just happily sharing a reputable source with sane shipping (looking at you, Adafruit)).
I just walked into my local electronics store and picked up a Zero W for $5 on sale – no kit.
Really wish they didn’t up the price if you buy a few at a time though.
10 Walk in
20 Buy one Zero W
30 Walk out
40 If wallet not empty Goto 10, else Go Home
I got two this way as well. Luckily my sister lives near one Microcenter and was able to grab one for me, and I picked one up at a closer store. At $5 apiece, it’s a no-brainer.
I live close enough to a Microcenter to do the same. My post was referring to the trolls who tend to drop in to any Pi Zero article complaining that they physically do not exist and are a MLM scam by the Foundation because they can be difficult to source online at times. The cognitive dissonance displayed by those people is baffling.
Not a hack, but just what I needed to jumpstart my own “not a hack”
My exact thought. Hostapd is old trick. But still nice though
This is the standard Linux way to use virtual interfaces. Should work on any platform as long as the driver and WiFi chip support it.
Works like a charm! Extended it a bit by installing OpenVPN client connecting with the free ProtonVPN plan and now routing all connected clients directly through it. Perfect for securely using insecure wifi networks. Also installed Lighttpd on the Pi Zero and created a basic web interface so you can connect to a WiFi network of your choice from the browser of a connected client.
there’s an *excellent* script available on github that, with minor mods, works on Raspbian Buster (Dec 2019). see https://github.com/lukicdarkoo/rpi-wifi. the mods:
next, insert `#!/bin/bash` as below:
# Populate `/bin/start_wifi.sh
sudo bash -c ‘cat > /bin/rpi-wifi.sh’ << EOF
echo 'Starting Wifi AP and client…'
Please be kind and respectful to help make the comments section excellent. (Comment Policy)