Do you allow this site to use Cookies?

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 4. Just for direct Wifi access to the PI when you are out and about.

Works with Raspbian Buster, Stretch and Jessie.

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.


Dnsmasq bug: in versions below 2.77 on Stretch and Jessie, there is a 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 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 and by default hostapd is masked so needs to be unmasked. This is done with the following commands:

sudo systemctl unmask hostapd

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


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-41-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 "$ssidreply" | 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 your 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

Add comment

# ldezem 2020-01-10 12:18
Thank you for this tutorial. I got it working on my first try without much prior experience with Raspberry. Thank you!
Reply | Reply with quote | Quote | Report to administrator
# roboberry 2019-11-30 19:14
Hi Tommy, You're welcome.
I have only used the script on Raspbian so I can't say how it would run on other variants of Debian.
I have just had a look at some network info online about Kali and it seems it is setup like Raspbian was pre Jessie using the /network/interfaces route. The autohotspot script uses dhcpcd.config rather than /network/interfaces.

You say you installed wpa_supplicant, if Kali doesn't use wpa_supplicant for wifi by default, then whatever is the default wifi setup would need to be disabled to go to a hotspot as the network wifi overrides the hotspot wifi. which is probably where the conflict is.

The concept to do the switch is that dhcpcd is disabled for wifi and wpa_supplicant is shutdown. Then dhcp is given to dnsmasq and hostapd is brought up. To go back the whole lot is reversed.

I plan to do some updates and add new stuff in the new year. It may be worth having a look at Kali and other variants and see what can be converted.
Reply | Reply with quote | Quote | Report to administrator
# Tommy 2019-11-30 14:55
Awesome project! Thanks for sharing it.
I am trying to make this work on a Pi 4 running Kali. It seems like there are conflicts when I install/run wpa_supplicant. Have you tried to do this on Kali? Thanks again!
Reply | Reply with quote | Quote | Report to administrator
# roboberry 2019-11-28 20:47
Hi Mats

I would guess that a line has not coped completely or an entry has split across two lines causing the syntax error.

Easiest option would be to recopy it from the download link. Otherwise compare the script to the one online and see if you can spot what is different from the lines around the line you highlight.

If you recopy the download link over the top of the existing autohotspot file you will probably need to do chmod +x /usr/bin/autohotspot again.

let me know if you still have issues
Reply | Reply with quote | Quote | Report to administrator
# Mats 2019-11-28 19:57
My hotspot do not start.

Running the autohotspot script i get the following response:
/usr/bin/autohotspot: 21: /usr/bin/autohotspot: Syntax error: "(" unexpected

That corresponds to the line:

What is wrong? I have only copied the script
Reply | Reply with quote | Quote | Report to administrator
# Laurent 2019-11-26 02:40
First thank you for this tutorial.
Unfortunately I could not make it work.

I created a GIST for the bash script I wrote based on your article.
File name is ``

My OS is Dietpi Stretch (Debian 9) on a rpi 3B+

What happens is, the hotspot shows up on the wifi (in MacOS) and I can connect to it, but I have no IP address given for the Mac, and when checking on the rpi, it does not have any ip address either...

`/etc/dhcpcd.conf` only contains `nohook wpa_supplicant`, nothing else.

/usr/bin/autohotspot is running (status was ok)

So I am a bit at a loss of what to do to make it work.
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-11-26 13:37
Hi Laurent

You beat me to the installer, was planning to do that in the new year :)

The setup has only been tested on Raspbian. So I can't guarantee it will work on other variants.

The issues will be around hostapd and dnsmasq.
If there is no ip then dnsmasq is the issue, it probably failed due to an error in the system or config.

run sudo systemctl status dnsmasq and see if there is an error.

Check that the ip in dnsmasq.conf is in the same range at the autohotspot scripts ip. So for the direct script it should be for autohotspot and - in dnsmasq.conf

If DietPi dosn't use wlan0 for wifi network it needs to be changed in hostapd.conf, dnsmasqq.conf and /usr/bin/autohotspot 9th line

Check that hostapd hasn't been remasked or enabled after an update. Though Stretch is probably still using the older version that wasn't masked by default.

Do you get any ip for wifi?

let me know how you get on and I will look into it further.
Reply | Reply with quote | Quote | Report to administrator
# Laurent 2019-11-27 01:45
Hi, thank you for your reply.

The ip address and range were both fine.
And wlan0 was used but did not have any IP.

I think that I had 2 issues:
- Dietpi is acting weird when installing other softwares (through their own system). And because of that, it would fail quickly even after a fresh install. This is unrelated to the hotspot problem, but made me move back to Raspbian.
- with stretch, dnsmasq ~2.76 is the default and can not be upgraded from what I saw without having to compile the other dependencies. It's annoying and don't want to deal with that for each install that I do.

So I installed a fresh Raspbian Buster image, put Raspap software (which failed before), and now everything is working like a charm.

In conclusion, dnsmasq was the main culprit I believe, and most scripts would fail because of that.
So the solution is a Raspbian Buster install, and I am guessing -not tested- that my installer would work now. I have not time for it now, so until I do Raspap it is.

Thank you again for your great tutorial
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-11-27 12:54
Hi Laurent

It may just be a different setup on DietPi. People have tried other variants on Jessie and had issue. I have not had any requests from any other Buster variants yet. Though dnsmasq was an older version on Stretch it did work, at least with my setups, though that doesn't mean a recent Stretch update hasn't caused issues. I will have to update a Stretch copy and see what happens.

I'm glad you have a solution that works for you and thanks for your feed back.
Reply | Reply with quote | Quote | Report to administrator
# Scott Frey 2019-11-26 16:07
Quoting Roboberry:
Hi Laurent

You beat me to the installer, was planning to do that in the new year :)

I would love to help test, as I would really like to have a working script. I'll Try Laurnet's in a few days and se if I can get some useful feedback
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-11-27 13:13
Hi Scott

I would be happy to try and get to the bottom of your issue. Looking at our last comments, if you can give me some further info I will try and debug your issue

Can you let me know:

Could you give me some details of your setup please as you have it setup on wlan1.

Which Pi are you using?

If you have internal wifi and usb wifi, sometimes they swap wlan port. ie internal wlan1, usb wlan0.

Which usb wifi device are you using?
Incase it is not hotspot compatible. Some wifi chips need a different version of hostapd so won't work with Raspberry Pi wifi drivers.

Can you just confirm that the references to wlan0 in hostapd.conf, dnsmasq.conf and usr/bin/autohotspot (line 9) have all been changed to wlan1.

If you run the hotspot manually what message do you get; sudo /usr/bin/autohotspot

While it is in the failed hotspot mode, does hostapd say it is running?
sudo systemctl status hostapd

any errors in dnsmasq?
sudo systemctl status dnsmasq

Any further info of what happens would be useful.

you can e-mail me at admin@ this site to make it a bit easier if you want.
Reply | Reply with quote | Quote | Report to administrator
# Scott Frey 2019-11-27 15:37
Sorry, "working installer script" ;)

I have got the hotspot working, it just sometimes has a weird delay I can't account for.
Reply | Reply with quote | Quote | Report to administrator
# roboberry 2019-11-27 19:56
Hi Scott

It's most likely be due to another process starting slowly.

You can have the hotspot start earlier in the bootup sequence, but it may cause it to go to a hotspot when the network is available. Though if you set a cron timer for 2 minutes it will switch back to wifi soon after.

In the /etc/systemctl/system/autohotspot.service

and reboot. Any issues just change t back.
Reply | Reply with quote | Quote | Report to administrator
# Eero 2019-11-17 09:51
I have set up my Pi 3 as a hotstpot and this part works fine. However, I want the pi work also as an UDP server/client, but UDP traffic is for some reason blocked; no packet going to/from the server. If pi is not as a hotspot then the UDP works fine. What could be the reason for this behavior?
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-11-17 16:05
Hi Eero

UDP will work in hotspot mode but there may be a timing issue. If upd starts before the hotspot is ready it may use a ip address from dhcpcd. But when the autohotspot starts it shuts down dhcpcd for wifi and craetes an ip for wifi on dnsmasq. UPD would not be on the right setup. If UPD starts after the ip it ready it should be fine. If the autohotspot switches back to network at some point, either manually or via timer the ip will change again and the upd connection will probably be lost again.
Reply | Reply with quote | Quote | Report to administrator
# Eero 2019-11-17 18:57
Thanks for your answer. Do you know if there is a way to restart udp services after hotspot starts? Then it would always get the right ip.
Reply | Reply with quote | Quote | Report to administrator
# Renaud 2019-09-25 09:20
I'm about setting up an headless RaspberryPi ZeroW for the lan of my computer-not-so-literate father.

With you script and the ability to automatically start a "known" Hotspot may his router was down is great. I'm covering my ass!

Just checking your script trying to understand what it does (I'm not very code fluent…) :

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
Is "$ssidreplay" correct? Shouldn't it be "$ssidreply" like the other occurrence?

For now I'm trying to make your script work with pi-hole (which somewhat fiddle with dnsmasq and replace it with it's own system).
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-09-25 10:05
Hi Renaud, your right it's a typo it should be $ssidreply. thanks for letting me know, I have read that script sooooo many times :)

Unfortunately I don't know what setup is required with PiHole but if you have to change the dhcp range in dnsmasq make sure the autohotspot script has the same ip for the first 3 numbers.

Let me know if you need further info
Reply | Reply with quote | Quote | Report to administrator
# Renaud 2019-09-25 19:32
As such your script doesn't work with Pi-Hole installed. But I guess I knew it before installing.
The Pi-Hole installation alters the dnsmasq configuration somewhat. Pi-hole now uses a Pi-hole flavored version of dnsmasq called "FTLdns" / "Pi-Hole FTL".

The problem is their DHCP server partially uses the assets (configuration files) of dnsmasq and/or alters dnsmasq configuration so that it doesn't interferes with Pi-Hole.

The Hotspot (hostapd) seems to appear and work but the DHCP server (dnsmasq) doesn't. Any machine which connects to the Hotspot isn't given any IP.
But the RaspPi correctly changes its IP address to so I can nonetheless access to it via ssh (> I manually changed my mac IP/mask/router).
The services installed on the Box doesn't work (shairport-sync / netatalk)

I'm a somewhat newbie in code, but I search for a solution :
- maybe use FTLdns instead of dnsmasq? (beyond my skills)
- alters dnsmasq configuration?
- alters the autohotspot script to turn off momentarily pi-hole then change dnsmasq configuration…
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-09-25 20:11
Hi Renaud
Thanks for the update. I would need to see what pi hole does. The script won't care what software is doing the dns it would just need to be in the same ip range. In theory finding out what ip range pihole is using and change the ip in autohotspot from to piholes ip ???.???.??.5 there may be more to it. If i get time i will see what config needs changing. It would be interesting to see.
Reply | Reply with quote | Quote | Report to administrator
# Renaud 2019-10-07 17:17
I think I'm in the way of a workaround. That's nice : trying to make Pi-Hole and Autohotspot works together is my makeshift "Bash Scripts and Raspbian Network Configuration 101"!

Pi-Hole is using whatever IP address range my router is using. The problem is that my router is rather finicky and poorly designed (hardcoded "public" IP address used for internal use > impossible to use Cloudflare in the Lan; impossible to change the routers DNS resolver due to dubious reasons…).

That are the reason why I need to use Pi-Hole's own DHCP and DNS resolver.

I like the idea to have a distinguish IP range between "main branch" (Pi-Hole&Router) and "SOS branch" (Autohotspot).

It's not perfect, but trial and errors made me :
> edit dhcpcd.conf to have multiple profiles (and a "fallback" one dedicated to Autohotspot — I don't know if that one is actually useful nor effective.

> edit somewhat to :
- turn off/on Pi-Hole DHCP and resolver before the Hotspot is started or stopped
- switch automatically dnsmasq configuration between Pihole's and Autohotspot when The hotspot is started and stopped

> make a few scripts and crontab edits to :
- make sure that the "good" dnsmasq configuration is in place when the Pi starts (in case of power outage when the wifi is on Hotspot mode > the files and folders may be wrong)
- save the pi-hole's dnsmasq configuration somewhere every now and again (to avoid a "old" configuration be written over a "new" one at startup.:

What work :
- hotspot starts and stop nicely when needed
- it was hard but PiHole successfully restart somewhat (dns resolution / ad blocking).

What doesn't work :
- Pi-Hole's DHCP doesn't restart / is unreliable. I need to reboot the Pi to make it work properly.
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-10-09 19:53
Hi Renaud

It's good to hear you have it working and it's possible to configure them both together.

The fall back you mention on dhcpcd for autohotspot won't be used in hotspot mode as the script disables dhcpcd on wlan0. So you probably don't need that.

When your happy with it I would be interested to take a look at your update. It will probably be useful to others.

Reply | Reply with quote | Quote | Report to administrator
# Paul W. Rogers 2019-09-04 21:52
further to last comment
yes you can do sudo crontab -e but
you need to set the path variable in the crontab

this is what I have
*/5 * * * * /usr/bin/autohotspotN >/dev/null 2>&1
Reply | Reply with quote | Quote | Report to administrator
# roboberry 2019-08-22 20:21
Hi Daniel

Thank you, glad you found it useful.
When the Pi goes from Hotspot to Wifi it just reinstates wifi in the same way wifi is started when you first bootup on a pi without the autohotspot. So it "should" act the same way. I take it you have been getting login pages until you used the autohotspot?

I had a quick look online and it is possibly and issue if when a dns server such as goggle on is set the page doesn't load because the wifi providers dns is not used. Google is set for the other autohotspot script for internet but not this Direct article.

So it may just be a setting but I would have to take it somewhere and try.

Have you previously used the same wifi provider which then stopped when the autohotspot direct script was setup? Any other info would be helpful. thanks
Reply | Reply with quote | Quote | Report to administrator
# Daniel 2019-08-22 19:21
Great tutorial, thank you. I do have a follow up question:
Auto connecting to a known wifi doesn't seem to work if the wifi requires an acceptance of terms and conditions after connecting.
Would it be possible to call a POST request when connecting to a wifi like that? The POST would be the link that the browser would call when the HTML form is posted. (This would be different for each wifi)
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-08-21 09:41
Hi Scott

Could you give me some details of your setup please as you have it setup on wlan1.
Can you just confirm that the references to wlan0 in hostapd.conf, dnsmasq.conf and usr/bin/autohotspot have all been changed to wlan1.

If you run the hotspot manually what message do you get; sudo /usr/bin/autohotspot

While it is in the failed hotspot mode does hostapd say it is running?
sudo systemctl status hostapd

The purge dns-root-data should be done first as a precaution for Jessie and stretch but I have never had an issue with it on any of my pi's but it has solved some users issues so it may just be specific to some setup with other software installed.

The masked hosapd service seems to have started when Buster was released but the autohotspot scripts start hostapd themselves so the service is not used, so shouldn't be an issue. It's noted on the static hotspot which it would be an issue for. but thanks for highlighting you experienced it as well.

Let me know how you get on and I will try to solve your issue.
Reply | Reply with quote | Quote | Report to administrator
# Scott Frey 2019-08-22 23:34
apparently it just takes some amount of time (in multiple minutes) before the hotspot is broadcast. This is after I unmasked hosted, so I am not sure if that is required or not, I would have to rebuild from scratch and not unmask and see if it ever appears. Should it take several minutes to begin broadcasting?
Reply | Reply with quote | Quote | Report to administrator
# Roboberry 2019-08-23 00:06
Hi Scott, no it shouldn't take several minutes to go to the hotspot. Try changing the channel in hostapd.conf to something else between 1and 12. There are various conditions to cause it to retry but that shouldn't take more than 10 seconds. If you run it manually you can see what it does as it tries to create the hotspot. Sudo /usr/bin/autohotspot
If you have other software running at startup that is slow, it can cause a delay before the script is run. If it switches during use of the pi then it should be quick.
Reply | Reply with quote | Quote | Report to administrator
# roboberry 2019-08-23 21:28
Hi Scott

You can use system-analyze to see if you have other software that is starting slow.
to see total startup time of Raspbian; sudo systemd-analyze

For each stage of start up; sudo systemd-analyze time
and longest to shortest service: sudo systemd-analyze blame

maybe this will highlight which service is causing a long delay.
The static hotspot shouldn't cause a delay.
The Autohotspot scripts do an additional check after 20 seconds so they will say they take around 21 seconds.
Reply | Reply with quote | Quote | Report to administrator
# Scott Frey 2019-08-20 22:18
I am unable to get this working as I have done in the. Using an install of Stretch, updated to latest. Upon testing, ifconfig shows wlan1 to be using the correct IP address, but no hotspot is being broadcast. I have unmasked Hospapd (that should be added to your instructions and tried to purge dns-root-data (it is unclear where in the steps that should be done) all to no avail.
Reply | Reply with quote | Quote | Report to administrator