Local hotspot

Set up a local hotspot

In order to work as an access point, the Raspberry Pi will need to have access point software installed, along with DHCP server software to provide connecting devices with a network address. Ensure that your Raspberry Pi is using an up-to-date version of Raspbian (dated 2017 or later).
Install the required software (dnsmasq and hostapd) with this command:
1
sudo apt update
2
sudo apt upgrade
3
sudo apt install dnsmasq hostapd
Copied!

Configuring a static IP

To configure the static IP address, open the dhcpcd configuration file with the following command:
1
sudo nano /etc/dhcpcd.conf
Copied!
Go to the end of the file and edit it so that it looks like the following:
1
interface wlan0
2
static ip_address=192.168.4.1/24
3
nohook wpa_supplicant
Copied!
Now restart the dhcpcd daemon:
1
sudo systemctl restart dhcpcd
Copied!

Configuring the DHCP server (dnsmasq)

The DHCP service is provided by dnsmasq. By default, the configuration file contains a lot of information that is not needed, and it is easier to start from scratch. Rename this configuration file, and edit a new one:
1
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
Copied!
1
sudo nano /etc/dnsmasq.conf
Copied!
Type or copy the following information into the dnsmasq configuration file and save it:
1
interface=wlan0 # Use the require wireless interface - usually wlan0
2
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
Copied!
Reload dnsmasq to use the updated configuration:
1
sudo systemctl reload dnsmasq
Copied!

Configuring the access point host software (hostapd)

1
sudo nano /etc/hostapd/hostapd.conf
Copied!
add the following text into the file
1
interface=wlan0
2
driver=nl80211
3
ssid=pytobot:00:00:00
4
hw_mode=g
5
channel=7
6
wmm_enabled=0
7
macaddr_acl=0
8
auth_algs=1
9
ignore_broadcast_ssid=0
10
wpa=2
11
wpa_passphrase=PASSWORD
12
wpa_key_mgmt=WPA-PSK
13
wpa_pairwise=TKIP
14
rsn_pairwise=CCMP
Copied!
We now need to tell the system where to find this configuration file.
1
sudo nano /etc/default/hostapd
Copied!
Find the line with #DAEMON_CONF, and replace it with this:
1
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Copied!

Start it up

Now enable and start hostapd:
1
sudo systemctl unmask hostapd
2
sudo systemctl enable hostapd
3
sudo systemctl start hostapd
Copied!

Add routing and masquerade

Edit /etc/sysctl.conf
1
sudo nano /etc/sysctl.conf
Copied!
and uncomment this line:
1
net.ipv4.ip_forward=1
Copied!
Add a masquerade for outbound traffic on eth0:
1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Copied!
Save the iptables rule.
1
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Copied!
Edit /etc/rc.local and add this just above "exit 0" to install these rules on boot.
1
sudo nano /etc/rc.local
Copied!
Add the following line aboce Exit 0
1
iptables-restore < /etc/iptables.ipv4.nat
Copied!
Using a wireless device, search for networks. The network SSID you specified in the hostapd configuration should now be present, and it should be accessible with the specified password.
If SSH is enabled on the Raspberry Pi access point, it should be possible to connect to it from another Linux box (or a system with SSH connectivity present) as follows, assuming the pi account is present:
Copied!
By this point, the Raspberry Pi is acting as an access point, and other devices can associate with it. Associated devices can access the Raspberry Pi access point via its IP address for operations such as rsync, scp, or ssh.

Update the SSID and password

The following script updates the SSID name to the last 8 digits of the mac addres. The PASSWD is updated to a random 8 digit number.
acc_point_update.sh
1
#!/bin/bash
2
3
SSID=pytobot:$(ifconfig eth0 | grep -Eo ..\(\:..\){5} | tail -c 9)
4
echo $SSID
5
6
PASSWD=$((10000000 + RANDOM % 9999999))
7
echo $PASSWD
8
9
sudo sed -i "s/SSIDNAME/$SSID/g" /etc/hostapd/hostapd.conf
10
sudo sed -i "s/PASSWORD/$PASSWD/g" /etc/hostapd/hostapd.conf
11
12
sudo systemctl unmask hostapd
13
sudo systemctl enable hostapd
14
sudo systemctl start hostapd
Copied!

​Execute script from startup

TODO: At this moment the script will search for SSIDNAME and PASSWD in the config file. The first time on startup the SSIDNAME and PASSWD will be changed to the MACADDR and random PASSWD, But after that its not possible to change it anymore. That is not a problem as long the SD card stays in the same controller with the same MACADDR.

References

Local hostspot- Raspberry Pi[SOURCE]
Last modified 2yr ago