Raspberry Pi Stretch Setting Up WiFi Access Point Hotspot

 

The Experiment: In this experiment were going write an interactive  bash script that will install a WiFi  Access Point / Hotspot on a Raspberry Pi 2 or 3 running Stretch. I have tried many of the WiFi Hotspot Installation Tutorial found on the Internet but none of them work after a reboot,  until I found this Tutorial here. So to make the install easier here is a Bash script which will configure the WiFi Hotspot and install the necessary dependence.

 

Table of Contents
1. Part List / Dependencies
2. Prototype
3. Notes
4. Conclusion
5. Reference Links


Part List / Dependencies:

1. Raspberry Pi2 B
2. USB WiFi Dongle
3. Raspberry pi B Case
4. 5v dc 2.5amp power supply

1. apt-get install hostapd
2. apt-get install dnsmasq
3. apt-get install iptables-persistent

^Top


Prototype:

Before Running the Script:

  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo reboot
  4. sudo nano /boot/config.txt
  5. # Comment out
    # Enable audio (loads snd_bcm2835)
    #dtparam=audio=on
  6. # Copy-Past
    # Disable Bluetooth
    dtoverlay=pi3-disable-bt

    # Disable Wifi
    dtoverlay=pi3-disable-wifi

  7. SAVE
#!/bin/bash
# RPI-2 WiFi Hotspot
# This version uses stretch image
# tr -d '\r' < infile > outfile
# chmod +x filename.sh
# sudo ./filename.sh

sleep 2s
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi

sleep 5s

# Verify debian version
debian_version=`egrep "jessie|stretch" /etc/os-release`
if [ `echo $debian_version|grep -c "jessie"` == 1 ]
then
echo "jessie"
exit 1
elif [ `echo $debian_version|grep -c "stretch"` == 1 ]
then
echo "Vesrion verified stretch"
else
echo "Some other version"
exit 1
fi

sleep 5s

echo "INSTALLING HOSTAPD SOFTWARE"
sudo apt-get install hostapd

sleep 5s

if grep -q '# WiFi Hotspot HOSTAPD' /etc/hostapd/hostapd.conf
then
echo "FOUND > # WiFi Hotspot HOSTAPD"
else
echo "Edit: /etc/hostapd/hostapd.conf"
cat <<EOF | sudo tee /etc/hostapd/hostapd.conf
# WiFi Hotspot HOSTAPD
interface=wlan0
driver=nl80211
ssid=<your-ssid>
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=<your-passphrase-at-leas-8-char>
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF
fi

sleep 5s

if grep -q 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' /etc/default/hostapd
then
echo "FOUND > DAEMON_CONF='/etc/hostapd/hostapd.conf'"
else
echo "Edit: /etc/default/hostapd"
echo DAEMON_CONF=\"/etc/hostapd/hostapd.conf\" | sudo tee -a /etc/default/hostapd
fi

sleep 5s

echo "INSTALLING DNSMASG SOFTWARE"
sudo apt-get install dnsmasq

sleep 5s

if grep -q '# WiFi Hotspot STATIC IP' /etc/dhcpcd.conf
then
echo "FOUND > # WiFi Hotspot STATIC IP"
else
echo "Edit: /etc/dhcpcd.conf"
cat <<EOF | sudo tee -a /etc/dhcpcd.conf # WiFi Hotspot STATIC IP interface wlan0 static ip_address=192.168.4.1/24 EOF fi sleep 5s if grep -q '# WiFi Hotspot DNS' /etc/dnsmasq.conf then echo "FOUND > # WiFi Hotspot DNS"
else
echo "Edit: /etc/dnsmasq.conf"
cat <<EOF | sudo tee -a /etc/dnsmasq.conf # WiFi Hotspot DNS interface=wlan0 dhcp-range=192.168.4.8,192.168.4.250,255.255.255.0,12h EOF fi sleep 5s if grep -q '#net.ipv4.ip_forward=1' /etc/sysctl.conf then echo "Edit: /etc/sysctl.conf UNCOMENT net.ipv4.ip_forward=1" sudo sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf else echo "FOUND > net.ipv4.ip_forward=1"
fi

sleep 5s

echo "SETUP POST ROUTING"
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

sleep 2s

echo "FORWARD IP TABLES"
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT

sleep 2s

echo "ACCEPT IP TABLES"
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

sleep 5s

echo "INSTALLING IPTABLE PRESISTENT SOFTWARE"
sudo apt-get install iptables-persistent

sleep 5s

echo "Rebooting System"
# sudo reboot
To run this script:
1. If script was created in Windows Text Editor:
   Save file to > rpi2hotspotwin
   Move the script to the Raspberry Pi > /home/pi/rpi2hotspotwin
   To remove \r Run script through the following command > 
   tr -d '\r' < rpi2hotspotwin > rpi2hotspot.sh
   Make script executable > chmod +x rpi2hotspot.sh
   Run the script > sudo ./rpi2hotspot.sh
   sudo reboot

2. If script was created in Unix Text Editor
   Save script to > rpi2hotspot.sh
   Make script executable > chmod +x rpi2hotspot.sh
   Run the script > sudo ./rpi2hotspot.sh
   Sudo reboot

Note: This is an interactive script just answer the prompts.
      So Y - Y - Y - Yes - No

Note: Don’t forget to set <your-ssid> and <your-passphrase-at-leas-8-char>.

Use at your OWN RISK see Disclaimer.
^Top


Notes:

Observation: While running a Bash Script created in a windows test editor, the script would always through a \r error. Did not know until I reached it that Window text editor insert a \r at the end of each line. To convert the Windows file to a Unix file run the script though the following command tr -d ‘\r’ < infile > outfile this will remove all the occurrences of \r.

This script only tested on RPI-2 B with USB WiFI dongle, but should also work on RPI-3 with on-board WiFi.

^Top


Conclusion:

This interactive Raspberry Pi Bash Script makes a WiFi Access Point / Hotspot install quick and easy.

^Top


Reference Links:

1. Using Raspberry Pi 3 running Stretch for a WiFi router
2.
3.

Reference Info
.

^Top

Advertisements