ddmitry1973
Posts: 12
Joined: Thu Apr 20, 2017 6:57 am

Raspberry Pi 3 on Stretch - best way to switch WiFi AP/client mode (details inside)

Wed Jan 09, 2019 1:26 pm

Hello all,

I'm running a custom application on Pi 3 (Stretch lite). Application is started from specially created account, say, userA (not root), which belongs to netdev group - so, can use wpa_cli, if needed.
Application have to switch built-in wifi from AP to client Mode.

So I'm looking for optimal solution for that.

My first idea is to play with wpa_supplicant.conf:
1. switch path wpa_supplicant.conf so that userA can change file contents
2. header of wpa_supplicant.conf is unchanged:

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
3. when I need AP mode i put as network section:

Code: Select all

network={
    ssid="APnet"
    mode=2
    key_mgmt=WPA-PSK
    psk="----"
    frequency=2437
    proto=RSN
    }
4. when I need Client mode i put as network section:

Code: Select all

 network={
    ssid="NameOfNetwork2Connect"
    mode=O
    key_mgmt=WPA-PSK
    psk="----"
    ... any other options I need ...
    }
5.then i call

Code: Select all

 "wpa_cli -iwlan0 reconfigure" 
from my app (as I told userA belongs to netdev, so has appropriate rights). Wpa supplicant re-reads configuration and restarts wifi into needed mode.

PROS: I've already have everything I need in my os env
CONS: current path to wpa_supplicant.conf is /etc/wpa_supplicant/wpa_supplicant.conf .
it is hardcoded into /lib/systemd/system/wpa_supplican@.service:

Code: Select all

[Service]
Type=simple
ExecStart=/sbin/wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant-%I.conf -Dnl80211,wext -i%I
I can, of course, edit this file, but I would like not to touch original systemd files. Tried to create /etc/systemd/system/wpa_supplicant@.service.d folder and put custom.conf:

Code: Select all

[Service]
Type=simple
ExecStart=/sbin/wpa_supplicant -c/usr/local/conf/wpa_supplicant/wpa_supplicant-%I.conf -Dnl80211,wext -i%I
as given in systemd configuration examples but it has no effect.

Pls, advise:
- Is my approach is correct?
- if yes, what's the right way to override path to wpa_supplicant.conf?
- if not - what approach would you propose - but, please, take into consideration that I would like to avoid starting/stopping services as this will require giving root privileges to userA account which I don' want to do

Thanks.

UPDATE:

No need to override path. I put wpa_supplicant.conf into needed folder, then made symbolic link to it in /etc/wpa_supplicant/. Works perfectly.

Return to “Advanced users”