haru-pi
Posts: 1
Joined: Fri Apr 20, 2018 1:28 pm

Raspberry pi 3 B+ as Access Point and Wifi client

Fri Apr 20, 2018 1:34 pm

Hello,

I'm trying to set up my raspberry pi to work as AP and wifi client at the same time.
I tried this tutorial https://pifi.imti.co/ but it's not really working for me. When I start the docker container I don't see the SSID. Also, I'm not using any Ethernet cables or anything like that as I don't want to use it. Do you have any suggestions on how I should do that?

Thanks in advance! : )

epoch1970
Posts: 3665
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Sat Apr 21, 2018 1:59 pm

If you peek into IOT wifi's Github tree you can see the (golang) commands the container uses to instanciate the virtual AP.
These are the same commands you can use on the command-line in a non-containerized OS.

I would suggest you stop Docker and try creating the virtual AP from the native OS. There is a least one (long) thread about this in the forums here.
If you can manage to get uap0 and wlan0, then the problem is on the Docker or container side.
If creating the uap0 interface manually fails, make sure you are running the latest version of Raspbian (3B+ is recent and network hardware was upgraded) and try again.

Before all that however. In my experience it is not possible to create the uap interface if the wifi client interface is already active. You say you're not using ethernet, make sure the Pi has no networking active at all when you start the container from the console.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

damonh
Posts: 28
Joined: Tue Aug 15, 2017 9:31 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Tue May 01, 2018 7:46 pm

So far my experience with trying to do this on Stretch and 3B has been less than stellar.
Sometimes it would work, more often than not it wouldn't. I have resolved by adding a second wifi dongal by CanaKIT. Seems to be working steadily.

I posted my setup files here: https://lb.raspberrypi.org/forums/viewt ... 3&start=75

pugbot
Posts: 4
Joined: Thu May 31, 2018 10:16 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Tue Aug 21, 2018 12:42 am

Okay, here is the method I used that worked for me. It is based on IOT wifi's solution, but I wanted to use a language other than Go to manage my wifi connections, so all changes are within the standard Raspbian Stretch OS.

These steps are (as best as I can remember) in the order that I did them in:

1. Update system
Run apt-get update and upgrade to make sure you have the latest and greatest.

Code: Select all

sudo apt-get update
sudo apt-get upgrade
This took over an hour on my Pi 3B+ with my 20Mbps connection.

2. Install hostapd and dnsmasq
Install the hostapd access point daemon and the dnsmasq dhcp service.

Code: Select all

sudo apt-get install hostapd dnsmasq
3. Edit configuration files
Here we need to edit the config files for dhcpcd, hostapd, and dnsmasq so that they all play nice together. We do NOT, as in past implementations, make any edits to the /etc/network/interfaces file, since this can cause problems, per tutorial notes here: https://raspberrypi.stackexchange.com/q ... 7921#37921

Edit /etc/dhcpcd.conf

Code: Select all

interface uap0
	static ip_address=192.168.50.1/24
        nohook wpa_supplicant
This sets up a static IP address on the uap0 interface that we will set up in the startup script. The nohook line prevents the 10-wpa-supplicant hook from running wpa-supplicant on this interface.

Replace /etc/dnsmasq.conf
Move the dnsmasq original file to save a copy of the quite useful example, you may even want to use some of the RPi-specific lines at the end. I did not test my solution with those.

Code: Select all

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
Create a new /etc/dnsmasq.conf and add the following to it:

Code: Select all

interface=lo,uap0               #Use interfaces lo and uap0
bind-interfaces                 #Bind to the interfaces
server=8.8.8.8                  #Forward DNS requests to Google DNS
domain-needed                   #Don't forward short names
bogus-priv                      #Never forward addresses in the non-routed address spaces
# Assign IP addresses between 192.168.70.50 and 192.168.70.150 with a 12-hour lease time
dhcp-range=192.168.70.50,192.168.70.150,12h
The IP address range here is totally arbitrary; use your own.

Create file /etc/hostapd/hostapd.conf and add the following:
(Feel free to delete the commented out lines)

Code: Select all

# Set the channel (frequency) of the host access point
channel=1
# Set the SSID broadcast by your access point (replace with your own, of course)
ssid=yourSSIDhere
# This sets the passphrase for your access point (again, use your own)
wpa_passphrase=passwordBetween8and64charactersLong
# This is the name of the WiFi interface we configured above
interface=uap0
# Use the 2.4GHz band (I think you can use in ag mode to get the 5GHz band as well, but I have not tested this yet)
hw_mode=g
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
driver=nl80211
# I commented out the lines below in my implementation, but I kept them here for reference.
# Enable WMM
#wmm_enabled=1
# Enable 40MHz channels with 20ns guard interval
#ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
Note: The channel written here MUST match the channel of the wifi that you connect to in client mode (via wpa-supplicant). If the channels for your AP and STA mode services do not match, then one or both of them will not run. This is because there is only one physical antenna. It cannot cover two channels at once.

Edit file /etc/default/hostapd and add the following over the #DAEMON_CONF line:

Code: Select all

DAEMON_CONF="/etc/hostapd/hostapd.conf"
4. Create startup script
Add a new file /usr/local/bin/wifistart (or whatever name you like best), and add the following to it:

Code: Select all

#!/bin/bash

# Redundant stops to make sure services are not running
echo "Stopping network services (if running)..."
systemctl stop hostapd.service
systemctl stop dnsmasq.service
systemctl stop dhcpcd.service

#Make sure no uap0 interface exists (this generates an error; we could probably use an if statement to check if it exists first)
echo "Removing uap0 interface..."
iw dev uap0 del

#Add uap0 interface (this is dependent on the wireless interface being called wlan0, which it may not be in Stretch)
echo "Adding uap0 interface..."
iw dev wlan0 interface add uap0 type __ap

#Modify iptables (these can probably be saved using iptables-persistent if desired)
echo "IPV4 forwarding: setting..."
sysctl net.ipv4.ip_forward=1
echo "Editing IP tables..."
iptables -t nat -A POSTROUTING -s 192.168.70.0/24 ! -d 192.168.70.0/24 -j MASQUERADE

# Bring up uap0 interface. Commented out line may be a possible alternative to using dhcpcd.conf to set up the IP address.
#ifconfig uap0 192.168.70.1 netmask 255.255.255.0 broadcast 192.168.70.255
ifconfig uap0 up

# Start hostapd. 10-second sleep avoids some race condition, apparently. It may not need to be that long. (?) 
echo "Starting hostapd service..."
systemctl start hostapd.service
sleep 10

#Start dhcpcd. Again, a 5-second sleep
echo "Starting dhcpcd service..."
systemctl start dhcpcd.service
sleep 5

echo "Starting dnsmasq service..."
systemctl start dnsmasq.service
echo "wifistart DONE"
There are other and better ways of automating this startup process, which I adapted from IOT wifi's code here: https://github.com/cjimti/iotwifi This demonstrates the basic functionality in a simple script.

5. Edit rc.local system script
There are other ways of doing this, including creating a daemon that can be used by systemctl, which I would recommend doing if you want something that will restart if it fails. Adafruit has a simple write-up on that here: https://learn.adafruit.com/running-prog ... -a-service. I used rc.local for simplicity here.

Add the following to your /etc/rc.local script above the exit 0 line (note the spacing between "/bin/bash" and "/usr/local/bin/wifistart"):

Code: Select all

/bin/bash /usr/local/bin/wifistart
6. Disable regular network services
The wifistart script handles starting up network services in a certain order and time frame. Disabling them here makes sure things are not run at system startup.

Code: Select all

sudo systemctl stop hostapd
sudo systemctl stop dnsmasq
sudo systemctl stop dhcpcd
sudo systemctl disable hostapd
sudo systemctl disable dnsmasq
sudo systemctl disable dhcpcd
7. Reboot

Code: Select all

sudo reboot
If you want to test the code directly and view the output, just run

Code: Select all

sudo /usr/local/bin/wifistart
from the terminal after commenting out the wifistart script line in rc.local.

Let me know if you have improvements or if this doesn't work for you. Cheers!
Last edited by pugbot on Fri Feb 15, 2019 4:32 am, edited 1 time in total.

pieyeguy
Posts: 1
Joined: Tue Jan 29, 2019 1:10 am

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Tue Jan 29, 2019 1:39 am

Like so many others I wanted use my pi 3B as both a wireless access point and wireless client using just the onboard wifi chip. I originally was following the IMTI Docker install https://imti.co/iot-wifi/#legacy-instru ... manual-way with limited success. I also tried the old manual way listed on the same blog with limited success. I eventually was able to get both the AP and WiFi working using a combination Pugbot's post and the original IMTI post.

As Pugbot mentioned in his post, the Interfaces file seems to be deprecated in Stretch. No changes are necessary to it. In it's place make the changes to the DHCPCD.conf as he described.

After that you can use either Pugbot's instructions or the IMTI Manual Setup instructions. I chose the later since they were a little cleaner without all of the service starts and stops in Pugbot's.

Now the key point using either Pugbot's instructions or the IMTI instructions. The DHCP range in dnsmasq.conf needs to include the static IP of the AP! In other words if the AP is 192.168.50.1 the DHCP range should be 192.168.50.1- 192.168.50.xxx. Without this the AP SSID broadcasts but the DHCP server would not hand out an IP address to the connecting client.

In Pugbot's instructions his static IP was 192.168.50.1 but his DHCP range was 192.168.70.50 through 70.150. In IMTI instructions the static IP was 192.168.50.1 but the DHCP range was 192.168.50.50 through 50.150. In both cases this was non-inclusive of the static IP of the AP.

Hope this helps.

davethomaspilot
Posts: 104
Joined: Tue Apr 29, 2014 6:18 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Tue Feb 12, 2019 3:40 pm

Thanks for this info!

I followed pieyeguy's advice and expanded the dhcp address range to include 192.168.50.1.

I can wifi connect to the hotspot and then ssh and/or vnc connect to 192.168.50.1. The pi also connects as a client to my wifi router. Everything works!

But, it doesn't seem reliable. Eventually, I can not longer communicate over the hotspot. It stays connected, but the ssh session and/or vnc session become non-responsive.

Sometimes, just wifi disconnecting and reconnecting solves the problem. But right now, it doesn't

I noticed that while I have hostapd.conf set up for channel 10 (that's what I initially saw when I used

Code: Select all

iwlist wlan0 channel 
windows is reporting that I"m connected to channel 9. So, I checked again using iwlist and see that the client is actually using channel 9. It connected to another wifi router I have running. Maybe it worked for a while with the hotspot on 9 and the client on 10, but not reliably.

I'm thinking that might be the issue. I can remove all but one entry in my wpa_supplicant.conf file so that the pi will always connect to the same router. I'll try this and see if it resolves the issue.

But, that leaves me with a couple of questions...

If the pi can't connect to the specified SSID (say I'm at a different location), will the wifi hotspot sitll work and use the channel specified in hostapd.conf?

Would it be possible to implement a script that would first attempt to connect as a client using wpa_supplicant.conf (with several SSIDs). If a good connection is made, then use the channel found from iwlist as the channel to use for the hotspot? If a client connection isn't made, then use a specified channel.

I'd prefer the specified channel be used for the hotspot. If the client can't connect to an SSID on this channel, I could still connect reliably to the hotspot. Is this achievable? (always use channel in hostapd.conf for hotspot regardless of client connection).

Thanks!

Facelook
Posts: 1
Joined: Wed Feb 13, 2019 2:30 am

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Wed Feb 13, 2019 2:34 am

Following pugbot's post, I was able to share the WiFi as hotspot and connect to it. However, I was not able to access the Internet via it. Any suggestions? Thanks.

eugen2009
Posts: 1
Joined: Wed Feb 13, 2019 3:35 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Wed Feb 13, 2019 4:30 pm

Following Pieyeguy's post I had some inexplicable problems.
After some try's with limited success I switched to following only pugbot's post and it works very well.

In the step 5. there is an char mistake, instead of"/bin/bash/ /usr/local/bin/wifistart" it should be "/bin/bash /usr/local/bin/wifistart".

I am able to share the WiFi as hotspot and connect to it too.
While RPI has Ethernet communication:
- possible to surf on RPI in the internet.
- possible to surf on the devices which are connected to RPI hotspot.

While RPI is connected through WiFi:
- it is possible to surf on RPI
- the devices could be connected to RPI hotspot
- not possible to surf on the devices which are connected to RPI hotspot.

I suspect it could be enough for my application. Maybe configuration of bridge=br0 could help to surf while RPI is connected through Wifi.

rudiratlos
Posts: 137
Joined: Tue May 01, 2012 8:47 am

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Thu Feb 14, 2019 1:45 pm

pls. look at:
https://github.com/rudiratlos/hotspot
you will find here a full blown hotspot script.

davethomaspilot
Posts: 104
Joined: Tue Apr 29, 2014 6:18 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Thu Feb 14, 2019 4:26 pm

Will this support simultaneous use of hotspot and wifi client?

Some posts say you must choose a hotspot frequency that's the same as what the client uses. But, I think I read your script uses one based on the least congesting frequency band?

Does that impact the reliability of the client connection?

rudiratlos
Posts: 137
Joined: Tue May 01, 2012 8:47 am

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Thu Feb 14, 2019 4:55 pm

davethomaspilot wrote:
Thu Feb 14, 2019 4:26 pm
Will this support simultaneous use of hotspot and wifi client?

Some posts say you must choose a hotspot frequency that's the same as what the client uses. But, I think I read your script uses one based on the least congesting frequency band?

Does that impact the reliability of the client connection?
my hotspot script supports ap (accesspoint) and client, if you have 2 wlan adapter. if you have only one (internal), it will use the adpater for ap.
If two adapters are available, the script will use 2 different channels. Running on the same channel (ap and client), I have problems with rpi 3Bs.

pugbot
Posts: 4
Joined: Thu May 31, 2018 10:16 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Fri Feb 15, 2019 4:33 am

eugen2009 wrote:
Wed Feb 13, 2019 4:30 pm
In the step 5. there is an char mistake, instead of"/bin/bash/ /usr/local/bin/wifistart" it should be "/bin/bash /usr/local/bin/wifistart".
Thanks eugen2009, I've fixed the typo in step 5.

pugbot
Posts: 4
Joined: Thu May 31, 2018 10:16 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Fri Feb 15, 2019 5:06 am

davethomaspilot wrote:
Tue Feb 12, 2019 3:40 pm
Would it be possible to implement a script that would first attempt to connect as a client using wpa_supplicant.conf (with several SSIDs). If a good connection is made, then use the channel found from iwlist as the channel to use for the hotspot? If a client connection isn't made, then use a specified channel.

I'd prefer the specified channel be used for the hotspot. If the client can't connect to an SSID on this channel, I could still connect reliably to the hotspot. Is this achievable? (always use channel in hostapd.conf for hotspot regardless of client connection).
Yes, this is definitely achievable; I am doing it myself! As you can also see in rudrilatos' reply, you can even do it with a bash script. I am actually doing this in a .NET Core program as part of a larger application (long story), which I would not particularly recommend, so I did not include the details.

Suffice it to say, I am parsing results from wpa_cli to figure out the client-side channel of any network prior to actually connecting to it (which I am doing manually). I use the following two commands to grab output containing SSID frequencies.

Code: Select all

wpa_cli -p /var/run/wpa_supplicant -i wlan0 scan   #Scan local wifi
wpa_cli -p /var/run/wpa_supplicant -i wlan0 scan_results   #Output scan results
I then match the frequency to the proper channel via lookup table, basically this table of WLAN channels: https://en.wikipedia.org/wiki/List_of_WLAN_channels If the channel of the SSID I am connecting to doesn't match my hostapd channel, I modify the setup files for hostapd (which involves a lot of parsing) and reboot the system.

The reboot is not very elegant, nor is parsing wpa_cli output, but it does the trick for me. There is most likely a much better way to gracefully bring down the hostapd and dnsmasq services and bring them up again matching the client-side channel without doing a full reboot, but I haven't had the time to fiddle with it yet, and what I have works for my purposes.

davethomaspilot
Posts: 104
Joined: Tue Apr 29, 2014 6:18 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Fri Feb 15, 2019 3:55 pm

Thank you!

I put working on this on hold, pending a reply indicating "been there,done that".

I'll give it a try soon.

davethomaspilot
Posts: 104
Joined: Tue Apr 29, 2014 6:18 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Sun Mar 03, 2019 8:54 pm

davethomaspilot wrote:
Tue Feb 12, 2019 3:40 pm
Thanks for this info!

I followed pieyeguy's advice and expanded the dhcp address range to include 192.168.50.1.

I can wifi connect to the hotspot and then ssh and/or vnc connect to 192.168.50.1. The pi also connects as a client to my wifi router. Everything works!

But, it doesn't seem reliable. Eventually, I can not longer communicate over the hotspot. It stays connected, but the ssh session and/or vnc session become non-responsive.

Sometimes, just wifi disconnecting and reconnecting solves the problem. But right now, it doesn't

I noticed that while I have hostapd.conf set up for channel 10 (that's what I initially saw when I used

Code: Select all

iwlist wlan0 channel 
windows is reporting that I"m connected to channel 9. So, I checked again using iwlist and see that the client is actually using channel 9. It connected to another wifi router I have running. Maybe it worked for a while with the hotspot on 9 and the client on 10, but not reliably.

I'm thinking that might be the issue. I can remove all but one entry in my wpa_supplicant.conf file so that the pi will always connect to the same router. I'll try this and see if it resolves the issue.

But, that leaves me with a couple of questions...

If the pi can't connect to the specified SSID (say I'm at a different location), will the wifi hotspot sitll work and use the channel specified in hostapd.conf?

Would it be possible to implement a script that would first attempt to connect as a client using wpa_supplicant.conf (with several SSIDs). If a good connection is made, then use the channel found from iwlist as the channel to use for the hotspot? If a client connection isn't made, then use a specified channel.

I'd prefer the specified channel be used for the hotspot. If the client can't connect to an SSID on this channel, I could still connect reliably to the hotspot. Is this achievable? (always use channel in hostapd.conf for hotspot regardless of client connection).

Thanks!
Has anyone successfully stayed connected to a hotspot (3a+ or 3b+) when configured per pugbot's great instructions?

As a client, the connection is reliable, indefinitely. But, as a hotspot, the connection drops frequently.

It doesn't seem like a range or interference issue--I can be right at the hotspot and still have problems staying connected.

I'm about ready to give up on using the wifi as both a hotspot and client and try something like the script rudiratlos wrote.

But, on the 3A+, I have only one USB slot and it's already used. It would be nice to have the ability to connect directly to the pi as a hotspot and also have the ability to make it a client to a different wifi router.

Using a script to switch whether it's a client or hotspot isn't a good solution for me. Once configured to be a client, it would need to be connected to a router to reconfigure as a hotspot.

It's internal to a gadget, headless, and frequently moved to different venues. I need the ability to always connect directly to it without requiring a router.

dsc3507
Posts: 40
Joined: Sun Apr 28, 2013 5:08 am

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Mon Apr 22, 2019 7:54 am

Why not a hardware switch connected to a GPIO bit and monitored to determine AP or client status? So we need a script for AP or client depending on the switch position. You can use GPIO interrupt input to save having to poll.

pugbot
Posts: 4
Joined: Thu May 31, 2018 10:16 pm

Re: Raspberry pi 3 B+ as Access Point and Wifi client

Fri Aug 23, 2019 2:32 am

dsc3507 wrote:
Mon Apr 22, 2019 7:54 am
Why not a hardware switch connected to a GPIO bit and monitored to determine AP or client status? So we need a script for AP or client depending on the switch position. You can use GPIO interrupt input to save having to poll.
That would work, yes. I have a remote app controlling my Pi, so I needed software-based control. Additionally, with this solution, you can get both AP+STA modes working simultaneously, provided they are on the same channel. Depends on your project needs, I guess.

Return to “Networking and servers”