Saturday, 28 May 2016 11:47

Raspberry PI 3 Auto WiFi Hotspot if no Internet

Written by 
  • Author Type: Individual
  • Operating System: Raspbian
  • Country: UK

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.

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/

 

Last modified on Friday, 04 November 2016 21:49
Google
roboberry

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

Website: www.raspberryconnect.com
More in this category: « Internet servers and backup

Leave a comment

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

28 comments

  • Comment Link Graeme Friday, 13 January 2017 21:01 posted by Graeme

    Hi Mafkann, thanks for the update on iwlist. still working for me on my up to date Pi's, presumable it affects new installs and not rolling updates as my Pi's are. I plan to make a few updates to this process soon so will look into the depreciations. thanks

    Report
  • Comment Link mafkaan Thursday, 12 January 2017 08:09 posted by mafkaan

    thanks. made it work
    I had not seen this was an older post so...

    few things I needed to work out. in /etc/rc.local
    ssids=( 'mySSID1','mySSID2' ) had to be space delimeted like this
    ssids=( 'mySSID1' 'mySSID2' )
    I had found out, it was treated as one long string so use spaces around
    The other was 'iwlist' seems to be deprecated. I found with google search. the command had to be 'iw dev wlan scan ap-force' run as superuser
    the lines changed : became
    ssids=( 'mySSID1' 'mySSID2' )
    if iw dev wlan0 scan ap-force | grep $ssid > /dev/null

    Using Jessie ( lite ) from november 2016
    - iw was already setup so no need to go install it

    Hope someone will benefit from this in the future

    Report
  • Comment Link Graeme Thursday, 29 December 2016 21:13 posted by Graeme

    Hi Oj, I think your routers password in /etc/wpa_supplicant/wpa_supplicant.conf is not correct. Check the entry for psk="" has the correct password. Also if your routers ssid and password are in /etc/network/interfaces then put a # infront of each line.
    #iface wlan0 inet dhcp
    # wpa-ssid "mySSID"
    # wpa-psk "Router Pasword"

    Hope this helps, let me know if this doesn’t work and I will look into it further. :)

    Report
  • Comment Link 0j Wednesday, 28 December 2016 21:51 posted by 0j

    Thank you for such usefull script.
    However I have problem

    However I got a problem to connect back to my home wifi.
    when running rc.local I got respond on "Obtaining IP from DHCP"

    Report
  • Comment Link Robert H Friday, 23 December 2016 22:46 posted by Robert H

    If anyone if having trouble with the rc.local script not resolving your AP name, change the following line in rc.local

    FROM
    ssids=( 'mySSID1','mySSID2' )
    TO
    declare -a ssids=( "mySSID1" "mySSID2" )

    Report
  • Comment Link Graeme Tuesday, 29 November 2016 19:18 posted by Graeme

    Hi Vishal, glad you have it working, not sure what's going on with that. It worked when it was originally tested. I will have to look into that further.

    Report
  • Comment Link Vishal Verma Tuesday, 29 November 2016 02:32 posted by Vishal Verma

    Thanks Graeme for the feedback and resolution. I removed the multiple values from ssids=( 'mySSID1','mySSID2' ); and changed to have only one entry of my wifi router ssid, and it worked perfectly. Appreciate your help. Thanks again. Regards, Vishal

    Report
  • Comment Link Graeme Friday, 25 November 2016 22:52 posted by Graeme

    Hi Vishal, the rc.local script is not detecting your SSID as it is written, if it does detected the ssid but network config was wrong it would not go to a hotspot. Do you have more than 1 ssid entered, if so just try 1.
    You can run rc.local on its own with the command sudo /etc/rc.local
    you will see what decision it makes.
    Email me if no luck so I can help further. admin@ this website

    Report
  • Comment Link Vishal Verma Friday, 25 November 2016 09:47 posted by Vishal Verma

    Hi, Firstly thanks for the great step by step instructions. I am able to have my pi as an access point and SSH from my laptop to the pi with RPI3Wifi hotspot. But now the pi never connects to the home wifi router. Have changed the setting in rc.local. There is only one entry in wpa_supplicant.conf for the home wifi router. But it never gets that (even after reboot). Request help here. Regards, Vishal

    Report
  • Comment Link Graeme Thursday, 24 November 2016 19:09 posted by Graeme

    Hi Dimitar, sorry to hear it didn't work for you, if you want to try again you can e-mail me for support. As for removing it it sounds like you have done what is needed. the IP of 10.0.0.5 is only set in rc.local so if that has been removed then I would check your WiFi settings with the Icon next to the clock. There should be no entry for the RPI3wifi or 10.0.0.5. Or check wpa_Supplicant.conf and make sure only your routers SSID is listed. If it is in your WifI settings then that probably will be why it didn't work. let me know if you need further information.

    Report
  • Comment Link Dimitar Sunday, 20 November 2016 23:38 posted by Dimitar

    Hello,
    I tried to follow the instructions, but it did not work for me.
    Now I try to go back before I started but when RPI boots it always comes with the IP address of 10.0.0.5 - can't connect to internet
    If I run sudo dhclient wlan0 - then I get a new ip address and i can connect to internet
    but if I reboot again - it comes back with IP 10.0.0.5 again
    How can I fix that?

    I am using Raspberry Pi 3.
    I removed the script from rc.local
    in/etc/network/interfaces I uncommented
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    I checked that both services are disabled - dnsmasq and hostapd
    I did not revert the changes in/etc/dnsmasq.conf and/etc/default/hostapd

    Report
  • Comment Link Dan Saturday, 05 November 2016 18:14 posted by Dan

    Hello,
    I have an issue, that is although my Raspberry Pi 3 has successfully be an auto wifi hotspot thanks to your tutorial, but when i pluged in the ethernet cable, my PI cannot connect to Internet (but still connect to LAN, i still can SSH it over IP 192.168.1.7). Other devices connect to my home network (cellphone, Laptop,etc...) still can connect to the internet, exept my PI
    So please tell me how to fix it?

    Response: In Hotspot mode no internet can be used even with a LAN cable connected. Once the SSID is recognised the internet is available.

    Report
  • Comment Link Graeme Monday, 31 October 2016 20:17 posted by Graeme

    Hi JI, i have tried various settings, i think your hotspot is being triggered because your router is not being detected which would be why dnsmasq is always active. The messages "No Wireless interface" is shown when the hotspot is active. can you e-mail me at admin@ this website, with a bit more detail of what is happening, also are you using a Pi3 or using a wifi adapter?
    SOLVED: hostapd password only 4 digits, minimum 8

    Report
  • Comment Link Graeme Monday, 31 October 2016 20:06 posted by Graeme

    Hi Ralf, check to make sure the SSID has also changed in the wpa_supplicant.conf file. This is usually where the details will be when they have been changed by the wifi icon by the clock. I have recently added details about the main wifi ssid and password in Step2 just above Start-up Script. let me know if this helps.

    Report
  • Comment Link Ralf Monday, 31 October 2016 12:46 posted by Ralf

    Thank you for this tutorial, the hotspot is working fine, but my router isn't recogniced any more. I changed the SSID of my router in the rc.local file and I tried even an other SSID but it's not working or better it's not finding my router. What can I do?

    Report
  • Comment Link JI Saturday, 29 October 2016 21:33 posted by JI

    First of all thank you for your quickly answer.
    I tried the "wpa_supplicant -B" line and WiFi still not working. I tried the other two options but enemas status continues being "active (running) since..."
    Another thing is that when I click on the double vertical line it says "No wireless interfaces found"
    Any new suggestions?

    Report
  • Comment Link Graeme Friday, 28 October 2016 20:01 posted by Graeme

    Hi Paul, it is possible there is another dns server running such as udhcpd or isc-dhcp-server.
    if you run the command "ip addr" you should get two inet entries under WLAN0. one should be 10.0.05 and the other the same as the wifi icon shows. my one is 169.254.71.151. If 10.0.0.5 is not showing then I believe something else is issuing an ip address.
    use sudo systemctl status udhcpd or isc-dhcp-server to see if they are running.

    Report
  • Comment Link Paul Atredies Friday, 28 October 2016 02:41 posted by Paul Atredies

    The tutorial works for setting up the hotspot to logon only to the pi, however it is issuing itself a strange Ipaddress that is not the 10.0.0.5 address, nor can I use the hostname to SSH into the pi. Any suggestions as to why?

    Report
  • Comment Link Graeme Thursday, 27 October 2016 22:00 posted by Graeme

    Hi JI, I also have this running on a Pi3 with Pixel. I have managed to replicate your issue. If you copy the long line from rc.local starting with wpa_supplicant -B and paste it into terminal (preceded by sudo) does your wifi come back? it will only be for this session though.
    If systemctl disable is not working then try "sudo update-rc.d dnsmasq disable" and the same for hostapd. If no luck then check the /etc/default/dnsmasq file and change ENABLED=1 to ENABLED=0.

    The issue is dnsmasq is still running at boot so your wifi won't connect to your network.
    let me know if this works or if you still have an issue.

    Report
  • Comment Link JI Thursday, 27 October 2016 11:13 posted by JI

    I did all the steps but is not working. It not create a wifi
    I have the new pixel interface and the icon is a double vertical line with two red X on it. When i put the mouse on it I get:

    eth0: link is down
    wlan0: not associated

    I check the hostapd and dnsmasq status and they are active. I redo "sudo systemctl disable dnsmasq" and "sudo systemctl disable hostapd" and they still active (with and without a reboot).

    What can I do to fix it?

    Report
  • Comment Link Striker Monday, 17 October 2016 19:32 posted by Striker

    Hello. I did this and now my wifi icon dissapeared. I did everything as in the tutorial so i dont know what could be wrong. Any ideas of what could be or how to undo everything? Thanks

    Hi, if you are getting two screens instead of a Wifi icon, as shown at the bottom of the article, then it is generating a hotspot and not connecting to your router. Make sure the SSID is the same as your router in rc.local, also hostapd maybe running at boot. check this with
    "sudo systemctl status hostapd" and "sudo systemctl status dnsmasq" if there is a green text saying Active (i think) then they are running at boot. redo "sudo systemctl disable dnsmasq" and "sudo systemctl disable hostapd" and try again.
    Are you using a Pi3? to reverse; remove the added script from /etc/rc.local and in the /etc/network/interfaces remove the # from "# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf"
    e-mail me if you have further issues, happy to help get it up and running for you.

    Report
  • Comment Link Vijayenthiran Tuesday, 20 September 2016 12:07 posted by Vijayenthiran

    In the sudo nano /etc/hostapd/hostapd.conf

    commenting out driver=nl80211 works for raspberry pi 3.

    Report
  • Comment Link Graeme Thursday, 01 September 2016 19:07 posted by Graeme

    Hi Rob, i no expert at networking but i think you need to add a wifi dongle to use Wlan1. Instead of using a static IP are you able to connect via a Host name for your network set up. Then the interfaces setup can be set to manual. I connect to my PI's at home via there host name and the Hotspot IP when out.
    I'm happy to try some static/manual setting on my set up to see what works if need be.

    Report
  • Comment Link Rob Thursday, 01 September 2016 00:18 posted by Rob

    This looks really useful, I just seem to be hitting a snag (it's late and it's probably me).
    I've got my Pi set on a static IP address for my home network using the network interfaces file on wlan0. Will changing wlan0 from static to manual mess that up?
    Could I have wlan0 set to static and wlan1 set to manual to get round this?
    Sorry if this is an obvious question.

    Report
  • Comment Link Graeme Wednesday, 03 August 2016 20:35 posted by Graeme

    Hi Emanon, the Wifi password is stored in the the WPA_supplicant.conf file, but the wifi connection can be setup normally via the WiFi icon in the top right corner of the desktop. To have it as a preferred connection then add it to the ssids=(...) list in the rc.local file. The pi will broadcast 2 IP's one similar to the one stated and 10.0.0.5 set in the script. Use 10.0.0.5. I don't think the host name is reliable for this but it works fine with the IP.
    For the connection issue; are you using a RPi3 or other model with a WiFi dongle? if using dongle it may not be compatible with AP mode.
    I have just checked out a Pi2 with wifi dongle which did create a hotspot but kept dropping the connection. This was probably because of power issues on the battery as it was fine plugged into the mains.
    If you have no luck then e-mail me to try and sort it out (link in contact us)

    Report
  • Comment Link emanon Tuesday, 02 August 2016 01:06 posted by emanon

    thanks for this tutorial! However, by default the machine doesn't connect to the default router (where do I set the password for it, by the way? Is it still the wpa_supplicant file?) and creates a hotspot. I suceed in connecting to it, but I can't ssh the pi (neither by its host name nor by the ip. The IP the hotspot gives me seems to be in a completely different network (169.254.78.193). Any ideas?

    Report
  • Comment Link Graeme Saturday, 11 June 2016 12:37 posted by Graeme

    hi, it uses an infrastructure Access Point, not a AdHoc connection. The pi doubles as a router so all devices connect to the pi's router (RPI3hot) so they are on the same network and communicate via the router and not directly connected to each other. For a ssh connection with a Tablet both the pi and the tablet are communication via the pi's router, even though there is physically only two devices from a network point of view there are 3.

    Report
  • Comment Link Alejandro Saturday, 11 June 2016 05:49 posted by Alejandro

    Hi, one question... This is a kind of Ad-Hoc conection?? I'm looking for that hours and hours and I can't find anything!

    Report

Additional information