coopi
Posts: 24
Joined: Sun Apr 16, 2017 3:46 pm

How-to: AP+wifi client on Zero W with Stretch Lite

Fri Oct 27, 2017 6:34 am

Just re-test it on a fresh raspbian installation today.

Different people like to handle stretch's /etc/network/interfaces(.d) in different way, not sure where is more "forgiveness" to change, I can only tell that the following combination works. Please feel free to share your experience.

I use serial console, no usb dongle is plugged. Before apt update, which need internet access, I edit /etc/network/interfaces

Code: Select all

# 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 wlan0
iface wlan0 inet dhcp
  wpa-ssid "YOURSSID"
  wpa-psk "YOURPSK"
To avoid struggling with wpa-supplicant, reboot is a clean and easy way.
Reboot.

You should now have internet access to do apt update & upgrade. Note at this stage that
# systemctl status networking is clean
# systemctl status dhcpcd is failed ALREADY AS ALWAYS
Install hostapd and dnsmasq after apt update & upgrade successfully

Code: Select all

$ sudo apt-get install hostapd dnsmasq

edit /etc/dnsmasq.conf, append to the bottom

Code: Select all

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

edit /etc/dhcpcd.conf
This is to enable ssh in thru AP's ip instead of wifi client's ip. BTW, bluetooth console could be a better choice after all.

Code: Select all

interface uap0

create /usr/local/bin/hostapdstart
amend yourAPssid & yourAPpsk inside the script accordingly

Code: Select all

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
iw dev wlan0 interface add uap0 type __ap
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
# write the first part of the conf file
echo -en 'interface=uap0\nssid=yourAPssid\ndriver=nl80211\nhw_mode=g\nieee80211n=1\nchannel=' > /etc/hostapd/hostapd.conf
iwlist channel 2> /dev/null | awk '/Current/ {print substr($5,1,length($5) - 1)}' >> /etc/hostapd/hostapd.conf
echo -e 'macaddr_acl=0\nauth_algs=1\nignore_broadcast_ssid=0\nwpa=2\nctrl_interface=/var/run/hostapd\nctrl_interface_group=0' >> /etc/hostapd/hostapd.conf
echo -e 'wpa_passphrase=yourAPpsk\nwpa_key_mgmt=WPA-PSK\nrsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
ifdown wlan0
ip link set dev uap0 up
ip addr add 192.168.50.1/24 broadcast 192.168.50.255 dev uap0
sleep 1
# start hostapd
hostapd -B -P /run/hostapd.pid /etc/hostapd/hostapd.conf &
sleep 1
service hostapd restart
ifup wlan0
service dnsmasq restart


insert the following line into /etc/rc.local before the exit 0

Code: Select all

/bin/bash /usr/local/bin/hostapdstart

Reboot

Enjoy!

SurferTim
Posts: 1764
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Fri Oct 27, 2017 2:01 pm

Thanks for this! I found some of your setup works well. Other parts are unnecessary, like creating the hostapd.conf file on the fly. This is what I used on my Zero W that boots correctly with Stretch Lite 2017-09-07. Edit: Also works on my RPi3.

Add this to /etc/network/interfaces

Code: Select all

auto wlan0
iface wlan0 inet dhcp
	wpa-ssid "MySSID"
	wpa-psk "MyPassphrase"
Reboot. This will cause the dhcpcd service to fail.
Edit: I think the dhcpcd service may be the cause of my original setup to fail.

Insure your OS is up to date.

Code: Select all

sudo apt-get update
sudo apt-get upgrade
Install the modules.

Code: Select all

sudo apt-get install dnsmasq hostapd
Edit /etc/rc.local and add this above "exit 0". Note that is two underscores on __ap.

Code: Select all

service hostapd stop
service dnsmasq stop
iw dev wlan0 interface add uap0 type __ap
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
ifdown wlan0
ip link set uap0 up
ip addr add 192.168.4.1/24 broadcast 192.168.4.255 dev uap0
service hostapd start
ifup wlan0
service dnsmasq start
The /etc/dnsmasq.conf file

Code: Select all

interface=uap0
dhcp-range=192.168.4.20,192.168.4.100,255.255.255.0,12h
The /etc/hostapd/hostapd.conf file. If you change the wpa_passphrase, it must be 8 to 63 characters. No shorter or longer!

Code: Select all

interface=uap0
driver=nl80211
ssid=RPiNet
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=mypassphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Edit /etc/default/hostapd, uncomment and change this:

Code: Select all

DAEMON_CONF="/etc/hostapd/hostapd.conf"
Edit /etc/sysctl.conf and uncomment

Code: Select all

net.ipv4.ip_forward=1
Reboot.
.

coopi
Posts: 24
Joined: Sun Apr 16, 2017 3:46 pm

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Fri Oct 27, 2017 4:10 pm

Thanks SurferTim, Tested and confirm that channel# (of hostapd) will now switch and match automatically no matter where you go. I believe it is an improvement made on Stretch.

About the dhcpcd service, as it is doing its work anyway, I guess there is some minor bug on "/etc/network/interfaces(.d)".
The /etc/network/interfaces file is very powerful. If I don't get it wrong, starting from Stretch, it is officially advised to leave this file alone and, instead, add conf files into the /etc/network/interfaces.d folder. I do agree it is a good move. Just timing-wise, now is not mature enough yet and making it a cost to use Stretch.

About the script, it really depends on one's usage.
It is also the first time I handle conf file this way, and I tends to like it. It allows me to do less HardCode (e.g. even iptables, sysctl) on conf files and makes it easier to manage how you are going to change the role of your RPi on the road. Have fun.

pentagolo
Posts: 1
Joined: Mon Dec 04, 2017 10:02 am

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Mon Dec 04, 2017 11:25 am

Thank you for this.

I need the access point to be setup correctly, even if the initial client setup fails. Therefore i need to understand how and why this works. Here are my assumptions by looking at your setup. Please correct me if i am wrong.

We need to get the channel used by the client setup before we startup hostapd. In the post of SurferTim this seems to be done when

Code: Select all

iw dev wlan0 interface add uap0 type __ap
is executed. The uap0 interface is created with the channel forced to be the same as the one used by wlan0 at this time. So later hostapd will ignore the channel specified in hostapd.conf.

It seems like hostapd needs to run before the client setup is done. Otherwise the two solutions would not need to shutdown the wlan0 interface before hostapd is started.

Using SurferTim's code, and the initial wifi connection could not be established (target router is not running, wrong SSID configured) the access point is not setup correctly. Is this because

Code: Select all

iw dev wlan0 interface add uap0 type __ap
fails, if wlan0 is not setup at this moment?

When hostapd is setup correctly and i afterwards try to setup wlan0 to use a network running on another channel, the client setup will fail, but the access point will still work as expected. So the configuration (channel) of the access point will not change when the client setup changes once uap0 is created.

Both solutions seem to have hostapd as well as dnsmasq services ebabled. But the configured device uap0 does not exist until /etc/rc.local is executed. So they could have been disabled as well.

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 13, 2017 6:13 pm

Thanks from me also. I'm using @SurferTim's version and it works OK.. but my situation is slightly different. I want the RPi zero to act as a wifi hotspot running a local server (I'm using Flask) to feed physical monitoring info back to A.N.Other's mobile phone. I really don't want the RPi to be able to access the internet apart from while setting up or to help with debugging.

If I take the relevant lines out of /etc/network/interfaces I can't connect to the RPi (and I don't get the dhcpcd ERROR on start up) If I change the wpa-ssid and wpa-psk to invalid values then it connects and serves http but it spends ages booting. Does anyone know of a simple way to toggle the connection to the internet off allowing quick booting (to save me spending hours reading documentation for iptables, hostapd, dnsmasq etc)?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

SurferTim
Posts: 1764
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 13, 2017 6:19 pm

If I understand your goal, you want just a standalone wifi AP with a web server on the RPi? The first example here will do that. Stop when you come to the "Add routing and masquerade" part.
https://github.com/SurferTim/documentat ... s-point.md

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 13, 2017 7:41 pm

@SurferTim Thanks, yes that's what I want, and in fact it does work really well as it is... apart from booting up stops for a long time at

Started hostname services..
Start job is running...

...then

..DHCPDISCOVER on wlan0... interval 5.. 9..17..21 etc at the end.

w.r.t. your instructions above (which is what I have used) I recommented out the line in /etc/sysctl.conf
#net.ipv4.ip_forward=1
and /etc/rc.local
#iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

and sure enough it won't connect to the internet even if I put in the correct details in /etc/network/interfaces but it still takes just as long to boot up.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

SurferTim
Posts: 1764
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 13, 2017 11:19 pm

It does take a while to start, and a bit longer before you can connect anything to the AP.

User avatar
paddyg
Posts: 2227
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 13, 2017 11:24 pm

Cheers, I can live with this. It's not bad really compared with a 'big' computer start up time. I think I must have been spoiled doing the initial testing on my RPi 3; I could fiddle around then reboot and it only seemed to take a few seconds! Thanks again for your detailed instructions.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

mattdaus
Posts: 3
Joined: Wed Dec 27, 2017 12:15 am

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 27, 2017 2:54 am

I've managed to get surfer Tim's method to boot in approx 30-40seconds as apposed to an original 2mins 30 seconds by chopping out the addition to rc.local and adding it to it's own script file in root.

I then add the script to the bottom of .bashrc

I added sudo to each line also, as it it was having permission issues running the commands.

So I've got to thank you guys working on this - it's been a life saver for my little project! But, any ideas as to why this kills eth0?

-edit-- sorry I had a mental block re eth0.. (I'm doing this on a Pi3) - just had to add it back into /etc/network/interfaces alongside the other setup for wlan0

SurferTim
Posts: 1764
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Dec 27, 2017 12:11 pm

mattdaus wrote: So I've got to thank you guys working on this - it's been a life saver for my little project! But, any ideas as to why this kills eth0?

-edit-- sorry I had a mental block re eth0.. (I'm doing this on a Pi3) - just had to add it back into /etc/network/interfaces alongside the other setup for wlan0
I'm not sure I understand. Are you using an access point and a wifi client on the same interface (wlan0)? What purpose does eth0 serve?

BTW, the RPi3 doesn't require an entry in /etc/network/interfaces for this type setup.

mattdaus
Posts: 3
Joined: Wed Dec 27, 2017 12:15 am

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Thu Dec 28, 2017 6:07 am

I'm pretty new to Pis and linux in general but I'm bashing my way through with guides etc.

Basically, I am trying to set up an image that runs host software that can go with a printer I made that I move around with a lot, putting it on new wifi networks a lot. The SD card is inaccessible inside the guts of the thing once it's all together.

So when I take it to a new place with a Wifi network, or ethernet connection - I can:

1. access the AP and tell it the new WiFi network details via the web interface running, or
2. connect via the ethernet either permanently then tell the Pi the WiFi details (uPnP is running so I can just easily access it via that without knowing the IP address if I choose to just use ethernet).

So that's why I wanted eth0, wlan0 and uap0 all to play nice :) So when first run, /etc/network/interfaces won't have any SSIDs to connect to, so I just leave the details out.

pmgomes
Posts: 1
Joined: Sat Jan 13, 2018 10:55 pm

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Sat Jan 13, 2018 11:02 pm

Hello,
Thanks for the configurations!

I tried both of them and they work properly for browsing with good internet speed.

Despite of this, I am struggling with Whatsapp as it take time to deliver messages after a time of inactivity of 30-60 seconds of inactivity. It happens both on Android and iPhone versions of the application, and on the former it actually goes to the state of "connecting" to the server when messages are not delivered. It goes to "connecting" like if it the connection was flapping.

I also tried to configure the wifi extender using the combination eth0+wlan0 and this behavious does not happen.

Anybody knows if there's any limitation when using uap0?


Many thanks,
Pedro Gomes.

PalfiZsolt
Posts: 7
Joined: Tue Feb 06, 2018 12:26 am

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Tue Feb 20, 2018 5:33 pm

Hi,

This solution works for me also on the latest official Raspbian lite package (Version:November 2017, Release date:2017-11-29). However, I always receive failed status for dhcpcd at boot process:

Feb 20 17:27:58 raspberrypi dhcpcd[221]: Not running dhcpcd because /etc/network/interfaces
Feb 20 17:27:58 raspberrypi dhcpcd[221]: defines some interfaces that will use a
Feb 20 17:27:58 raspberrypi dhcpcd[221]: DHCP client or static address

So I disabled dhcpcd by the sudo systemctl disable dhcpcd.service command. Everything is fine now, but I don't understand why. If there is no dhcpcd, who will talk with the dhcp server to get the IP? Who does dhcpcd's job, if it is missing?

Thanks.

Pizwuser
Posts: 1
Joined: Thu May 24, 2018 10:08 am

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Thu May 24, 2018 10:28 am

I tried both SurferTim and coopi ways. Unfortunately my pi zero w didn't change channel with SurferTim's way. Only coopi's way is work for me. I had no idea why? I am using diertpi.

I have questions for uap0 and wlan0 for my case. Both up and connectable. It is very good. If wlan0 disconnected for some reason, uap0 is still working.

Only problem is wlan0 can't reconnect automatically or triggered by ifup, ifdown for both wlan0 and uap0. No matter what I did , uap0 still up and wlan can't reconnect. Only way is to reboot.

Any way that I can reconnect wlan0 automatically in this case. I am using coopi'way.

Thanks in advance for your assistance.

cecchisandrone
Posts: 3
Joined: Tue Dec 31, 2013 1:04 pm

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Sat Aug 04, 2018 1:51 pm

Guys, do you have any idea why, randomly, the devices connected to the AP cannot ping other devices neither the pi. I discovered with tcpdump that this is due to pi not forwarding arp requests. If I add manually arp entries, devices can ping. Even the pi itself has the following strange entries in the arp table:

Code: Select all

? (192.168.1.25) at <incomplete> on uap0

chrispazz
Posts: 5
Joined: Sun Sep 09, 2018 7:13 pm

Re: How-to: AP+wifi client on Zero W with Stretch Lite

Wed Sep 19, 2018 8:10 pm

Hello,
my PI Zero W is currently initiating at boot a ppp0 connection to internet with a 3G USB dongle and connecting as WIFI client to my WIFI router.

With this method will I be able to let it act as an access point router using the 3G dongle to access the internet instead of wifi client net?

Return to “Networking and servers”