Finally I got the solution.
The only point is that I need a reboot to switch from Client to AP.
But this is not really an issue in my case.
However if there could be a solution in the last script I'm openminded
See script : scl2ap.sh
Thanks
Wifi
Activate Wifi in /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=FR
network={
ssid="Freebox-CKL-I"
psk="xxxxxxxxx"
}
Kill and restart Wifi
sudo pkill wpa_supplicant
sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext
Access point:
The Raspberry Pi needs to have the hostapd access point software package installed:
sudo apt-get install hostapd
To create a new configuration file : /etc/hostapd/hostapd.conf
Change : country, ssid , wpa_passphrase : to match with yours
country_code=FR
interface=wlan0
ssid=LapemNetwork
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Verify that configuration file location in /etc/init.d/hostapd containing is there, if not add.
DAEMON_CONF=/etc/hostapd/hostapd.conf
Enable the wireless access point service and set it to start when your Raspberry Pi boots:
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
DHCP Distribution
In order to provide network management services (DNS, DHCP) to wireless clients install dnsmasq
sudo apt-get install dnsmasq
To configure the distribution DHCP IP range we need to configure /etc/dnsmasq.conf
At the bottom of the file we need to add the following lines.
interface=wlan0
dhcp-range=192.168.0.100,192.168.0.200,12h
activate the dnsmasq service at boot:
sudo systemctl enable dnsmasq.service
AP as static
AP needs to have a static address: configured in etc/dhcpcd.conf
Defaut (DHCP)
sudo cp /etc/dhcpcd.conf /etc/dhcpcd-dynamic.conf
Static add at the end of the file
sudo nano /etc/dhcpcd.conf
#Static IP
interface wlan0
static ip_address=192.168.0.1/24
nohook wpa_supplicant
sudo cp /etc/dhcpcd.conf /etc/dhcpcd-static.conf
Default mode AP
Command to run to activate AP for next boot
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq.service
sudo cp /etc/dhcpcd-static.conf /etc/dhcpcd.conf
SWITCH : AP --> Client : definive
file : sap2cl.sh
Effect is dynamic and definive --> Next start will be as Client mode
#!/bin/bash
# This script will stop Hotspot (AP+DHCP) and will Switch your Raspberry to a WIFI Client (Definitely)
# Effect is dynamic and definive --> Next start will be as Client mode
# Run : bash sap2cl.sh
echo "========================================"
echo " Switch from Hotspot (AP+DHP) to Client "
echo " Next start will be : Wifi Client mode "
echo "========================================"
echo " "
echo "Stopping hostapd, dnsmasq "
sudo systemctl stop hostapd.service
sudo systemctl stop dnsmasq.service
echo "Configure Client to recover dhcp"
sudo cp /etc/dhcpcd-dynamic.conf /etc/dhcpcd.conf
sudo systemctl daemon-reload
echo "Restart wpa_supplicant"
sudo pkill wpa_supplicant
sleep 2
sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext
echo "Done."
exit
SWITCH : AP --> Client : Next boot will be as AP
file : sap2clnsap.sh
Effect is dynamic --> Next start will be as AP
#!/bin/bash
# This script will stop Hotspot (AP+DHCP) and will Switch your Raspberry to a WIFI Client (Temporarily)
# Effect is dynamic --> Next start will be as AP
# Run : bash sap2cl.sh
echo "========================================"
echo " Switch from Hotspot (AP+DHP) to Client "
echo " Next start will be : AP mode "
echo "========================================"
echo " "
echo "Stopping hostapd, dnsmasq "
sudo systemctl stop hostapd.service
sudo systemctl stop dnsmasq.service
echo "Configure Client to recover dhcp"
sudo cp /etc/dhcpcd-dynamic.conf /etc/dhcpcd.conf
sudo systemctl daemon-reload
echo "Restart wpa_supplicant"
sudo pkill wpa_supplicant
sleep 2
sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext
echo "Prepare for next start as AP"
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq.service
sudo cp /etc/dhcpcd-static.conf /etc/dhcpcd.conf
echo "Done."
exit
SWITCH : Client --> AP : THIS IS NOT DYNAMIC, AND NEEDS A REBOOT
File : scl2ap.sh
#!/bin/bash
# This script will Switch from Client to Hotspot (AP+DHP)
# Run : bash scl2ap.sh
echo "========================================="
echo " Switch from Client to Hotspot (AP+DHP) "
echo " THIS IS NOT DYNAMIC, AND NEEDS A REBOOT "
echo "========================================="
echo " "
echo "Start hostapd, dnsmasq"
sudo systemctl restart dnsmasq.service
sudo systemctl restart hostapd.service
echo "Configure AP wit static IP Address"
sudo cp /etc/dhcpcd-static.conf /etc/dhcpcd.conf
sudo systemctl daemon-reload
echo "Restart wpa_supplicant"
sudo pkill wpa_supplicant
sleep 2
sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext
echo "A REBOOT is needed here"
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq.service
echo "Done."
exit
-B
and add-d
inwpa_supplicant
command line to see debug lines. Also, wpa_supplicant not assign an IP address you must usedhclient
.dhclient --help
and in/etc/dhcp/dhclient.conf
you can find config example. 1) start wpa_supplicant with&
at the end or add-B
, 2) start dhclient. NOTE: also you can create one second wireless interface ` iw dev wlan0 interface add ap0 type __ap` , one for the client (for wpa_supplicant, wlan0), one for the AP (for hostapd, ap0)