Friday, 07 July 2017 19:31

Raspberry Pi - Auto WiFi Hotspot Switch - Direct Connection

Written by 
  • Author Type: Individual

A script to allow the Raspberry Pi to connect to a know wifi router or automatically generate a Non Internet Hotspot Access Point if no network is found. You can then use SSH or VNC on the move and switch between the hotspot and home network without a reboot.

This is suitable for the Pi Zero W which has no Network port or anybody who does not need an internet routed access point on a Raspberry Pi 3 or RPi 3B+, RPi 4. Just for direct Wifi access to the PI when you are out and about.

Works with Raspbian Buster, Stretch and Jessie.

Latest updates to setup - 16th May 2018 - hostapd.conf, dhcpcd.conf, autohotspot

When i am home I like to have my Raspberry Pi connected to the home network and the internet but when I am out I would like to connect to it using ssh via a hotspot using a tablet, phone or laptop.

I wanted a Raspberry Pi that connects to my home networks Wifi when I am at home or generates a wifi Hotspot/Access Point when I am out.

I have done this in a previous script but, this is an updated process and more robust than my original script. So this article is how to setup a Raspberry Pi so it can automatically switch between a network wifi connection and a non internet hotspot.


If do require an internet routed hotspot as you have a RPi4, RPi3 B+, RPi3 or a RPi 1/2 with a wifi dongle then use my alternative script at Raspberry Pi - Auto WiFI Hotspot Switch Internet

If you would just like a permanent hotspot then you can use this guide Raspberry Pi - Hotspot/Access Point dhcpcd method


  • When your home:   On starting the Raspberry Pi it connects to your home routers wifi
  • When your out: On starting, if any known wifi connection is not found it will generate a hotspot so a direct wifi connection can be made to the Raspberry Pi by a tablet, phone or laptop.  Useful for SSH, VNC and webservers.
  • While in Hotspot mode for RPi 1/2/3/4:  if an ethernet cable is connected the Raspberry Pi, then it will NOT have internet access

Additional Features:

Using a Cron, a timer can be setup so the wifi connection can be regulary checked. It will switch between a wifi router and a hotspot without a reboot depending on the results. This is useful for

  • Raspberry Pi in car entertainment systems
  • Raspberry Pi Dash Cams
  • If the RPi looses wifi connection in your garden or near your home when using the camera or sensors.
  • You run a script or program at home connected to your router and wish to monitor it while you are out. As a hotspot is generated without a reboot the script/program is not interuppted.

It is also possible to run the script from a GPIO button so you can manually run the script to switch depending on where you are.



This has been tested on Raspbian Buster, Stretch, Jessie. To see which version you have enter the command lsb_release -a

  • Raspberry Pi Zero W or Raspberry Pi 3, RPi3B+, RPi4
  • Raspberry Pi 1,2 or Zero with a Wifi Dongle*,
  • Wifi already configured for your home router

*some WiFi dongles don't work in adhoc/AP mode or don't work with with the nl80211 driver used with the RPi 3 & Pi zero W inbuilt wifi, so you may want to check this first before starting.

 This setup has been tested on Raspbian Jessie, Raspbian Stretch and Raspbian Buster using a RPi4, RPI3 B+, RPI3, RPI Zero W and a RPI 2 . A connection has been made to the Hotspot using an Android Tablet, Ipad2, Raspberry PI and Windows 10. All worked fine with SSH.

Note: Existing Autohotspot users

If you are currently using one of my older autohotspot scripts and would like to change it to this setup, please read all sections as there are changes to the dnsmasq.conf file, interfaces file, the service file and the autohotspot file.


Dnsmasq bug: in versions below 2.77 there is a recent bug that may cause the hotspot not to start for some users. This can be resolved by removing the dns-root-data. It may be benificial to do this before you start the rest of the installation as it has been reported that doing it after installation for effected users does not work but you won't know if it is an issue until after the installation is complete.

check your version with : dpkg -s dnsmasq

versions 2.77 and above are ok. If not then try the command:

sudo apt-get purge dns-root-data

thanks to danny for highlighting this.

Note about Raspbian Stretch & Buster Network Device Names

For Raspbian Stretch there has been changes to how the network drivers are named, called Predictable Network Interface Names,  and may be different for the usual wlan0 and wlan1 for wifi and eth0 for ethernet connections. Though the official Foundation version of Raspbian Stretch seems to be keeping to the old standard names, at least at the time of writing,  this may not always be the case. For this guide I will use wlan0 as the device that is used.  

To check the device name for your setup enter the commmand iw dev and take a note of the "Interface" name. For wifi it should start with wl , replace your device name with any reference to wlan0 in the article, scripts and config files.

Step 1:

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

download file here:

#2.4GHz setup wifi 80211 b,g,n
wpa_pairwise=CCMP TKIP

#80211n - Change GB to your WiFi country code

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

For more information on wifi dongles see

  • 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.
  • The country_code should be set to your country to comply with local RF laws. You may experience connection issues if this is not correct. Your country_code can be found in /etc/wpa_supplicant/wpa_supplicant.conf or in Raspberry Pi Configuration - Localisation settings

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


Check the DAEMON_OPTS="" is preceded by a #, so is #DAEMON_OPTS=""

And save.

DNSmasq configuration

Next dnsmasq needs to be configured to allow the Rpi to act as a router and issue ip addresses. Open the dnsmasq.conf file with

sudo nano /etc/dnsmasq.conf

Go to the bottom of the file and add the following lines (download here)

#AutoHotspot Config
#stop DNSmasq from using resolv.conf
#Interface to use

and the save (ctl & o) and exit (ctrl & x)

Step 2:

Now that hostapd and dnsmasq are configured we now need to make some changes to the interfaces file, add a service file and then add the autohotspot script.

The interfaces file is not required and should be empty of any network config. Depending which version of Raspbian you have this file may still contain network config.


sudo nano /etc/network/interfaces

If you file shows more than the standard top 5 lines like this

 # interfaces(5) file used by ifup(8) and ifdown(8) 
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

then make a copy of of your file and then remove any excess lines from the interfaces file.

To make a backup of your interfaces file use the command

sudo cp /etc/network/interfaces /etc/network/interfaces-backup


dhcpcd is the software that manages the network setup. The next step is to stop dhcpcd from starting the wifi network so the autohotspot script in the next step takes control of that. Ethernet will still be managed by dhcpcd.

Open dhcpcd.conf with the command

sudo nano /etc/dhcpcd.conf

at the bottom of the file enter the line

nohook wpa_supplicant

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

autohotspot service file

Next we have to create a service which will run the autohotspot script when the Raspberry Pi starts up.

create a new file with the command

sudo nano /etc/systemd/system/autohotspot.service

Then enter the following text or download here

Description=Automatically generates an internet Hotspot when a valid ssid is not in range

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


For the service to work it has to be enabled. To do this enter the command

sudo systemctl enable autohotspot.service

Software check:

If your version of Raspbian has been repeatedly upgraded from an old OS image, it is possible that that you don't have the correct wifi tools install. These are installed by default on more recent versions of Raspbian.

to check you have iw installed enter the command

dpkg -s iw

If it is installed the first two lines that shown should be

Package: iw
Status: install ok installed

if not enter the command

sudo apt-get install iw

AutoHotspot Script

This is the main script that will manage your wifi connections between a wifi router and an Hotspot.

It will search for any wifi connection that is setup on you Raspberry Pi by using the details found in /etc/wpa_supplicant/wpa_supplicant.conf

If no wifi signal is found for a known SSID then the script will shutdown the wifi network setup and create a Hotspot. Allowing you to connect to the Raspberry Pi from a wifi device using SSH or VNC.

 The script works with SSID's that contain spaces and by entering your routers MAC address it can be used with hidden SSID's.

Hidden SSIDs

If your routers SSID is not broadcast/hidden then find this section in the script

#Enter the Routers Mac Addresses for hidden SSIDs, seperated by spaces ie
#( '11:22:33:44:55:66' 'aa:bb:cc:dd:ee:ff' )

and enter your routers MAC address in the brackets of mac=() as shown in the example. Make sure mutiple MAC addresses are seperated by a space.


 Creating the autohotspot script:

Create a new file with the command

sudo nano /usr/bin/autohotspot

and add the autohotspot script. This can be downloaded from here

#version 0.95-4-N/HS

#You may share this script on the condition a reference to 
#must be included in copies or derivatives of this script. 

#A script to switch between a wifi network and a non internet routed Hotspot
#Works at startup or with a seperate timer or manually without a reboot
#Other setup required find out more at

wifidev="wlan0" #device name to use. Default is wlan0.
#use the command: iw dev ,to see wifi interface name 

#These four lines capture the wifi networks the RPi is setup to use
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//')
IFS=$IFSdef #reset back to defaults

#Note:If you only want to check for certain SSIDs
#Remove the # in in front of ssids=('mySSID1'.... below and put a # infront of all four lines above
# separated by a space, eg ('mySSID1' 'mySSID2')
#ssids=('mySSID1' 'mySSID2' 'mySSID3')

#Enter the Routers Mac Addresses for hidden SSIDs, seperated by spaces ie 
#( '11:22:33:44:55:66' 'aa:bb:cc:dd:ee:ff' ) 

ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking

    echo "Creating Hotspot"
    ip link set dev "$wifidev" down
    ip a add brd + dev "$wifidev"
    ip link set dev "$wifidev" up
    dhcpcd -k "$wifidev" >/dev/null 2>&1
    systemctl start dnsmasq
    systemctl start hostapd

    echo "Shutting Down Hotspot"
    ip link set dev "$wifidev" down
    systemctl stop hostapd
    systemctl stop dnsmasq
    ip addr flush dev "$wifidev"
    ip link set dev "$wifidev" up
    dhcpcd  -n "$wifidev" >/dev/null 2>&1

	echo "Checking WiFi connection ok"
        sleep 20 #give time for connection to be completed to router
	if ! wpa_cli -i "$wifidev" status | grep 'ip_address' >/dev/null 2>&1
        then #Failed to connect to wifi (check your wifi settings, password etc)
	       echo 'Wifi failed to connect, falling back to Hotspot.'
               wpa_cli terminate "$wifidev" >/dev/null 2>&1

#Check to see what SSID's and MAC addresses are in range
i=0; j=0
until [ $i -eq 1 ] #wait for wifi if busy, usb wifi is slower.
        ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1 
        echo "SSid's in range: " $ssidreply
        echo "Device Available Check try " $j
        if (($j >= 10)); then #if busy 10 times goto hotspot
                 echo "Device busy or unavailable 10 times, going to Hotspot"
	elif echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
                echo "No Device Reported, try " $j
        elif echo "$ssidreply" | grep "Network is down (-100)" >/dev/null 2>&1 ; then
                echo "Network Not available, trying again" $j
                j=$((j + 1))
                sleep 2
	elif echo "$ssidreplay" | grep "Read-only file system (-30)" >/dev/null 2>&1 ; then
		echo "Temporary Read only file system, trying again"
		j=$((j + 1))
		sleep 2
	elif ! echo "$ssidreply" | grep "resource busy (-16)"  >/dev/null 2>&1 ; then
               echo "Device Available, checking SSid Results"
	else #see if device not busy in 2 seconds
                echo "Device unavailable checking again, try " $j
		j=$((j + 1))
		sleep 2

for ssid in "${ssidsmac[@]}"
     if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
	      #Valid SSid found, passing to script
              echo "Valid SSID Detected, assesing Wifi status"
              return 0
	      #No Network found, NoSSid issued"
              echo "No SSid found, assessing WiFi status"

	#if no wifi device,ie usb wifi removed, activate wifi so when it is
	#reconnected wifi to a router will be available
	echo "No wifi device connected"
	wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
	exit 1


#Create Hotspot or connect to valid wifi networks
if [ "$ssidChk" != "NoSSid" ] 
       if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
       then #hotspot running and ssid in range
              echo "Hotspot Deactivated, Bringing Wifi Up"
              wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
       elif { wpa_cli -i "$wifidev" status | grep 'ip_address'; } >/dev/null 2>&1
       then #Already connected
              echo "Wifi already connected to a network"
       else #ssid exists and no hotspot running connect to wifi network
              echo "Connecting to the WiFi Network"
              wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
else #ssid or MAC address not in range
       if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
              echo "Hostspot already active"
       elif { wpa_cli status | grep "$wifidev"; } >/dev/null 2>&1
              echo "Cleaning wifi files and Activating Hotspot"
              wpa_cli terminate >/dev/null 2>&1
              ip addr flush "$wifidev"
              ip link set dev "$wifidev" down
              rm -r /var/run/wpa_supplicant >/dev/null 2>&1
       else #"No SSID, activating Hotspot"

and save (ctl & o) and exit (ctl & x)

For the autohotspot script to work it needs to be executable. This is done with the command

sudo chmod +x /usr/bin/autohotspot

Thats it, you are ready to go. Now test that everything is working ok.

Thanks to Willem Me and Tino for there contributions to the development of this script.

Testing the Hotspot

To test that the RPi is setup ok reboot the RPI. When the desktop returns you should have the wifi icon Raspbian Pixel WiFI icon by the clock.
For those setting this up on a headerless RPi then you should see the pi on your routers network. 

To test the hotspot works ok edit the ssid name of your router in the wpa_supplicant file.


sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and add 'off' to the end of your routers ssid. For example:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={ ssid="mySSID1" psk="myPassword" key_mgmt=WPA-PSK }

change to

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={ ssid="mySSID1off" psk="myPassword" key_mgmt=WPA-PSK }

save (ctrl & o) and close (ctrl & x) the file

Now reboot the Raspberry Pi.

Once the RPi is up and running the wifi icon near the clock should now be two red crosses Raspbian Pixel AP mode icon This means it is an access point, it could also mean your network is down but other devices should detect the hotspot. On a Tablet, phone or Laptop scan for wifi signals. You should see one for RPiHotSpot.

Select this as the wifi signal to connect to. The password is what you setup in the hostapd.conf file. From my example it is 1234567890

Wifi Connections-android

Local wifi signals in range on Android. Will be RPiHotSpot not RPI3Hot

For SSH and VNC the connection ip is also if you have setup the Rpi as a webserver use the same ip to see the webpage.

For ssh use ssh pi@

For vnc use

 Once you are happy the setup is working ok change the wpa_supplicant.conf file back to your routers ssid. From the next reboot the autohotspot script will manage your wifi connection.

If you find this guide useful and wish to show your appreciation then you are welcome to make a donation or share a link to this article. There is no obligation to do so, this guide is free for use and support is available to everybody as long as I know the answer :)


Setting up a Timer

If the autohotspot script is set to be run at set intervals, when you go out of range of your home router it will automatically generate a hotspot and then when you get back in range it will deactivate the hotspot and connect to the routers wifi. If you run a program in the background or use programs like tmux then any software you are running will keep working.

If you have an active ssh or vnc session running when the switch happens it will be lost.

Using a Cron job, tasks can be set off automatically at certain times, dates or intervals. For my use running the script every 5 minutes is fine but this can be changed to your needs. To setup a cron task enter the command

crontab -e

At the bottom of the file enter the command

*/5 * * * * sudo /usr/bin/autohotspot >/dev/null 2>&1

There is a space after each entry and * except the first *.

The first * position is for minutes. If you want it to check every minute just use * instead of */5

If you want to use hours, say every 2 hours enter it as

* */2 * * * sudo /usr/bin/autohotspot >/dev/null 2>&1

 Save the cron tab with ctrl & o and close it with ctrl & x

The cron job will automatically start straight away.

Disable Cron Timer

If you no longer need the timer running edit the cron with

crontab -e

and put a # infront so it is now

#*/5 * * * * sudo /usr/bin/autohotspot

The script will now only work at boot up or if you manually run the autohotspot script with the command

sudo /usr/bin/autohotspot

Note on Permissions:

by default the pi user does not need to enter a password to use the sudo command. If you are not using the default pi user and while doing this guide you have had to enter a password every time sudo is needed then you will find the the cron job will not work as it wont have permission to run the autohotspot script.

To fix this you can give the user permission to run the script without a password.
Enter the command

sudo visudo

enter your password
at the bottom of the file add the following line, where username is the user that will be using the script:

username ALL= NOPASSWD: /usr/bin/autohotspot

Now you will be able to run the script with sudo with the cron.

Thanks to Jim for pointing this out as an issue for some users.


If you are not using the default PI user and you are still have issues then check your default Paths from CRON for your user. It will need /sbin/ for iw.

Adding the path line to the cron will tell it were to look, PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Thanks to Atanas Atanasov for highligting this for non PI users



Script Removal

If you don't wish to continue using the autohotspot script then the Raspberry Pi can be revered back to a standard wifi setup with the following steps.

Disable the script with the command

sudo systemctl disable autohotspot


In the /etc/dhcpcd.conf file remove the line that was added

nohook wpa_supplicant


If you had previous config in your interfaces file and made a backup you can restore your original interfaces file with the command

sudo mv /etc/network/interfaces-backup /etc/network/interfaces

Then reboot.

The autohotspot script will no longer have control of you wifi. Dnsmasq and hostapd can be uninstalled if you no longer need them.

 Trouble Shooting

  • If you get no wifi connection or no hotspot  then it is most likely the autohotspot script is not executable or the service has not been enabled

redo the follow commands

sudo chmod +x /usr/bin/autohotspot


sudo systemctl enable autohotspot.service


  • You are in range of your router but it only creates a hotspot. If there is an issue with connecting to the router, such as the password is wrong. The script will fall back to the hotspot so you still have some type of connection. Check your password in the wpa_supplicant.conf file. 
  • You can connect to the hotspot via an Android Phone but you can't get a ssh connection. Some users have found this issue where Android uses there data connection rather than the wifi. Disabeling data has allowed them to use ssh.   
  • If this setup is not working as expected you can check the script for errors by running it manually in a terminal window with the command, sudo /usr/bin/autohotspot ,you can also check the service status with, sudo systemctl status -l autohotspot ,and if the hotspot has failed try, sudo systemctl status -l hostapd
  • You need to add a new wifi network to the RPi but it is in Hotspot mode so you are unable to scan for new wifi signals. You will need to add the new network to /etc/wpa_supplicant/wpa_supplicant manually. Enter the following details replacing mySSID and myPassword with the correct details. If your router has a hidden SSID/not Broadcast then include the line;  scan_ssid=1
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

network={ ssid="mySSID1" psk="myPassword" key_mgmt=WPA-PSK }
  • Unable to Authenticate the RPIHotSpot connection from another device. Make sure the password for the wifi connection is the one in the hosapd.conf file and not your Raspberry Pi's user password. The default pasword is 1234567890
Last modified on Saturday, 29 June 2019 11:08
roboberry SuperUser.  Contactable via the site admin e-mail in the Contact Us link.


Leave a comment

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


  • Comment Link JIm Wednesday, 05 September 2018 17:06 posted by JIm

    If you have a network in wpa_supplicant.conf that has a psk with spot will fail to connect to a valid network in wpa_supplicant.conf and error with "Failed to connect to non-global ctrl_ifname: wlan0 error: No such file or directory" and then return to the Hotspot:

    Sep 05 01:20:50 autohotspot[395]: Connecting to the WiFi Network
    Sep 05 01:20:51 autohotspot[395]: Checking WiFi connection ok
    Sep 05 01:21:11 autohotspot[395]: Failed to connect to non-global ctrl_ifname: wlan0 error: No such file or directory
    Sep 05 01:21:11 autohotspot[395]: Wifi failed to connect, falling back to Hotspot.
    Sep 05 01:21:11 autohotspot[395]: Creating Hotspot

  • Comment Link roboberry Tuesday, 17 July 2018 14:24 posted by roboberry

    Hi CT83, There shouldn't be an issue with your SSID, underscores and full stops should be fine. Though if there is a connection issue it will fall back to the hotspot which is probably the issue.
    You can try running the autohotspot script manually and you will see feedback and any issues. The first part shows the SSID's it is checking from wpa_supplicant so you will see if your SSID is being checked in full. If not then that is the issue.

    As a test if you use the manual SSID feature near the top of the script
    #ssids=('mySSID1' 'mySSID2' 'mySSID3')
    change to
    then comment out the lines ablve with a #
    #wpassid=$(awk '/s..........

    This will stop checking wpa_supplicant and use the manual entry, so the format is not an issue.

    If that works the I can only suggest you change the SSID, if possible, and reverse the changes.

    The RapiConfig and wpacli will fail because wpa_supplicant is not running.

    One other to check is if you have created your wpa_supplicant file manually has it got the correct config at the top? If not the connection will also fail.

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    let me know how you get on and let me know the details if you have further issues.

  • Comment Link CT83 Saturday, 14 July 2018 17:12 posted by CT83

    I have followed all the steps. Now I can successfully create a HotSpot and connect to it. But my RPi Zero won't detect existing saved WiFi Networks, my SSID is 'CT83_WiFi_2.4G' are any characters causing problems?

    I tried `sudo raspi-config` >> 'Network Options' >> 'WiFi', I got `Failed to Communicate with wpa_supplicant` error.
    I tried `wpa_cli` in the terminal and it was stuck at retrying.

    I am now stuck forever in Hotspot Mode? any ideas?

  • Comment Link roboberry Friday, 13 July 2018 20:07 posted by roboberry

    Hi cristofe47, thanks for pointing that out, fixed now. Thank you.

  • Comment Link roboberry Friday, 13 July 2018 15:10 posted by roboberry

    Hi Ovidiu, This issue was highlighted from user feedback as a bug, though as yourself I personally haven't had an issue either but is probably dependent on setup. The version of dnsmasq of 2.76-5 is behind on Raspbian. The latest version is 2.79. The bug was fixed in 2.77 but we have yet to get to that version in the Raspbian release. Once we finally catch up I can remove the warning.

  • Comment Link Ovidiu Constantin Friday, 13 July 2018 09:03 posted by Ovidiu Constantin

    Hello roboberry,

    You have a little mistake in preamble when you say : "versions 2.77 and above are ok".
    Can you correct that?

    More recent dnsmasq version is 2.76-5
    I discover that when I try to apply your solution on my RPiZW.
    With dnsmasq 2.76-5 all works very well.

    Thank you.

  • Comment Link cristofe47 Tuesday, 10 July 2018 18:34 posted by cristofe47

    Thank you for a super useful script!

    The script linked in the section "autohotspot service file" is different from the script in the text:

    in the link:

    in the text:

    The correct version should be "ExecStart=/usr/bin/autohotspot" as the rest of the script refers to autohotspot and not autohotspotN


  • Comment Link roboberry Monday, 11 June 2018 20:40 posted by roboberry

    hi 600burger, Thanks and i'm glad you find it useful.
    I have not come across /r in the wpa_supplicant file. I presume it was one where wpa_supplicant was put in the root folder on an new SD, can you let me know what system or software the file was created in?

    Thanks for highlighting the issue and your fix. I will see if I can squeeze it into the awk command. White spaces will be an issue as they are allowed in the ssid.


  • Comment Link 600burger Sunday, 10 June 2018 06:36 posted by 600burger


    Awesome script - will be using with many projects in the future!

    I actually had the same issue Sayed seems to have. It turns out the ssid variable(s) had an extra '/r' character on them and was breaking the SSiD check around line 110.

    My hack was to add:
    cleanssid=$(echo $ssid | tr -d '\r')

    and change:
    if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1

    if (echo "$ssidreply" | grep $cleanssid) >/dev/null 2>&1

    I assume there is a better way to get this done (for /n and maybe white space?) on line 18 - but I don't have the intelligence or beer to debug that awk/sed pipe.

    Hope this helps someone else along the way!


  • Comment Link roboberry Wednesday, 06 June 2018 13:02 posted by roboberry

    Hi Frank
    Your welcome :)
    The ip address is set in the autohotspot script in the section
    echo "Creating Hotspot"
    ip link set dev "$wifidev" down
    ip a add brd + dev "$wifidev"
    ip link set dev "$wifidev" up
    dhcpcd -k "$wifidev" >/dev/null 2>&1
    systemctl start dnsmasq
    systemctl start hostapd
    you have quoted the ip for the internet version of the script! but it is in the same place

    if you are using 192.168.50.# then you can just change the last number in this script.
    If you need to change any other part of the ip address then you also have to make the changes in the /etc/dnsmasq.conf file.

    This line dhcp-range=,,12h need to be changed to the same first 3 numbers as what you put in the autohotspot script.

    So if you wanted ip

    change the autohotspot entry to
    change dnsmasq.conf to

    the port number :5900 is added by VNC so not part of the setup.

  • Comment Link Frank Wednesday, 06 June 2018 06:44 posted by Frank

    thank you for this is a great script which worked first time. But i do have a wuestion: Where in the script (or anywhere else) do you set the ip to how can I change this?
    Thank you again

  • Comment Link roboberry Wednesday, 30 May 2018 09:09 posted by roboberry

    Hi Peter, yes your write, I changed the image missed the text forgot the text. Updated now.

  • Comment Link roboberry Wednesday, 30 May 2018 09:00 posted by roboberry

    Hi Jan VdA, That is a feature I have in development, it will be an adaption of a script another user gave me for this purpose but needs to be adapted for general use. The concept is the same but uses Python Flask to run the webpage from bootup but is accessible when the hotspot is active. It currently overwrites wpa_supplicant but will be adapted to alter or add to wpa_supplicant and only be active while the hotspot is active.
    Once the setting have been change it runs the autohotspot script and it will switch without rebooting.

    This seems a good option to me as you don't need to run a full webserver and a python script manages the updating of the network settings.

  • Comment Link roboberry Wednesday, 30 May 2018 08:48 posted by roboberry

    Hi Jake, when the pi is running run the script and see what feedback you get, sudo /usr/bin/autohotspot this will output what it's doing.
    Also by entering sudo systemctl status autohotspot or sudo journalctl -xe you will see what the output was from last time it run via the service file.

    If you are getting neither network or hotspot and the service file showed it ran at boot check the permissions of the script sudo chmod +x /usr/bin/autohotspot

    Let me know how what responses you get and I will get you up and running.

  • Comment Link Peter Wednesday, 30 May 2018 07:51 posted by Peter

    Is it a typo, that it should show " two arrows facing opposite directions"? Isn't it like the Internet version with showing just two crosses?

  • Comment Link Jan VdA Tuesday, 29 May 2018 11:13 posted by Jan VdA


    This is all very very interesting.

    I am actually interested in a very related use case.
    So the idea is that
    1. when the "wifi hotspot" is active that also a simple website is active on the raspberry pi that allows to set/change the wifi network SSID, passport (maybe also its static IP address). So by connecting to the wifi hotspot and navigating to that local website you can modify the wifi network settings.
    2. I can create that simple website using node-red.
    3. but then I need a way (or script) that allows to change or add those new wifi network settings so that next time it is rebooted that it tries to connect using those wifi network settings.

    So this makes it possible to take your headless raspberry pi along and get it easily connected to any wifi network.

    So if you have any advice about step 3 that would be very helpfull.


  • Comment Link Jake Sunday, 27 May 2018 18:13 posted by Jake

    Thanks for putting together this. Exactly what I need! Unfortunately cannot get it to work on stretch. Followed exactly and will not connect to existing network. Switched off wifi, rebooted and no hotspot created either. I've reflashed and tried again with no joy.

    Is there anything I can try? How do you access the logs without remote access?

  • Comment Link roboberry Saturday, 12 May 2018 21:00 posted by roboberry

    Hi Jordan

    in the file /etc/dhcpcd.conf go to the bottom of the file and add in
    nohook wpa_supplicant

    This usually fixes some issues that some people get.
    I am currently testing an update that I expect to have posted in the next week which may help if that dosn't.

    but let me know if you still have issues.

  • Comment Link Jordan Saturday, 12 May 2018 02:13 posted by Jordan

    I am trying to get this to work on a Raspberry Pi W.

    My phone connects, then gets stuck at "Obtaining IP address..."

    Here is the log from the pi:
    May 12 00:18:53 waterboy avahi-daemon[233]: Registering new address record for on wlan0.IPv4.
    May 12 00:18:55 waterboy dhcpcd[357]: wlan0: no IPv6 Routers available
    May 12 00:19:32 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX IEEE 802.11: associated
    May 12 00:19:32 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX RADIUS: starting accounting session 5AF632E2-00000000
    May 12 00:19:32 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX WPA: pairwise key handshake completed (RSN)
    May 12 00:20:08 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX IEEE 802.11: disassociated
    May 12 00:20:08 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX IEEE 802.11: disassociated
    May 12 00:20:12 waterboy hostapd: wlan0: STA XX:XX:XX:XX:XX:XX IEEE 802.11: associated

    When I run the autohotspot script by hand (after disabling the service) I get:
    Hotspot already active

    any help would be most appreciated!

  • Comment Link roboberry Wednesday, 25 April 2018 19:21 posted by roboberry

    Hi jcestefan, Thanks for the feedback, Unfortunately that seems only to be an issue for some users, bit looking into what could be the cause.
    I'm glad you find the script useful :)

  • Comment Link jcestefan Tuesday, 24 April 2018 23:42 posted by jcestefan

    Thanks roboberry.

    I had issues like Adam R. but has been fixed with adding in /etc/dhcpcd.conf (at bottom):
    nohook wpa_supplicant

    Thank you again! and thanks for this amazing setup!

  • Comment Link roboberry Thursday, 19 April 2018 13:51 posted by roboberry

    Hi John S, you're right, seems a typo has got in. Thanks for highlighting that.

  • Comment Link roboberry Thursday, 19 April 2018 13:49 posted by roboberry

    Hi Shawn S, other users have reported a similar issue but I have not been able to replicate it to look into it properly as it seems to only affect some users.
    The ip is only referenced in the autohotspot script when a hotspot is activated so if no hotspot has been generated it can't be assigned.
    When it goes from the hotspot to the network th ip is cleared so shouldn't show.
    It it is possible that dnsmasq is running while you are connected to the router, if it is you do see both ip's but network wifi takes priority.
    could you check that with sudo systemctl status dnsmasq
    and let me know as I would like to get to the bottom of this. If that is not the case.
    The country code is important for wifi and this not being set for some users has caused issues.
    in /etc/wpa_supplicant/wpa_supplicant.conf is should start with
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    where GB is your country.
    also in /etc/hostapd/hostapd.conf add in the country code as well.
    another thing to try is let the autohotspot script start up wifi instead of dhcpcd. dhcpcd will still work it just won't start network wifi.
    in /etc/dhcpcd.conf add the line
    nohook wpa_supplicant
    The resolve.conf file should get updated every time wlan0 is brought down and up again and when dhcpcd is started or refreshed so I think this is a symptom of why you are seeing bot IP addresses.
    try entering; sudo dhcpcd -n wlan0
    which will refresh dhcpcd network and see if that makes it work, it's not a fix but let me know if it makes a difference.
    Let me know how it goes.

  • Comment Link Shawn S Sunday, 15 April 2018 05:05 posted by Shawn S

    seems like my issue is similar to posted by rustyferret

    When my PI is connected to my wifi router, in the resolv.conf, the name server shows and not getting updated i believe for some reason.

    i checked for the hooks- it shows as follows
    /lib/dhcpcd/dhcpcd-hooks $ ls
    01-test 02-dump 10-wpa_supplicant 20-resolv.conf 30-hostname 50-ntp.conf

    any clues on what i should be looking at next?

    Shawn S

    (sorry i posted this on the wrong page earlier where you have the instructions for auto hotspot and wifi with internet connection- what i am really looking for is di

    Thanks for putting together a great tutorial. I think I followed all the steps but the issue I am facing is after my PI reboots, it assigns itself a static IP of which I believe is the IP it uses in the hotspot mode. I have confirmed that it is indeed connecting to my WiFi router which should assign the IP an ip in the range of 192.168.1.x

    For some reason it seems after following all these steps, the PI is not using dhcp client when connecting to the WiFi router .

    Any idea what I may have missed?

    Some additional info:

    The switching between the hotspot and WiFi client mode seems to be working. I can connect to the pi with in the hotspot mode.

    In the WiFi mode, I checked in the router and it seems like it does assign to MAC of the pi. So I tried using that to connect to the PI and it works with vnc and ssh

    After connecting to PI, I did command IP addr on the terminal and it shows first and the as the 2nd ip. So looks like in the WiFi mode, 2 IP addresses are getting assigned to the wlan0 interface.

    when i do ifconfig, even when it is connected to my wifi router, it only shows the ipaddress

    Is this expected ?

    The issue this is causing is I cannot connect to my pi using the hostname (rpi.local) as it does not seem to get broadcasted on the 192.168.1.x network (my WiFi routers lan). It seems like it is getting broadcasted on 10.x network

    Also from my PI, when it is connected to my wifi router, DNS does not seem to work. from the PI terminal, i can ping and i get responses back. But when i ping, responses time out. I cannot go to any websites on the web browser.

    Shawn S

  • Comment Link John S. Friday, 13 April 2018 20:21 posted by John S.

    I've been testing out the autohotspot script and I've noticed that the variable "j" in the subroutine FindSSID() doesn't increment. I believe that the statement "j=$((j = 1))" should be "j=$((j + 1))" otherwise "j" will always be 0 or 1.

    Thanks for this project. It's really opened up the capabilities of the Pi for me.

  • Comment Link roboberry Monday, 02 April 2018 19:19 posted by roboberry

    Hi Markus, I don't think this is the issue, there has been recent reports of ssh connection issues for some users since the Raspberry Pi 3B+ was released. It may just need a line adding to the /etc/hostapd/hostapd.conf file for country code; country_code=GB
    or your country. But as I can't reproduce the bug myself I can't resolve it yet. Raspbian does not use dhcp for networking it used dhcpcd instead so though dhcp is installed it is not active. It looks like the dhcp -r command releases the lease. This should happen during the switch as the interface is brought down which will refresh the lease when dnsmasq and the interface comes up.

    If you haven't already set your wifi region in Raspberry Pi Configuration then update that, not that will change the hotspot situation but best to do that. And add the line to the hostapd.conf file. I will continue to see what changes are affecting some users.

  • Comment Link Markus Friday, 30 March 2018 16:00 posted by Markus

    Hi roboberry,

    thanks for that guide, it'll help a lot for our donkey car using a raspi.

    Unfortunately one thing was not working on my side. In case of being a wifi client the raspi connected to the hotspot on the smartphone but got (or requested) no ip address.
    After some searching I found out that this could be done using the command 'dhclient -nw "$wifidev" '. Then, in case of switching to hotspot mode, I kill the dhclient process in function createAdHocNetwork using command 'dhclient -r'.

    I refactored the wifi client start into a new function called "startWifiClient" including the wpa_supplicant call as well as the dhclient -nw call.
    Then I replaced all wpa_supplicant commands this the function call startWifiClient.

    In createAdHocNetwork at first I call the 'dhclient -r'.

    Now the switching works on my side. I don't understand why it works on your side without the dhclient calls.

    Best regards,

  • Comment Link roboberry Tuesday, 27 March 2018 22:01 posted by roboberry

    Hi Adam, I'm not sure if the issues you are getting ar e similar to Carlos on the other article, but just as a double check can you make sure dnsmasq and hostapd are disabled.
    sudo systemctl disable dnsmasq
    sudo systemctl disable hostapd

    then reboot.
    I have tried to replicate the issue but my PI's are running fine on the latest updates/firmware.
    When the pi starts the wifi is started as normal and then the autohotspotN script check the status of the wifi and makes changes if required. You can change that so the autohotspotN script starts wifi.
    enter sudo nano /etc/dhcpcd.conf
    and at the bottom of the file enter
    nohook wpa_supplicant
    and then save and reboot.

    Let me know if this resolves your issue.
    Unfortunately I can't test it on a MAC OS but if you still have issues could you see if you have an issue as well on another non MAC device.

    Let me know how you get on and i look into it further.

  • Comment Link Adam R. Tuesday, 27 March 2018 21:25 posted by Adam R.

    A little more info on the issue... I was looking at the internet enabled version of this guide, and noticed one of the commenters there had the same issue, but was able to get it working by pinging the connecting device from the RPi. I did the same thing and it started working.

    I am not sure what happens during that process, but it appears to fix the issue. Obviously taking that step is not practical based on the real world use case of this AP mode script, but maybe it will help pin down what causes the problem.

    Thanks again!!!

  • Comment Link Adam R. Tuesday, 27 March 2018 13:23 posted by Adam R.

    Thanks for the script! I am seeing a strange issue and wonder if you can help. The AP comes up as expected when the network in wpa_supplicant is not found or can't connect, but when I connect to the AP from my Macbook (High Sierra), I can only SSH when my Macbook is waiting for an IP to be assigned. While it is waiting for its DCHP lease, I can ssh, however, as soon as the IP address is given to my Macbook, the address stops responding entirely.

    When I go into Network Preferences on my Macbook, I see that the router address is, but still can't hit the RPi.

    Thanks in advance!

  • Comment Link roboberry Thursday, 22 March 2018 19:52 posted by roboberry

    Hi Chris. I would expect that Ethernet would still be available through the routers network so no sure why you didn't get access. I will have to see if something has changed.

    Regarding the web config interface I have this typr of thing in development. A user wrote a script in python to do that so a webserver setup is not required. I need to finish some modes to make it useful for general use but it will be in a future update. A bit delayed due to the release of the PI3 B+ as I'm testing setups with that at the moment.

  • Comment Link chris Thursday, 22 March 2018 15:41 posted by chris

    Thanks for the follow up roboberry. I assume the reason that doing SSH over ethernet didn't work for me was that my script was broken (with the empty first line). Good to know I can SSH over ethernet now that I've got things working properly.

    One thing I realized I'd really like to make available is a web configuration interface. I found this project on GitHub, but wasn't sure how to combine it with your terrific script. -

    I think it would be an awesome addition to the hotspot setup to allow easy configuration via browser to get the PI to join a new wifi network.

  • Comment Link roboberry Saturday, 17 March 2018 11:21 posted by roboberry

    Hi Chris, I'm glad you have managed to find the issue and got it working, it's often the little things that cause big issues. The Direct script has no Wifi to Ethernet routing for the hotspot to get Internet access but you can still ssh through the Ethernet port from another device on the network to gain access to the pi. The other script is the one that has Ethernet routing as well.

  • Comment Link Chris Saturday, 17 March 2018 00:56 posted by Chris

    I figured out the problem. I had an empty line at the start of the bash script file. Broke everything. Fixed and all set. Please ignore my previous comments. I think this would be a helpful check for noobs like myself.

  • Comment Link chris Friday, 16 March 2018 22:49 posted by chris

    Hi roboberry. I was able to get back into the pi zero w (headless) running latest Stretch Lite by adding a new wpa_supplicant.conf file to the boot volume. I did some of the troubleshooting steps and had the output below:

    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    /usr/bin/autohotspot: 23: /usr/bin/autohotspot: Syntax error: "(" unexpected
    pi@raspberrypi:~ $ sudo systemctl status -l autohotspot
    ● autohotspot.service - Automatically generates an internet Hotspot when a valid ssid is not in range
    Loaded: loaded (/etc/systemd/system/autohotspot.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Fri 2018-03-16 21:25:29 UTC; 1h 16min ago
    Process: 583 ExecStart=/usr/bin/autohotspot (code=exited, status=203/EXEC)
    Main PID: 583 (code=exited, status=203/EXEC)
    Mar 16 21:25:29 raspberrypi systemd[1]: Starting Automatically generates an internet Hotspot when a valid s
    Mar 16 21:25:29 raspberrypi systemd[1]: autohotspot.service: Main process exited, code=exited, status=203/E
    Mar 16 21:25:29 raspberrypi systemd[1]: Failed to start Automatically generates an internet Hotspot when a
    Mar 16 21:25:29 raspberrypi systemd[1]: autohotspot.service: Unit entered failed state.
    Mar 16 21:25:29 raspberrypi systemd[1]: autohotspot.service: Failed with result 'exit-code'.
    lines 1-11/11 (END)
    pi@raspberrypi:~ $ sudo systemctl status -l hostapd
    ● hostapd.service - LSB: Advanced IEEE 802.11 management daemon
    Loaded: loaded (/etc/init.d/hostapd; generated; vendor preset: enabled)
    Active: inactive (dead)
    Docs: man:systemd-sysv-generator(8)

    The second line with the unexpected "(" seems significant. But I'm not sure how to go about fixing this. Any advice would be greatly appreciated.

  • Comment Link Chris Friday, 16 March 2018 14:46 posted by Chris

    Thanks so much for creating this setup. I've been looking for a solution like this to be able to use my Pi Zero W as a mobile Airplay receiver. I did have some issue when setting up the script and upon testing there is no hotspot network created. Since I'm running headless, I thought I would try to plug in an ethernet adapter and get back into the Pi, but that didn't work and I now see that this script doesn't factor in ethernet in any way (am I right?). I can always fall back to hooking up a keyboard, mouse and monitor, but is there any other way to get into the Pi Zero W (with or without ethernet adapter) and try to fix my mistake? You know.. aside from completely reinstalling Raspbian Stretch?

  • Comment Link roboberry Friday, 02 March 2018 20:15 posted by roboberry

    Hi Simmo,

    The setup for Jessie was fine, unfortunately you have to do things differently for Stretch.

    The RTNETLINK answers: File exists in this situation usually refers to the file at /var/run/wpa_supplicant/wlan0

    When wifi network starts through wpa_supplicant a file is created with the device name. When Wifi is ended through the GUI by the clock or termination via wpa_cli then the device file in /var/run/wpa_supplicant is cleared.
    The script double checks that it has been removed by specifically deleting it just in case, before it goes to a hotspot.

    If wpa_supplicant exits unexpectedly or the interface is deactivated by another process or a second instance of wpa_supplicant is running then the device file is not cleared. The hotspot cannot bring wlan0 back up with, ip link set dev wlan0 up, if the device file still exists.
    So no device file should exist in /var/run/wpa_supplicant before the hotspot is activated.

    As for the cause, though probably unlikely, while your wifi is running check you only have one instance active with the command: pgrep wpa_supplicant
    if you see more than one number then you have multiple instance and that is the issue.
    Check that there is no reference to wpa_supplicant in the /etc/network/interfaces file, usually empty on stretch.

    If you have one instance then, check that the path in the top 3 lines of you /etc/wpa_supplicant/wpa_supplicant.conf file shows: ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    if it doesn't then the file is not created as expected.

    In general if the file does exist, rebooting resets everything and it will work again. As I presume this is not happening for you there is some other setup issue somewhere.

    I have not been able to reproduce your issue. I presume your sequence of events is edited as some feedback is missing, so if this doesn't help can you send me the output of the manual switches, and I try to find the issue.

  • Comment Link ben simmo Thursday, 01 March 2018 20:49 posted by ben simmo

    Not working for me (everything copy and pastes and then double checked again)
    WiFi normal connects, and I can turn it on and off
    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    Wifi already connected to a network
    pi@raspberrypi:~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    Cleaning wifi files and Activating Hotspot
    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    RTNETLINK answers: File exists
    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    RTNETLINK answers: File exists
    pi@raspberrypi:~ $ sudo chmod +x /usr/bin/autohotspot
    pi@raspberrypi:~ $ sudo systemctl enable autohotspot.service
    pi@raspberrypi:~ $ sudo /usr/bin/autohotspot
    RTNETLINK answers: File exists
    pi@raspberrypi:~ $

    and tried various combinations of this, even purged and removed dnsmasq, the dns-root-data (didn't the first time as assumed the warning was for older Raspbian (this is the latest Stretch fully up to date and not a lot else on it)
    Didn't realise there is actually no 2.77+ version available to us on Raspbian Stretch)

    having no luck, there seems to be no actually messages to help with debug just those given above?
    Help, I used to use the old version way back when and that worked great in good old Jessie.

  • Comment Link Dan Tuesday, 20 February 2018 03:50 posted by Dan

    I really would like to get the web script peter wrote to add APs on the fly via a web interface while in hotspot mode. I understand you want time to edit it to make it general for everyone, but can I PLEASE get what you or Peter has now. I'll probably hack it to bits to customize it anyway.

    Thanks for the tutorial. Great work!

  • Comment Link roboberry Monday, 19 February 2018 21:22 posted by roboberry

    Hi Sayed, I have emailed you a script which shows more feedback to debug the issue.

  • Comment Link Sayed Zishan Ali Monday, 19 February 2018 04:45 posted by Sayed Zishan Ali

    Thanks for the reply roboberry, I already did what you said. Un-Commenting those four line is working as expected the script is able to find my SSID and connect to that easily.
    I triple checked my SSID format it is in quotes and has no any special character even i tried creating some fresh mobile hotspots ans put those in wpa_supplicant file but no luck.
    My Wifi SSID: TP-LINK_6264 (i hope "-" and "_" should work)
    My mobile hotspot SSIDs: mpcg, Zinix, Ziniks11 etc.

  • Comment Link roboberry Saturday, 17 February 2018 20:14 posted by roboberry

    Hi mwimpennys , thank you and you're welcome

  • Comment Link roboberry Saturday, 17 February 2018 11:27 posted by roboberry

    Hi Sayed, thanks for your comments and I'm glad you found the guide easy to follow.
    The issue Mark had was that the wpa_supplicant.conf file was missing some of the top lines which are important to it working. It should start like this:
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    the country should be your country if your not in the UK.
    The line you edited changing; if (echo "$ssidreply" | grep "$ssid") basically says show the found ssid signal in range and compare it to your wpa_supplicant ssid list. By changing grep to echo this will always be true regardless of the results of the previous checks.
    There may be somthing in your ssid that is not compatable with the script. What format is your ssid in your wpa_supplicant file? it shoudl be in quotes and not contain commas or apostrophies as they will be removed.
    so ssid="mySSID1" and ssid="mySSID 1" is ok but ssid="mySSID'1" will fail.

    Try the feature where you add your own ssid to the script to try this;
    Near the top of the script add a # to the start of the four lines below IFSdef=$IFS

    and remove the # on this line
    #ssids=('mySSID1' 'mySSID2' 'mySSID3')
    and add your ssid to the first entry, somthing like
    ssids=('yourssid' 'mySSID2' 'mySSID3')

    Try that and it should find your wifi network ok. Let me know what happens and the format of your ssid is.

    I will be adding the script from Peter but I need to modify it for general use as it is for his specific requirements but will be made available.

  • Comment Link @mwimpennys Friday, 16 February 2018 22:31 posted by @mwimpennys

    Works a dream! Thanks for putting it together, great work!

  • Comment Link Sayed Zishan Ali Wednesday, 14 February 2018 05:08 posted by Sayed Zishan Ali

    Hi Roboberry,

    First of all thank you so so much for creating this script very nice tutorial for all of us. It is very well written and its very nice that you answer all questions and work hard to fix the problems we have with this script.

    I had the same issue as Mark had. I installed several versions of Raspbian Jessie & Stretch and i went through all the comments but nothing was working for me even i tested the script on pi3 and zero W as well but on the reboot it was always creating the hotspot. I am very new to RPi and script so i don't know much about it. Yesterday i was trying to see what happening in your script i and found this line "if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1" once you pointed it out for Mark. I edited (grep "$ssid") to (echo "$ssid") and it worked for me without any issue. I googled for grep command and found out it is for Searching text in a file or something. So please guide me if i did something wrong. I also need the script which Peter Allday sent you for adding a web service and web page that gets served up on boot, and displays the list of available networks.

    Thanks again sir.


  • Comment Link Ed Wednesday, 07 February 2018 21:00 posted by Ed

    Hi, Just wanted to thank you for sharing these instructions. Although I am not wifi or Linux literate I was able to follow your tutorial without any big issues. I have however run into a showstopper.

    My home setup consists of a RPI Zero W running the latest Raspian OS and a TP-LINK dual band router (uses the 192.168.0.XXX IPs). The home wifi for the 2.4 band is currently on channel 11. Prior to making any changes I was able to connect to the PI from both an Android tablet and Windows 10 computer using Real VNC. I was also able to see the PI on my network using the EZ File Manager app.

    After following your tutorial I powered down my PI and turned off my router. I powered up the PI and it created a hotspot network. I was able to sign on to the hotspot with my Android tablet and connect to the PI using VNC per your instructions. I then powered off the PI and turned my router back on, powered up the PI and it connected to my home network. Very happy camper at this point.

    However, after I signed back on to my home network, and attempted to connect to the PI thru VNC the connection failed on both Windows and Android and I could no longer see the PI using EZ File Manager when I did a scan of my home network. I retried over several hours with no luck.

    I then followed your directions and disabled your script. When I rebooted I was immediately able the connect on both Windows and Android thru VNC.

    I'd love to get this resolved because it would be a great option when I am traveling and just need a local network (without internet) to wirelessly connect several devices.

    Again, thanks for your work. If you have suggestions, or can point me to something to check, or need any additional info please let me know. Ed

  • Comment Link roboberry Wednesday, 07 February 2018 19:23 posted by roboberry

    Hi RustyFerret, your issue sounds like your dns name server is not set is being very slow to be set. This is usually done automatically by dhcpcd or dnsmasq. Though for this setup the dnsmasq.config is set to no-resolv as we don't need to use a dns, as it doesn't go online for the hotspot. But when networking is activated via wpa_supplicant and dhcpcd the /etc/resolv.conf file is updated and the name servers are automatically added. So check that your file has a name server entry. My file contains the line
    which is actually my router as that will handle the name server calls and I believe quite common.
    Check your dhcpcd hooks to make sure the resolv.conf hook is active for dhcpcd
    cd /lib/dhcpcd/dhcpcd-hooks
    the folder should contain the file 20-resolv.conf

  • Comment Link rustyferret Monday, 05 February 2018 20:59 posted by rustyferret

    Hi :)

    Has anyone any ideas why I have a really long delay on accessing websites on my pi zero w when connected to my home router? I have gone over the steps 4 times and I'm sure I've got everything right! (I'm fairly new to linux OS)

    When I try access a site via a web browser I get 'The web page at might be temporarily down or it may have moved permanently to a new web address' ERR_NAME_RESOLUTION_FAILED

    This lasts for around 2-4 minutes and then the page will reload (or I will reload the browser) and google will show.

    When I ping I get 'Temporary failure in name resolution', however when I ping this seems to be fine. Once I've waited for the browser to finally show the webpage I can then ping google by domain name and it works.

    I've tried some pretty extensive googling to resolve this but with no luck. I'd love to understand what's gone wrong.

    Thanks in advanced

  • Comment Link roboberry Monday, 05 February 2018 19:58 posted by roboberry

    Hi Vinay, you can have as many entries in wpa_supplicant as you like, I personally have 3.
    The script will test if each ssid is in range and if at least one is, then the network wifi will be activated. If more than one ssid is in range then the script doesn't choose which one to connect to, wpa_supplicant chooses. If the ssids contain a comma or an apostrophe then it won't be found as these are stripped out of the SSID as part of the formatting but spaces are ok.
    Are you seeing an issue with your setup?

  • Comment Link Vinay Monday, 05 February 2018 17:28 posted by Vinay


    Thanks for this, I have a request though. How do I get this working with multiple sources, different SSIDs in the wpa_supplicant I mean?

  • Comment Link roboberry Tuesday, 30 January 2018 20:18 posted by roboberry

    Hi crisdeodates, I haven't tried streaming but VNC has always been fine on both. Another user had a similar issue when using UDP streaming so maybe there is a similar situation. When the pi starts up the normal system network is started, when the autohotspot script starts later in the boot process it will asses the situation and make the change. But you can delay the wif network starting by a few seconds and get the autohotspotN script to start it instead. This resolved his issue it just means the wifi starts later in the boot sequence.

    in the /etc/dhcpcd.conf file go to the bottom of the file and add the line
    nohook wpa_supplicant

    and reboot and try again. Let me know what happens and I will help further if you still have an issue.

  • Comment Link crisdeodates Tuesday, 30 January 2018 16:57 posted by crisdeodates

    Hi roboberry,

    Thanks for this tutorial. I installed autohospot by following the above steps. But i have an issue.

    From raspberry pi, i am streaming my webcam video. If i enable autohotspot and if i am connect to my wifi router, stream wont work. Also VNC is also not working.

    If autohotspot script is enabled and if i am in hotspot mode,both streaming and VNC seems to work.

    Could you please help me resolve issue this issue that comes when i am connected to my wifi while autohotspot script is enabled.

    Thanks in advance..

  • Comment Link roboberry Sunday, 21 January 2018 20:36 posted by roboberry

    Hi Xander, your welcome. Glad you find it useful :)

  • Comment Link Xander Saturday, 20 January 2018 19:35 posted by Xander

    Fantastic script. Also, a very well written tutorial. Thank you!

  • Comment Link roboberry Thursday, 04 January 2018 10:34 posted by roboberry

    Hi Peter, I sent you an e-mail :)

  • Comment Link Peter Allday Friday, 29 December 2017 13:48 posted by Peter Allday

    Hi Roboberry,

    I have finally got it to work after much fiddling and re-installing, and have extended it a bit. I have added in a web service and web page that gets served up on boot, and displays the list of available networks. You can then select a network and enter the password for it, and then it will edit the wpa_supplicant file and replace it with the new connection details, and then call the autohotspot command to connect to that network. I have just got it working, so may not be production ready.

    I don't know if you would find that of interest , if so send me an email and I can let you have the code that I have and install instructions.

    best regards


  • Comment Link roboberry Saturday, 23 December 2017 10:44 posted by roboberry

    Hi Agustin, There was a recent update in stretch that has caused the hotspot to be a bit temperamental on some devices. I have an Android Tablet and sometime it won't connect to the hotspot unless the pi's wifi is brought down and up again, which is now included in the script. But my windows laptop and other device are fine. I have observed that connections fail due to encryption protocol timeout issues but will then connect without any changes. So I believe the issue is outside my control but I will be looking at it in detail soon just in case a setting now needs modifying.

    As this script is the one without Internet it will be normal for the connected device to report "Connected, No Internet". I think it depends on the OS if it shows that or not. My Android Tablet always does but my wifes Ipad doesn’t. The other script that has internet routing will do the same until an Ethernet cable is connected to the RPI then it changes to just "Connected"

    Previously some users with Android phones have had connection issues because the phone routed the ssh call through data rather than wifi. Once they disabled the data for ssh then it started working, so maybe that's the same for VNC.

    I will update the article if I need to make changes, as this issues affects me too, hopefully in the meantime it doesn’t cause to many issues for you.

  • Comment Link roboberry Saturday, 23 December 2017 10:11 posted by roboberry

    Hi Rob, thanks for you feedback. I was aware if you repeatedly switch it will sometime make the wrong choice. As yet I haven't been able to investigate but was hoping to over the Christmas period. What I have seen does reflect your finding so probably the same thing.
    As the base function is to only switch at boot up it should not be a problem, but for the timer mode it would show up more but be ok on the next cycle.
    It seems like something is being locked so maybe it just needs more time between checks or an extra step put in place. I will update as soon as I have a fix.

  • Comment Link Agustin Lobo Friday, 22 December 2017 19:19 posted by Agustin Lobo

    Big thanks!
    I have installed down to "Setting up a Timer" (excluded) and it works, with no network (in the field), with my Android tablet
    through realVNC (that comes included in raspbian). I often have to turn off and on my wifi on the tablet several times until I get to connect with VNC. I observe that it works if I see the 2 arrows in the wifi icon.
    It also works fine from my MAC, both with ssh and VNC. It does take a very long time to the MAC to connect to the
    hotspot, though.
    Surprisingly, I cannot connect using other Android devices, nor from my PC (WIN 7 Pro). Even ping does not work.
    I observe that, once connected to the hotspot, the wifi icon states "no internet connection" in those devices that do not connect, while this statement is not present in my tablet 1 or my MAC. Is there something to be done in the client side? It looks like there would be some setting that is
    set by default in some devices and not in others.

  • Comment Link Rob Oldaker Thursday, 21 December 2017 19:07 posted by Rob Oldaker

    Hi roboberry,

    I have got a bit further now and have another slight quirk. I am using the autohotspot script to test whether the password is correct for a given wifi hotspot.

    In this situation the rPi is in "access-point" mode and I edit "wpa_supplicant.conf" file and enter an existing wifi hotspot with a password(i.e. I add or edit a "network={...}" entry). I then run the hotspot script to determine whether the password is OK. This nicely puts the rPi in "wifi" mode and tests whether it connected ok (i.e. the password was OK). If the password is NOT OK it will put the rPi back to "access-point" mode (which is really useful). In this case if I do the test again (i.e. just run the hotspot script again) it fails to perform the scan and finds no available hotspots. From what I can tell the call to "iw dev wlan0 scan ap-force ..." in FindSSID is returning "command failed: read-only file system (-30)". If I then run the script again its OK. It looks like when testing for a password you need to run the script twice - first to see if its OK and if it failed to run it again before using the script to perform further tests. Hopefully what I have said makes some sense.

    Fingers crossed you can re-create what I have found and maybe able to shed some light as to what is going on?

    Thanks for your time,


  • Comment Link roboberry Monday, 18 December 2017 13:59 posted by roboberry

    Hi Rob, that's good it worked, lucky guess :). The only difference is the autohotspot script will start wifi slightly later in the boot process than the dhcp server, and along with the 10 second delay in the autohotspot script to check if a network connection has been made, you may find that wifi starts as the desktop comes up or just after. The wlan0 interface will still be started as normal just the connection will be later.

    The 169.254.#.# ip is normal. It's a local ip from the dhcp server and shows if you hover the mouse over the wifi icon by the clock but will be the one to use. ip addr will report both. You can connect using ssh with either but I would stick with

    glad I could help.

  • Comment Link Rob Oldaker Monday, 18 December 2017 10:51 posted by Rob Oldaker

    Hi roboberry,

    Your instructions work! Adding "nohook wpa_supplicant" to "/etc/dhcpcd.conf" fixed the issue. I also double checked the problem is there without this fix.

    Works fine in "access point mode" as well so I can find no side effects with this change.

    One small quirk I found in "access point mode" was that my app thought its ip address was "" but this maybe something to do with my code (I am using "mono" library functions). If I run "ifconfig" it says the ip address is the expected "" and I can connect to my app using this ip address.

    Thanks for the quick reply,


  • Comment Link roboberry Sunday, 17 December 2017 23:13 posted by roboberry

    Hi Rob, that does sound odd, unfortunately I haven't used it with UDP services so that is not tested. When the pi first boots it will just use the standard wifi setup. As you say the autohotspot script just test the situation and only makes changes if the ssid id is not found.
    I will have a think about what the issue could be but what you can try is:
    when the pi starts the dhcpcd service starts wpa_supplicant and gets the network wifi going. You can change that so the Autohotspot script activates it instead. in /etc/dhcpcd.conf go to the bottom of the file and add the line
    nohook wpa_supplicant

    after a reboot the autohotspot script will in control of activating the wifi. see if that makes any difference.
    If you was using the internet version of the script I would suggest it that ip_forwarding is disabled during the script but as its the direct version no settings are changed until it goes to a hotspot.
    Let me know if that makes any difference,

  • Comment Link Rob Oldaker Saturday, 16 December 2017 18:11 posted by Rob Oldaker

    hi roboberry,

    Thanks for this script and very precise instructions. I have it working with my raspberry pi but thought I would point some slightly strange behaviour to see if you know what might be going on.

    I am writing an audio app that uses UDP messages to communicate with different rpi's running the same app. I have my test rpi connected to my local network using wifi. If I use your "autohotspot" service as is then the app stops working correctly. Even though it can broadcast udp messages ok it refuses to accept them - there are no error messages or anything, the messages from other rpi players simply don't get through.

    If I disable the service but run it manually after the pi account has logged in (i.e. run "sudo /usr/bin/autohotspot" from a command terminal) it works fine. This seems really strange as the autohotspot script really doesn't do much in this situation, it simply scand for the available hotspots and determines if there is an entry for at least one in the "wpa_supplicant" config file. When I run it manually I get the expected "Wifi already connected to a network" message.

    To test the script I have been using a fresh install of the latest raspbian stretch image (with desktop) (nov 2017).

    Thanks for your time,


  • Comment Link roboberry Saturday, 09 December 2017 21:28 posted by roboberry

    Hi Mark, well it is good you have found the issue. The wifi being brought down and up shouldn't be an issue, but you can comment them out and see what happens.
    All my PI's are fine except for the one that has the latest setup and for some reason the network need bringing up twice before the hotspot connect ok. All other situations are fine.
    You shouldn't loose you wifi after a period of time unless there is interference from other devices nearby like cordless phones, microwaves etc or too many routers in your area are using the same channel. If you router doesn’t from change busy channels on it's own then restarting it often gets it to change to a quieter channel.

  • Comment Link Mark Friday, 08 December 2017 06:01 posted by Mark

    Hi roboberry,

    I did what you asked and it wouldn't join my home network or create the hotspot with the edits. I followed your conversation with Steve and updated my wpa_supplicant.conf from:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    ssid="Word Word Word Word"
    psk="my password"


    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    ssid="Word Word Word Word"
    psk="my password"

    Now it works. Thank you. I think it might be good to add a note to the tutorial to make sure wpa_supplicant.conf has the update_config=1 and country=xx lines to it.

    It does seem to still have connection bugs on at least the home network side. When I know it's just joined the network, I can SSH into it and use SSH for a short while (5 minutes or so), but it drops the network permanently and doesn't create the hotspot (I don't have the crontab set up to on a timer to keep checking). So I have to hard power cycle it and it works again. If I wait after it's been running for a while, then try to SSH into it, it's already lost the network.

    Do you think this issue is tied to the issue that you verified and had a quick fix of adding the down/up lines?
    ip link set dev "$wifidev" down
    ip link set dev "$wifidev" up

  • Comment Link roboberry Wednesday, 06 December 2017 21:03 posted by roboberry

    Hi Steve

    The top 3 lines on my PI's are:
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    I have tested it be removing the lines and I get a non working wifi which is listed on my network but not usable. Also in this state there are mutiple wpa supplicants but the issue is the misconfiguration of wpa_supplicant. Once the lines above are added and after a reboot everything functions correctly.

    Try this out and let me know if you have further issues.

  • Comment Link Steve Palm Wednesday, 06 December 2017 19:15 posted by Steve Palm

    Not sure how much or what kind of logs would be helpful, but...

    I have two networks in my wpa_supplicant configuration if that matters.

    Initially the PiZeroW connects to the wifi, but then, presumbably when the cron runs the script, it gets the address changed to the one for the hotspot mode but the hotspot doesn't come up, and in fact it is still connected to the WiFi.

    In fact, right now, there are six instances of wpa_supplicant running, looking to having been started approx five minutes apart, so probably by the cron job.

    In looking at it some more, using Google a lot, it seems it may have been because the following lines were not at the top of my wpa_supplicant.conf file:


    Prior that I would get an error when running wpa_cli, which the script uses to determine the status of the connection.

    I saw several mentions online (and in the manpage) that said:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel

    but that didn't work for me...

    At least this is my guess, doing more testing/watching/waiting....

  • Comment Link roboberry Wednesday, 06 December 2017 09:24 posted by roboberry

    Hi Mark, from the feedback it is comparing the ssid being broadcast against the one in your wpa_supplicant file but for some reason it is not seeing a match and going to a hotspot. If it had mad a match it would say connecting to network and then checking network is up. So something about your SSID is being altered. I would expect if you tried to connect to a different router it would be fine. To test this use the feature for using a static ssid and not the wpa_supplicant file.
    find this section
    #Note:If you only want to check for certain SSIDs
    #Remove the # in in front of ssids=('mySSID1'.... below and put a # infront of all four lines above
    # separated by a space, eg ('mySSID1' 'mySSID2')
    #ssids=('mySSID1' 'mySSID2' 'mySSID3')

    change the last line to, based on your example.
    ssids=('word word word word')
    also the four lines above this section need a # in front of them to stop it checking the wpa_supplicant file.

    This will just take what you have written and use that to check the broadcast ssid.

    You password can be anything. All the script does is says is a vaild SSID or Mac address being broadcast if so shutdown the hotspot and activates network wifi. The Pi manages how and what it connects to the script is not involved in that.

    The script doesn't default to a hotspot, the switch is based on finding a matching SSID being broadcast.

    Let me know how you get on. I think the next step is to give you a script that has a lot more feedback, so you can see what it is doing and why it is not getting a match with your ssid.

  • Comment Link Mark Wednesday, 06 December 2017 02:27 posted by Mark

    Hi roboberry,

    My ssid is of the type "Word Word Word Word" without the quotations. I did the echo command line in the script. When it was disabled and on my network, I ran the script manually and it showed (with my real ssid and not the example):

    Checking SSID Word Word Word Word
    Cleaning wifi files and Activating Hotspot

    It then disconnected from my network and created the hotspot. SSH'ing into the hotspot, I did sudo /usr/bin/autohotspot and got (with my real ssid and not the example):

    Checking SSID Word Word Word Word
    Hostpot already active

    ^yes, it showed "Hostpot". I don't know if that matters; most likely not.

    My password for the network includes spaces and special characters, but no apostrophes, commas, or quotation marks.

    Could the issue be with defaulting to creating the hotspot instead of checking for the network, and connecting to it, at boot of the Pi?

  • Comment Link roboberry Tuesday, 05 December 2017 09:37 posted by roboberry

    Hi Mark

    Does your SSID contain anything other than letters, numbers or a space? If it contains an apostrophe, comma or quote marks then it won't be recognised. The script gets the SSID's from your wpa_supplicant file but to do that commas, quotation marks and apostrophes are striped out. I'm guessing that as your wifi works fine normally your SSID is getting altered.

    If you add a line to the script and run it manually again you can see what it is checking for.
    Find this section:

    for ssid in "${ssidsmac[@]}"
    if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
    #Valid SSid found, passing to script
    return 0
    #No Network found, NoSSid issued"

    and add the echo line after do

    for ssid in "${ssidsmac[@]}"
    echo "Checking SSID " "$ssid"
    if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
    #Valid SSid found, passing to script
    return 0

    If your SSID is the issue can you give me an example of how it is written, I can see what can be done but you may just have to change the SSID in your router.

  • Comment Link Mark Tuesday, 05 December 2017 02:19 posted by Mark

    Hi roboberry,

    I did the sudo/usr/bin/autohotspot and it does as "Hotspot already active". When I disable the autohotspot, it connects just fine the my network. So that seems odd that with the script enabled, it's not detecting my ssid. I have my wpa_aupplicant file updated correctly with my network info. My ssid is not hidden. It seems like I'm getting close to having this working; I just don't know where this last hangup is.

  • Comment Link roboberry Monday, 04 December 2017 09:55 posted by roboberry

    Hi Mark, no problem it is easily done. I have done that type of thing enough times during it's development.
    If the script doesn't receive an ip address from your router within 10 seconds it will presume a network issue and fall back to the hotspot.
    if you run the script manually in hotspot mode you can see what it is doing.
    sudo /usr/bin/autohotspot
    If it says Hotspot already active then the Pi doesn't detect your SSID in range, if it connects to your router and then says checking wifi connection ok, the goes back to hotspot then it finds the router but fails to receive an ip within 10 seconds which is a way of confirming the connection has been made. My one gets it within 4 - 6 seconds,
    In the autohotspot script in the section ChkWifiUp() change sleep 10 to sleep 20 and see if that helps.

    You can also do sudo systemctl status autohotspot to see the same messages it issued at bood up.

    hope this helps

  • Comment Link Mark Sunday, 03 December 2017 23:33 posted by Mark

    Hi roboberry,

    I feel stupid on this first part as I forgot to remove the "#" from the DAEMON_CONF="/etc/hostapd/hostapd.conf" line. Sorry about that.

    After fixing that issue, the Pi will make the hotspot and I can connect to it from my iPad. It will not, however, join my home network that I set it up on. I checked, using my iPad while in the hotspot, the wpa_supplicant.conf file and my ssid and password are still set correctly.

    I did the "grep -i dhcpcd /var/log/syslog" like you asked, and it has a few lines saying "waiting for carrier", "carrier acquired", "adding address", "carrier lost", "deleting address" before it says "Starting dnsmasq" and creating the hotspot.

    Using my iPad, I was able to do the troubleshooting things you requested. I commented out the "interface wlan0" from /etc/dhcpcd.conf and did "sudo systemctl disable autohotspot". This allowed the Pi to join my home network upon a shutdown and restart. Upon un-commenting the "interface wlan0" and enabling the autohotspot again, the Pi won't join the home network and instead makes the hotspot.

    So it now seems I have an issue with it connecting to a known wifi network at boot, where it drops the connection and proceeds to making a working hotspot instead.

    This progress includes the 2 lines you had me put in the creatAdHocNetwork function:
    ip link set dev "$wifidev" down
    ip link set dev "$wifidev" up

  • Comment Link Roboberry Saturday, 02 December 2017 10:22 posted by Roboberry

    Hi Mark, the update would only take effect when it goes to a hotspot, the network side of things is unaffected so it seems you have a different issue.
    The version of stretch shouldn't make a difference, i have various pi's on different states of Jessie and stretch and it works ok on them all. The image i tested it on was the latest one, with updates to Thursday.
    If you have no network then at boot there is something else going on.
    Check /etc/dhcpcd.conf that the line is interface wlan0 and not interfaces wlan0, check your wpa_supplicant.conf file.
    Are you able to make any changes if you have no network and not on a screen?

    Even with the authotspot.service disabled so the script is not used you will still have network and ssh at boot. If you can check sudo systemctl status dhcpcd for errors.
    You could check the logs in /var/logs for issues. Try messages and syslog. Try grep -i dhcpcd /var/log/syslog

    If you do the removal section and disable autohospot and undo dhcpcd.conf then see just try to get your network back. Then re-enable them and see what happens

    Let me know what results you get.

  • Comment Link Mark Friday, 01 December 2017 19:13 posted by Mark

    Hi roboberry,

    It's good that you can duplicate my issue, but I tried the fix you supplied and now the Pi doesn't even show up on my network and it doesn't create the hotspot network.

    Could this be due to the Foundation releasing a new version of Stretch and possibly updates to the 09/07/17 version that I'm using?

  • Comment Link roboberry Thursday, 30 November 2017 22:35 posted by roboberry

    Hi Mark, I can only presume something has changed in an update as I updated a noobs image and it now has the same issue. It seems if you cycle the interface on and off it sorts it out. Not ideal a I need to find the route cause.

    Adding a couple of lines to the autohotspot script will work as a quick fix.
    open /usr/bin/autohotspot
    then in this section

    ip link set dev "$wifidev" down
    ip a add brd + dev "$wifidev"
    ip link set dev "$wifidev" up
    systemctl start dnsmasq
    systemctl start hostapd

    add two lines at the start so it look like this

    ip link set dev "$wifidev" down
    ip link set dev "$wifidev" up

    ip link set dev "$wifidev" down
    ip a add brd + dev "$wifidev"
    ip link set dev "$wifidev" up
    systemctl start dnsmasq
    systemctl start hostapd

    you should be up and running after a reboot.

  • Comment Link roboberry Wednesday, 29 November 2017 22:07 posted by roboberry

    Hi Mark, I have installed a new image of Raspbian using Windows 10 and done the setup through putty. It duplicates your issue, but what im seeing is dnsmasq is the issue constantly connecting and disconnecting. If i manually run autohotspot with good SSID to go to network and then bad SSID back to hotspot, the hotspot becomes stable and I can ssh into it.

    You can duplicate this by setting the cron timer and switching on and off your router. Not ideal but will show if you are getting the same. I will need to see why this is not happening on my existing noobs images and any difference with windows setup to linux setup. I will continue tomorrow as it is late here and let you know when i find the issue.

  • Comment Link roboberry Wednesday, 29 November 2017 08:58 posted by roboberry

    Hi Mark, I think that systemctl is working ok and hostapd is probably being disabled ok. I guess there is an error somewhere and without seeing what messages are being shown in systemctl status and the autohotspot script it is difficult to determine the area to look at. I will download Raspbian Desktop as all my images are Noobs, not that should make a difference. Then I will follow the headerless setup from Windows and see what issues I get. It's a bit odd that you can't SSH while the network is available. Usually even if Hostapd and dnsmasq are active while wpa_supplicant has not been disabled then network takes priority and ssh should still be available. I will let you know what results I get.

  • Comment Link Mark Wednesday, 29 November 2017 03:04 posted by Mark

    Hi roboberry,

    When I disable hostapd and dnsmasq, I get these responses respectively:

    hostapd.service is not a native service, redirecting to systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install disable hostapd

    Synchronizing state of dnsmasq.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install disable dnsmasq

    When I enable autohotspot, I get this response:

    Created symlink /etc/systemd/system/ → /etc/systemd/system/autohotspot.service.

    I run this Pi headless, so the only way I connect to it is through SSH or a remote desktop. I redid a clean install from the Stretch image and followed your instructions again. I did not install xdrp or vnc4server on this try to see if they might be the culprit. Since it's headless, I don't know what it's trying to do when I allow it on the network or turn off my network. My network shows it to be on it, but I can't SSH into it. Pings are successful though. When I have my network off, I can't find the hotspot with WiFi devices.

    At each step that requires copy/paste of your programs, I clicked the link to the program page and do a Ctrl+A and pasted directly into the file over SSH using a right click on my mouse (I use PuTTY for SSH).

    Being that you said the systemctl should come back with "Synchronizing state for hostapd.service..." and I have the message "redirecting to systemd-sysv-install..." due to hostapd not being a native service, do you think my issue is with hostapd?

  • Comment Link roboberry Tuesday, 28 November 2017 20:02 posted by roboberry

    Hi Mark, Raspbian comes with a raspberry pi edition of RealVnc installed and as far as I know other VNC services will conflict with it and cause issues. I would try uninstalling the ones you have installed and going to the Preferences menu and Raspberry pi Configuration and then enabling VNC in the interfaces section. Also check that ssh is enabled. Then try RealVNC on windows. I use RealVNC on Android and it works fine.
    The systemctl should come back with a line starting with "Synchronizing state for hostapd.service with sysvinit using update-rc.d ....etc" not that I have noticed systemd-sysv-install before on my systems. It is a process systemd uses to activate non systemd services.

    When you enabled autohotspot it should have come back with a line starting with "created symlink from /etc/systemd/system/....etc"

    What happens when you deactivate your router or make a false ssid and manually run sudo /usr/bin/autohotspot does it respond with any messages?
    and does it create the hotspot?

    If you run sudo systemctl status autohotspot does it say active(exited) in green

    I would say that the autohotspot script or service has an error causing it not to run. At initial boot dhcpcd will activate the network then autohotspot will assess the situation and switch if required.
    So initially network is available as it would be without the script.

    Other causes could be if any of the
    downloaded config file or scripts were opened in windows and re-saved it can alter some of the characters such as swapping between ' and ` which causes errors and the line ends are different.

    you can also try sudo systemctl stop autohotspot and then sudo systemctl start autohotspot and then sudo systemctl status autohotspot and see if that works ok.

    Let me know what feed back you get and I will look into it further.

  • Comment Link Mark Tuesday, 28 November 2017 02:57 posted by Mark


    I'm using Stretch (2017-09-07) with Desktop on a Pi Zero W. I started off with a clean image write and put a blank file called "ssh" and another file called "wpa_supplicant.conf" with my WiFi information in the boot directory of the micro SD card from Windows 7 before putting the card into the Pi. Once in the Pi, I installed xrdp and vnc4server to remote desktop to the Pi. I then followed your tutorial to the letter.

    My Interface is wlan0. When I proceeded to "sudo systemctl disable hostapd" and "dnsmasq", Raspbian changes "systemctl" to "systemd-sysv-install" upon execution.

    I proceeded through the remainder of the steps, even following the troubleshooting section with regards to redoing the two listed commands (sytemctl does not change to systemd.. for this instance), and I opened "/etc/wpa_supplicant/wpa_supplicant" and found it empty, so I added my WiFi information to that.

    Upon a shutdown and power on, the Pi shows up on my home network but cannot be SSH'd into. However, I can successfully ping it. When I turn my network off and hard cycle the Pi, it does not create its own network.

    Would you please help me?

  • Comment Link Roboberry Monday, 13 November 2017 13:51 posted by Roboberry

    Hi Agustin, This setup can be used as a permanent hotspot, not the normal way to do it but does work.
    You will need to complete the guide and followed all instructions as the autohotspot script contains the setup for the hotspot.
    Then to make it always go to hotspot put a fake ssid in this line
    #ssids=('mySSID1' 'mySSID2' 'mySSID3')
    so it shows
    and remove the #, you need to also put a # infront of the 4 commands above this line.
    #wpassid=$(awk ........
    #IFS=$IFSdef #reset back to defaults

    You will still need to disable the hostapd and dnsmasq services.

    This way it will not look at the wifi networks set up on your rpi so will always go to a hotspot at boot up.

  • Comment Link Agustin Lobo Monday, 13 November 2017 10:48 posted by Agustin Lobo

    Thanks for your directions.
    I've followed until step "AutoHotspot Script" because I do not need the switch, just want the hotspot always created. My purpose is using an Android device to always control the RPi through VNC, mainly outdoors (often with no wifi network) but also indoors.
    While I can see the RPi hotspot and connect to it, I cannot connect to the RPi using VNC or SSH, from either Android or Linux.
    I'm totally clueless...
    Any suggestion on what I could be doing wrong?


  • Comment Link roboberry Monday, 13 November 2017 09:08 posted by roboberry

    Hi Andrea, your welcome.
    The article you refer to would not be compatible with Raspbian Stretch as it is using the /etc/network/interfaces file. The ifup command in ifup uap0 needs the interfaces file. You would need 'ip link set dev uap0 up'
    and rc.local is being phased out and replaced with systemd. Saying that it will all still work on Raspbian Jessie, but not with the autohotspot setup.

    In General having a AP and network on the same device would not work as this script swaps between network wifi and AP so both will not be active at the same time which is what you would need it to do.

    For wifi and AP you would need to add a usb wifi adapter to wlan1. That would need further config to get it working smoothly as well but does work.

  • Comment Link Andrea Sunday, 12 November 2017 22:31 posted by Andrea

    It works perfectly on Raspberry pi zero w. Thanks!
    you can quickly say if this other configuration its compatible?

  • Comment Link Roboberry Saturday, 11 November 2017 17:01 posted by Roboberry

    Hi Ben, if the autohotspot script tries to connect to the network but no ip address is issued by the router within 10 second it presumes the connection has failed and go back to a hotspot.

    Enter the command sudo /usr/bin/autohotspot
    It will give you feed back. If it says "Cleaning wifi and activating hotspot" but after it says "Checking Wifi connecton OK" it says "Wifi failed falling back to hotspot" then your router may be taking longer than 10 seconds, my experience it connects within 6.

    Change the line sleep to more than 10:

    echo "Checking WiFi connection ok"
    sleep 10 #give time for connection to be completed to router

    As your wifi password is ok as it works ok then i presume it's a slow connection.

    Let me know if you still have an issue.

  • Comment Link Ben Drewery Saturday, 11 November 2017 14:21 posted by Ben Drewery

    I cant seem to get it to connect to my home wifi, it always creates the hotspot. If I disable the service it will then connect to my wifi. I am running raspbian stretch and tested on a pi3 and pi0w.

    any suggestions would be much appreciated.


  • Comment Link roboberry Wednesday, 08 November 2017 11:00 posted by roboberry

    Hi Lakshmi, You're setup is being a bit troublesome. It is a bit frustrating when everything you try fails, I have been there many times :)

    The flush is done just before the hotspot is activated so that part should be ok.
    wpa_cli terminate >/dev/null 2>&1
    ip addr flush "$wifidev"
    ip link set dev "$wifidev" down
    rm -r /var/run/wpa_supplicant >/dev/null 2>&1

    The line that would clear the issue is sudo rm -r /var/run/wpa_supplicant

    when wpa_supplicant is running it puts a file called wlan0 in that location. This is what is the feedback refers to.

    The log you show has errors for IPv6, I believe that is expected as the RPi networking is setup for IPv4. Though realvnc is showing an error. While getting your access point running I would disable realvnc in raspi-config then enable it again once your up and running.

    If the RTNETLINK error is happening like this i'm wondering if you have wpa_supplicant running more than once.
    Can you confirm which Hotspot article you are following, I know your interested in have an internet hotspot and you are using ip but this article your commenting on is the non internet hotspot.
    Just want to confirm you haven't taken config from both articles which would cause issues.

    Can you confirm that you have updated your setup to the latest guide that was updated about two weeks ago.

    Your /etc/network/interfaces file should contain no reference to wlan0 and should effectively be empty of device config.
    Then /etc/dhcpcd.conf should have 1 line added, interface wlan0
    /etc/dnsmasq.conf should contain this reference dhcp-range=,,,12h

    Also you previously wanted to set this on a PI3 with a usb wifi dongle? are you setting this up on the internal wifi, wlan0?

    when you manually run /usr/bin/autohotspotN what feedback is it giving?

    If you are continuing to have issues you are welcome to email me your config files , hostapd.conf, dnsmasq.conf, interfaces, dhcpcd.conf, wpa_supplicant (remove passwords), autohotspot.service, autohotspotN
    and any other information and I can have a look on my Pi3. email admin@ this site.

  • Comment Link Lakshmi Narasimman Tuesday, 07 November 2017 13:22 posted by Lakshmi Narasimman


    even after reboot hotspot not working. I am getting this error even after reboot and execute manually the script as well.

    I found the following line causing this issue(from the hotspot script)

    ip a add brd + dev "$wifidev"

    After searching the google i added the below line before the above mentioned line

    sudo ip addr flush dev "$wifidev"
    ip a add brd + dev "$wifidev"

    Even after the above lines added hotspot is not coming up, but script doesn't show any failure messages.

    Then i checked in /var/log/messages , i found the below errors.

    Nov 7 18:29:27 raspberrypi kernel: [ 11.371527] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
    Nov 7 18:29:27 raspberrypi kernel: [ 11.371541] brcmfmac: power management disabled
    Nov 7 18:29:28 raspberrypi kernel: [ 11.454911] nf_conntrack version 0.5.0 (15360 buckets, 61440 max)
    Nov 7 18:29:32 raspberrypi kernel: [ 16.124774] fuse init (API version 7.26)
    Nov 7 18:29:34 raspberrypi vncserverui[823]: Failed to load module "gail"
    Nov 7 18:29:34 raspberrypi vncserverui[823]: Failed to load module "atk-bridge"
    Nov 7 18:31:20 raspberrypi kernel: [ 123.655820] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
    Nov 7 18:31:20 raspberrypi kernel: [ 123.655854] brcmfmac: power management disabled
    Nov 7 18:31:34 raspberrypi kernel: [ 138.283092] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
    Nov 7 18:31:34 raspberrypi kernel: [ 138.283106] brcmfmac: power management disabled
    Nov 7 18:31:38 raspberrypi kernel: [ 141.987507] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
    Nov 7 18:31:38 raspberrypi kernel: [ 141.987540] brcmfmac: power management disabled

    Could you suggest how shall i proceed on this?

  • Comment Link roboberry Monday, 06 November 2017 15:05 posted by roboberry

    Hi Lakshmi
    "RTNETLINK answers: File exists" just means that one of the Network Wifi temporary files has not cleared when the WiFi network was stopped. As a result the Hotspot can't be started. This only happens when the network has unsuccessfully be brought down due to a crash or network error.
    The autohotspot script deals with this and will remove it as part of the process so if you reboot it should clear. It may also clear if you manually enter sudo /usr/bin/autohotspot.
    If the hotspot/network switch is working fine and you see this then it's just the feedback from one of the commands that can be ignored.

    Just to let you know, I have been getting a new issue over the weekend meaning my Android device won't connect to the hotspot. This is due to a recent security fix for Hostapd and wpa_supplicant and is not the setup. It works fine with Windows. So if you experience an issue connecting to the hotspot this may be the cause. I'm hoping this will be resolved next time hostapd is updated.

  • Comment Link Lakshmi Narasimman Sunday, 05 November 2017 16:18 posted by Lakshmi Narasimman


    I followed your steps to remove/purge dnsmasq and then freshly installed.
    I followed your troubleshooting section to test the autohotspot script
    means executed the script on command line and I got the following output

    RTNETLINK answers: File exists

    Shall I know what would be reason ?

  • Comment Link roboberry Sunday, 05 November 2017 11:38 posted by roboberry

    HI Lakshmi, The latest version available for Raspbian is 2.76.5 I personally have had no issues with it, without doing the dns root purge on several RPi's that I have set up but has been reported as a bug on some setups.

    try uninstalling dnsmasq with sudo apt-get purge dnsmasq
    and then use sudo apt-get autoremove , just to do a general clean up.
    Then hopefully you can try to reinstall dnsmasq (2.76) with the apt-get command.

  • Comment Link Lakshmi Narasimman Saturday, 04 November 2017 15:44 posted by Lakshmi Narasimman

    Hi Graeme,

    I have followed your steps, after all the steps i found it was not working.
    Then i found the reason, because of dnsmasq 2.76.
    I updated like the following
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get purge dns-root-data

    Before installing dnsmasq i checked the following command
    dpkg -s dnsmasq
    it informed dnsmasq doesn't exists
    then i given the following command
    sudo apt-get install dnsmasq

    After the above i continued further(not checked the version), then finally found hotspot is not working due the dnsmasq
    then again i given the following command
    dpkg -s dnsmasq

    But This time i found the below output :

    Package: dnsmasq
    Status: deinstall ok config-files
    Priority: optional
    Section: net
    Installed-Size: 72
    Maintainer: Simon Kelley
    Architecture: all
    Version: 2.76-5+rpt1+deb9u1
    Config-Version: 2.76-5+rpt1+deb9u1
    Depends: netbase, dnsmasq-base (>= 2.76-5+rpt1+deb9u1), init-system-helpers (>= 1.18~)
    Suggests: resolvconf
    Conflicts: resolvconf (e3b873444952bc1192bb53f0c
    /etc/insserv.conf.d/dnsmasq 530a424ac064ea9d86f235d12ecc227a
    Description: Small caching DNS proxy and DHCP/TFTP server
    Dnsmasq is a lightweight, easy to configure, DNS forwarder and DHCP
    server. It is designed to provide DNS and optionally, DHCP, to a
    small network. It can serve the names of local machines which are
    not in the global DNS. The DHCP server integrates with the DNS
    server and allows machines with DHCP-allocated addresses
    to appear in the DNS with names configured either in each host or
    in a central configuration file. Dnsmasq supports static and dynamic
    DHCP leases and BOOTP/TFTP for network booting of diskless machines.

    I don`t how to get out of the above issues. I have attempted the downloading the version 2.78 directly and install as well. even tough i am getting the above output

    Could you suggest how to proceed further on this ?

  • Comment Link Roboberry Sunday, 29 October 2017 18:35 posted by Roboberry

    Hi jay, i have tried to replicate your issue with Jessie and Stretch and not found a problem. Which version of Raspbian are you using?
    I renamed the resolve.conf file to hide it and then got dns errors as expected.

    The /etc/resolv.conf file should contain your dns server details. When the network wifi is active the system automatically populates the resolv.conf file. My one shows the ip to my local router. When in hotspot mode the system changes it to the local ip , then changed it back once the network is back. Can you check the content of the resolv.conf file as this may be your issue.

    I don't know if this is anything to do with purging the dns_root_data for dnsmasq as i have not had any issues with doing that.

    If you are still having issues, let me know more details and i will look into it further.


  • Comment Link jay iyer Sunday, 29 October 2017 00:29 posted by jay iyer

    Thanks for this code.! It works for me. Except that I get DNS errors when I try to connect to a domain name like "" but it works when I go to absolute IP addresses like ""
    So what should I do.?
    I have the configurations exactly as you have it here.

    I wanted to clarify my earlier question about not being able to connect to domain names: I am operating in the mode where I am not enabling the hotspot but connected to my known wifi network normally.

  • Comment Link roboberry Thursday, 26 October 2017 23:25 posted by roboberry

    Hi Lakshmi, There is not much good info on getting a hotspot on stretch on the net at the moment so it is difficult to get a good guide.
    To see your wifi usb enter iw dev , for me it showed phy#0 with wlan0 (internal wifi) and phy#1 with wlan1 (usb wifi). But on a RPi2 with just the USB Wifi it just showed Phy#0 for wlan0.
    When you enter iw list you should see phy#1 and wlan1 for your usb wifi.

    Going back to your original question, My script has only been tested with wlan0 but will probably work fine on wlan1 once the references are changed to wlan1.

    A Hotspot on wlan0 could be routed to your phone on wlan1 ok to get internet from a tethered phone, but im not sure it can be setup to receive internet from multiple sources, depending on what one is connected. You would need to research iptables and if you can FORWARD a hotspot on wlan0 to multiple devices ie eth0, wlan1, Bluetooth etc. depending on which one has the current internet access. There is probably a way to do it but not something I have looked into.

    But if that issue can be solved then the NAS and media servers should be able to be setup from any guides you can find.

    I'm currently testing my two scripts on Jessie and Stretch and hope to have them available at the weekend, as long I have no new issues.

  • Comment Link LakshmiNarasimman Wednesday, 25 October 2017 01:55 posted by LakshmiNarasimman

    Hi Roboberry

    Thanks for help and suggestion, Except hotspot i am able to find articles from internet. Already some of your suggestions were already seen by me. Yes, of course i already attempted some hotspot scripts ,it not worked much. I will wait for your article publish .
    for getting supported interfaces mode , I checked that command that given the supported modes, but i am not able to identify that modes are for built-in-wifi or usb-wifi adapter.
    I am able to see only for one device(there i saw phy0 like that).

  • Comment Link Roboberry Sunday, 22 October 2017 19:44 posted by Roboberry

    Hi Lakshami
    My other script (link at top of article) will do the hotspot and Ethernet features you ask but I don't think my script is suitable for you as it will only generate a hotspot when your not near your home router. You will need a static hotspot for what you request which this is not what my script is for Unless you don't configure your RPI to connect to your home router.

    Second issue is this script currently only works on Jessie. I now have a working version for Stretch but that is not published yet. Hopefully it will be sometime this week (before end of Oct 2017). In Stretch network setup changed so you will probably find hotspot guides written for Jessie don't work on stretch.

    To see if your usb wifi adapter can do AP mode, in terminal enter iw list
    within the "Supported Interface Modes" section of the output look for AP in the list, which means it can be used for an access point.

    To connect to the phones hotspot with a RPI just set the RPis wifi to the phones SSID as you would a normal router using the wifi icon next to the clock.
    You won't be ablr to connect to the phones hotspot and the RPI be a hotspot at the same time.

    To use a connected hard drive as a network drive you will probably need to look into setting the RPI up as a NAS (Network Attached Storage) which will allow all connected devices to access and save files.
    To act as a media server you will need to look into UPnP servers such as Media Tomb to serve video and music but there are other options but this is separate to the requirements of a hotspot.

    When I publish my script that works on Stretch it will show you the steps to setup an internet routed hotspot but I can't help with a media server or a NAS drive as I have not set these up on a RPi before but there should be plenty of guide on line.
    Sorry I can't be more helpful than that.

  • Comment Link Lakshmi Narasimman Saturday, 21 October 2017 13:18 posted by Lakshmi Narasimman

    1.Raspbian Pi3 have Ethernet & Built-in Wifi and Raspbian Strech has been installed
    2. I have additional USB-Wifi Adapter (TP-LINK725N)

    I have the following requirements
    Pi3 will be connected to monitor,keyboard,mouse ,usb-wifi adapter ,USB hard disk
    Pi can use the wired internet (currently I don`t have)
    Pi will use the phone internet via built-in wifi
    using usb-wifi adapter it should act as hotspot.(advise how to check usb-wifi adapter supports ap mode)
    This hotspot should serve internet to other devices(it should server internet from any source,ethernet,built-in wifi ,bluetooth etc)
    Additional to this connected hard disk should be available as network share
    hard disk conntent should be act as media server.
    using other devices user should be able to upload the file to pi3 connected hard disk.
    using other devices user should be able to listen video,music etc..

    Please provide steps to how to achieve this.


Additional information