Sunday, 19 February 2017 10:55

Raspberry PI 3 Auto WiFi Hotspot if no Internet - OldScript

Written by 
Rate this item
(3 votes)

I'm using a Raspberry Pi 3 which i want to connect to via SSH from my Android tablet or phone when i am not in range of my home router.

My requirements are for when the pi is at home it connects to my home router or other known routers so it is available on the local network. If no router is found when it is started up then it will create a wifi hotspot, which is not rooted to the Internet, so i can SSH to the RPi from another device and control the camera and run other custom scripts.

Previously i could do this by creating an adhoc hotspot on my Android Nexus tablet and set the Raspberry Pi to connect to the Tablet if the home network was not in range. In google's infinite wisdom they dropped this feature for my Nexus tablet in the Android Marshmallow update, meaning i could no longer connect to my pi when i was out and about.

The other solution is for the RPi to create a wifi hotspot and connect my tablet to that. Most tutorials for hotspot setup on the pi are for creating an access point to the internet via the Raspberry pi which is not what I require.
I use my pi for photography amongst other things when im out and about, so i don't need internet access just a direct connection to control the Raspberry Pi via SSH from my tablet.

 

Note: This article is now out of date as some of the commands have been depreciated and is now just for reference. Please use the new article for a AutoHotspot. Raspberry PI 3 Auto WiFi Hotspot if no Internet

This tutorial is the solution I used based on parts of other tutorials i have found online.

Aim:

Raspberry Pi searches for known router's (SSID)

If the router is not found then it creates a hotspot so tablets, phones and computers can connect to the Raspberry Pi's WiFi hotspot, which is not routed to the internet but allows a connection via SSH, VNC etc.

Requirements:

Raspberry Pi 3 or other Raspberry Pi with WiFi dongle*

Internet Connection

WiFi connection set-up to the local router

*some WiFi dongles don't work in adhoc mode or don't work with with the nl80211 driver used in this guide, so you may want to check this first before starting.

 

Step1:

To start with hostapd hotspot client and dnsmasq lightweight dns server need to be installed.

Open a Terminal session.

Update Raspbian with the latest updates by entering the commands:

sudo apt-get update

sudo apt-get upgrade

 To install hostapd enter the command:

sudo apt-get install hostapd

enter Y when prompted.

To install dnsmasq enter the command:

sudo apt-get install dnsmasq

enter Y when prompted

The installers will have set up the programme so they run when the pi is started. For this setup they only need to be started if the home router is not found. So automatic startup needs to be disabled. This is done with the following commands:

sudo systemctl disable hostapd

sudo systemctl disable dnsmasq

Now the hostspot configuration file can be setup. This contains the name of the WiFi signal you will need to connect to (SSID) and the security password.

To edit the configuration files I will be using the nano text editor but if you prefer an editor with an point and click interface then replace nano with leafpad in the following instructions.

Hostapd Configuration

Using a text editor edit the hostapd configuration file. This file won't exist at this stage so will be blank.

sudo nano /etc/hostapd/hostapd.conf

enter or paste the settings:

interface=wlan0
driver=nl80211
ssid=RPI3wifi
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=1234567890
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

  • The interface will be wlan0
  • The driver nl80211 works with the Raspberry Pi 3 onboard WiFi but you will need to check that your wifi dongle is compatable and can use AP mode.

For more information on wifi dongles see elinux.org/RPi_USB_Wi-Fi_Adapters

  • The SSID is the name of the WiFi signal broadcast from the RPi, which you will connect to with your Tablet or phones WiFi settings.
  • Channel can be set between 1 and 13. If you are having trouble connection because of to many wifi signals in your area are using channel 6 then try another channel.
  • Wpa_passphrase is the password you will need to enter when you first connect a device to your Raspberry Pi's hotspot. This should be at least 8 characters and a bit more difficult to guess than my example.

To save the config file press ctrl & o and to exit nano press Ctrl & x

Now the defaults file needs to be updated to point to where the config file is stored.
In terminal enter the command
sudo nano /etc/default/hostapd

Change:
#demon_conf=""
to
demon_conf="/etc/hostapd/hostapd.conf"

And save.


dnsmasq configuration

Next dnsmasq need to be configured to allow the PI to act as a router and issue IP addresses.

sudo nano /etc/dnsmasq.conf

Go to the bottom of the file and add the following lines

#Pi3Hotspot Config
#stop DNSmasq from using resolv.conf
no-resolv
#Interface to use
interface=wlan0
bind-interfaces
dhcp-range=10.0.0.3,10.0.0.20,12h

And then save (ctrl & o) and exit (ctrl & x)

Step2:

Now that hostapd and dnsmasq are configured we now need to make some changes to the interfaces file and then add a script that will detect if you are at home or not.

Next we need to edit the interfaces file. There will be several entries already in the file. Look for references to Wlan0 and alter them as below.
Any reference to wpa_conf for wlan0 should be disabled by putting a # at the start of the line.

Open the interfaces file with the command

sudo nano /etc/network/interfaces

edit the following lines as below

auto lo wlan0
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

now save (ctrl & o) and exit (ctrl & x)


Note: Due to the constants updates to Linux the Wifi login details have moved, if your home routers SSID and password was listed in this file then the hotspot will probably not get generated.

You will need to put a # infront of each line

#iface wlan0 inet dhcp
#       wpa-ssid "mySSID"
#       wpa-psk "Router Pasword"

These details need to be in the wpa_supplicant.conf file to work with this setup.

Add your router details to the wpa_supplicant.conf file with 

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and add the following commands to the bottom of the file.

network={
        ssid="mySSID"
        psk="Router Password"
        key_mgmt=WPA-PSK
}

If in the future you change your router connection details with the WiFi icon by the clock then check that the changes have been made to the wpa_supplicant.conf file and not the interfaces file.

Start-up Script

The final stage is to add the startup script to the rc.local file.
This is an altered version of the "Rpi Network conf Bootstrapper" script by Lasse Christiansen

This script will check what routers are available when the RPi is stared in the order of mySSID1, mySSID2 etc. The first router found in the list will be connected to using existing configured WiFi settings. If none of the listed SSIDs are in range then a WiFi hotspot is created.

sudo nano /etc/rc.local

Your rc.local file will look like this if it has not previously been edited.

#!/bin/sh
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
Fi
exit 0

Firstly change the top line from #!/bin/sh to #!/bin/bash

The line "exit 0" need to be at the bottom of the file so add the bootstrapper script between

printf "My IP address is %s\n" "$_IP"
Fi

and

exit 0

 

#Wifi config - if no prefered Wifi generate a hotspot
# RPi Network Conf Bootstrapper
 
createAdHocNetwork()
{
    echo "Creating RPI Hotspot network"
    ifconfig wlan0 down
    ifconfig wlan0 10.0.0.5 netmask 255.255.255.0 up
    service dnsmasq start
    service hostapd start
    echo " "
    echo "Hotspot network created"
    echo " "
}
 
echo "================================="
echo "RPi Network Conf Bootstrapper"
echo "================================="
echo "Scanning for known WiFi networks"
ssids=( 'mySSID1','mySSID2' )
connected=false
for ssid in "${ssids[@]}"
do
    echo " "
    echo "checking if ssid available:" $ssid
   echo " "
    if iwlist wlan0 scan | grep $ssid > /dev/null
    then
        echo "First WiFi in range has SSID:" $ssid
        echo "Starting supplicant for WPA/WPA2"
        wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
        echo "Obtaining IP from DHCP"
        if dhclient -1 wlan0
        then
            echo "Connected to WiFi"
            connected=true
            break
        else
            echo "DHCP server did not respond with an IP lease (DHCPOFFER)"
            wpa_cli terminate
            break
        fi
    else
        echo "Not in range, WiFi with SSID:" $ssid
    fi
done
 
if ! $connected; then
    createAdHocNetwork
fi 

 And finally save (ctrl & o) and exit (ctrl & x)

In the line ssids=( 'mySSID1', 'mySSID2' ) change mySSID1 to the SSID name of your home router,

in the UK common SSIDs are in the format of SKY12345, BTHub-1234, TalkTalk12345, VM123456, plusnet1234 etc

if you connect your Rpi to other routers then these can be added separated by a comma.
Remove mySSID2 if you only want to search for one router as this will speed up the startup process.

 

You should now be able to reboot and if all has gone ok your Raspberry Pi will start and connect to your router as normal.

 Testing the Hostspot


To test if the hotspot is being created edit the rc.local file again and change your SSID to something else such as mySSID1off

ssids=( 'mySSID1','mySSID2' ) to ssids=( 'mySSID1off','mySSID2off' )

Save the rc.local file and then reboot.

When the desktop loads the WiFi network icon in the top right corner will be two screens instead of the usual WiFi icon.

RPIwifihotspot-wifi

When the mouse is placed over the icon is should show a popup displaying the RPI3hot access point.

RPIwifihotspot-AP

 

Check the WiFi signals in range with a wifi device and you should see that one of them is RPI3hot

wifisignal-android

 

Select this as the WiFi signal to connect to. The security password will be the one you set in the hostapd.conf file.

From my example that would be 1234567890.

Now you are connected to the Raspberry Pi's hotspot and you can now open a SSH connection as usual using the host name. If you are using the IP address then that will be 10.0.0.5

Once your happy it is all working, reset the SSID entry back to your routers SSID and your ready to go.

 

This setup has been tested on a Raspbery Pi 2 and PiZero with a WiFi dongle and a Raspberry Pi 3 all running Raspbian Jessie. 

 Reference:

Tutorial used as reference for this guide are:

lcdev.dk/2012/11/18/raspberry-pi-tutorial-connect-to-wifi-or-create-an-encrypted-dhcp-enabled-ad-hoc-network-as-fallback/

nims11.wordpress.com/2013/05/22/using-hostapd-with-dnsmasq-to-create-virtual-wifi-access-point-in-linux/

frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

 

Read 17151 times Last modified on Sunday, 19 February 2017 13:06
Google
roboberry

RapberryConnect.com SuperUser.  Contactable via the site admin e-mail in the Contact Us link.

Website: www.raspberryconnect.com

Leave a comment

Comments for Guest and account owners. Account owners will need to login to use registered e-mail address.

1 comment

  • Comment Link Sanjay Singh Friday, 14 July 2017 11:57 posted by Sanjay Singh

    Thanks a lot to the author.

    I would like to tell that its working perfectly on Raspberry Pi 3 Model B. If someone wants to create a wifi hotspot network which get start with RPi boot then specify name of your wifi hotspot network in ssids=('mySSID1', 'mySSID2'), that is: ssids=('RPI3wifi')

    Thanks...

    Report

Additional information