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

Updated Oct 2017: now works with Raspbian Stretch and Raspbian 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 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

Aim:

  • 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:  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
  • 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.

 

 Requirements:

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

  • Raspberry Pi Zero W or Raspberry Pi 3
  • 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 and Raspbian Stretch using a 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.

Note:

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 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:

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

  • The interface will be wlan0
  • The driver nl80211 works with the Raspberry Pi 3 & 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 elinux.org/RPi_USB_Wi-Fi_Adapters

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

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

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

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

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
no-resolv
#Interface to use
interface=wlan0
bind-interfaces
dhcp-range=10.0.0.50,10.0.0.150,12h

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, the dhcpcd.conf 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.

Enter

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

Next we need to add a line to the dhcpcd.conf file. Open the file with

sudo nano /etc/dhcpcd.conf

then scroll to the bottom of the file and add the line

interface wlan0

and the save (ctl & 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


[Unit]
Description=Automatically generates an internet Hotspot when a valid ssid is not in range
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/autohotspot
[Install]
WantedBy=multi-user.target

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' )
mac=()

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


#!/bin/bash
#version 0.95-N/HS

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

#Network Wifi & Hotspot with Internet
#A script to switch between a wifi network and an NON Internet routed Hotspot
#For use with a Raspberry Pi zero W or Zero with usb wifi dongle. 
#Also for any Raspberry Pi where an internet routed hotspot is not required.
#Works at startup or with a seperate timer or manually without a reboot
#Other setup required find out more at
#http://www.raspberryconnect.com

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

IFSdef=$IFS
#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=","
ssids=($wpassid)
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' ) 
mac=()

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

createAdHocNetwork()
{
    ip link set dev "$wifidev" down
    ip a add 10.0.0.5/24 brd + dev "$wifidev"
    ip link set dev "$wifidev" up
    systemctl start dnsmasq
    systemctl start hostapd
}

KillHotspot()
{
    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
}

ChkWifiUp()
{
	echo "Checking WiFi connection ok"
        sleep 10 #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
	       createAdHocNetwork
	fi
}

FindSSID()
{
#Check to see what SSID's and MAC addresses are in range
ssidChk=('NoSSid')
i=0; j=0
until [ $i -eq 1 ] #wait for wifi if busy, usb wifi is slower.
do
        ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1 
	if echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
		NoDevice
	elif ! echo "$ssidreply" | grep "resource busy (-16)"  >/dev/null 2>&1 ;then
		i=1
	elif (($j >= 5)); then #if busy 5 times goto hotspot
                 ssidreply=""
		 i=1
	else #see if device not busy in 2 seconds
		j=$((j = 1))
		sleep 2
	fi
done

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

NoDevice()
{
	#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
}

FindSSID

#Create Hotspot or connect to valid wifi networks
if [ "$ssidChk" != "NoSSid" ] 
then
       if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
       then #hotspot running and ssid in range
              KillHotspot
              echo "Hotspot Deactivated, Bringing Wifi Up"
              wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
              ChkWifiUp
       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
              ChkWifiUp
       fi
else #ssid or MAC address not in range
       if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
       then
              echo "Hostspot already active"
       elif { wpa_cli status | grep "$wifidev"; } >/dev/null 2>&1
       then
              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
              createAdHocNetwork
       else #"No SSID, activating Hotspot"
              createAdHocNetwork
       fi
fi

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.

enter

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

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


network={
	ssid="mySSID1"
	psk="myPassword"
	key_mgmt=WPA-PSK
}

change to


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 arrows facing opposite directions Raspbian Pixel AP mode icon This means it is an access point. 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 10.0.0.5 also if you have setup the Rpi as a webserver use the same ip to see the webpage.

For ssh use ssh pi@10.0.0.5

For vnc use 10.0.0.5::5900

 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.

 

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.


 

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

Remove the line "interface wlan0" from the bottom of the /etc/dhcpcd.conf file.

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 and have this icon Network Downthen 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
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 Sunday, 19 November 2017 11:05
Google
roboberry

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

Website: www.raspberryconnect.com

Leave a comment

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

65 comments

  • 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
    ssids=('fakessid')
    and remove the #, you need to also put a # infront of the 4 commands above this line.
    #wpassid=$(awk ........
    #IFS=","
    #ssids=($wpassid)
    #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.

    Report
  • 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?

    Thanks
    Agus

    Report
  • 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.

    Report
  • 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?
    imti.co/post/145442415333/raspberry-pi-3-wifi-station-ap
    Thanks

    Report
  • 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:

    ChkWifiUp()
    {
    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.
    Graeme

    Report
  • 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.

    Thanks

    Report
  • 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
    createAdHocNetwork



    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 192.168.50.5 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=192.168.50.150,192.168.50.200,255.255.255.0,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.

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

    Graeme,

    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 192.168.50.5/24 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 192.168.50.5/24 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?

    Report
  • 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.

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

    Graeme,

    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 ?

    Report
  • 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.

    Report
  • 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 ?

    Report
  • 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 127.0.0.1 , 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.

    Graeme

    Report
  • 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 "http://www.groupon.com" but it works when I go to absolute IP addresses like "103.34.2.108"
    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.

    Report
  • 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.

    Report
  • 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).

    Report
  • 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.

    Report
  • 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.

    Report
  • Comment Link Roboberry Tuesday, 17 October 2017 11:36 posted by Roboberry

    Hi Mastro, unfortunately the script does not work in Raspbian Stretch, please see the article notes in red, Due to the network setup changes I have yet to get a stable AP and network to modify the script for Stretch. For now I would suggest you reverse the changes otherwise you will need Raspbian Jessie for a working version.

    In stretch, wpa_cli -i wlan0 status ,will give you the ip_address. Wlan0 will only work for RPi3 and ZeroW. Otherwise you will need to use the new device name of your wifi dongle. Networking woks fine in Stretch as wpa_supplicant is started in the dhcpcd service, because the ip_address is not returned, as you point out, it tries to go to hotspot that then fails to be configured.

    I'm still hoping to fix this soon, but been a bit busy with other things at the moment but I will get it working :)

    Report
  • Comment Link mastro Monday, 16 October 2017 14:32 posted by mastro

    Sorry if i continue to update.
    I have disabled autohotspot and i've tried to give this command:
    $: sudo wpa_cli status
    Selected interface 'p2p-dev-wlan0'
    wpa_state=DISCONNECTED
    p2p_device_address=ba:27:eb:f1:e6:70
    address=ba:27:eb:f1:e6:70
    uuid=8db221da-beb7-542c-89cd-dcbba6161d43

    there is no 'ip_address' (|grep 'ip_address')

    I hope this can help

    Hi Roboberry,
    I have same Brandon's issue: "It turns out that it has been connecting to my home wifi, however it will disconnect and create the hotspot after the 10 second sleep duration. "
    I've raspbian strech.
    I think that raspberry connet to wifi, because i can ping it for few seconds from a pc in the wifi network.

    UPDATE: i have set timeout in 120 seconds, and i was able to connect trough vnc in wifi network for some seconds!

    Someone can help? Thanks

    Report
  • Comment Link mastro Monday, 16 October 2017 14:20 posted by mastro

    Hi Roboberry,
    I have same Brandon's issue: "It turns out that it has been connecting to my home wifi, however it will disconnect and create the hotspot after the 10 second sleep duration. "
    I've raspbian strech.
    I think that raspberry connet to wifi, because i can ping it for few seconds from a pc in the wifi network.

    UPDATE: i have set timeout in 120 seconds, and i was able to connect trough vnc in wifi network for some seconds!

    Someone can help? Thanks

    Report
  • Comment Link Roboberry Friday, 29 September 2017 21:06 posted by Roboberry

    Hi Blastman

    Sound like you have a general issue with your system setup.
    do a search for "debian systemctl dbus error"
    This should give you clue on what to do such as this site
    https://superuser.com/questions/1173756/cant-start-systemd-failed-to-get-d-bus-connection

    Hope this works for you.

    Report
  • Comment Link Blastman Thursday, 28 September 2017 20:09 posted by Blastman

    Hi,

    I've a problem with systemctl, I've installed because I only had service command.
    When I execute some systemctl instruction, like sudo systemctl disable hostapd, thie error appears Failed to get D-Bus connection: No connection to service manager.
    Do you know what can I do?.

    Thanks so much, I found it a very usefull article if it worked for me.

    Report
  • Comment Link Graeme Saturday, 23 September 2017 21:35 posted by Graeme

    Hi Lakshmi

    I don't believe you can have a single wifi connection be a client and a server at the same time. You would need a second wifi connection to do that.

    This script will give a AP when away from home router and client when at home. In both cases SSH and VNC can be used.

    Report
  • Comment Link Graeme Saturday, 23 September 2017 21:30 posted by Graeme

    Hi alejandro

    Are you using Raspbian Jessie or Stretch

    in terminal enter lsb_release -a
    This script is not currently working on Stretch due to recent changes.

    The latest version of dnsmasq is only available from the dnsmasq main website where you will need to download the file and install it manually. It's not currently available through apt-get.

    The file error is due to the wifi setting not being cleared properly, which the script usually takes care of so im guessing your using Stretch. If using Jessie give me more details of what is happening and I will try and help

    Report
  • Comment Link Lakshmi Narasimman Saturday, 23 September 2017 18:01 posted by Lakshmi Narasimman

    Thanks Graeme. Is it possible on single wifi as client as well AP. Is any proven method present
    in this case can we able to connect ssh /vnc etc

    Report
  • Comment Link alejandro Thursday, 21 September 2017 23:43 posted by alejandro

    HI, not work for me.

    the error is "RTNETLINK answers: File exists".

    my dnsmasq is 2.76 is posible upgrade? how?

    Report
  • Comment Link Graeme Monday, 18 September 2017 22:18 posted by Graeme

    Hi Lakshmi, Not sure I can help here but point 1 can be done with a standard Hotspot, there are many tutorials in the net. This will make it a perminant hotspot and connected devices will have a internet if a Ethernet cable is attached. Alternately there is my other Auto hotspot that will allow hotspots to use the internet. http://www.raspberryconnect.com/network/item/330-raspberry-pi-auto-wifi-hotspot-switch-internet (though not working on Raspbian Stretch yet)

    point 2: not sure what you mean by "it has to server" you can use web servers, media servers http://www.datamation.com/open-source/best-linux-media-servers.html
    or file servers such as SAMBA all require different setups depending what you want to achieve.

    Point 3, If you want the hard drive to be available to for storage of any connected drive then you need to look into NAS setups.
    eg https://www.maketecheasier.com/nas-solutions-linux/

    Yes you can do what you want but I can't help you more than the above. :)

    Report
  • Comment Link Lakshmi Narasimman Saturday, 16 September 2017 15:59 posted by Lakshmi Narasimman

    I need my pi as the following aims

    1.it has to serve the devices as hotspot (if it is connected via ethernet or usb 3g dongle or existing wifi router/mobile hotspot or even no connection with wifi/ethernet)
    2.if connected ethernet/usb 3g dongle/mobile hotspot as internet , it has to server, if not also it should server to other devices
    3. if above meets if any usb hard drive attached it should server other devices as network drive/media server etc

    could anyone suggest on this, i am completly new to linux/raspbian enviornment.
    if anyone suggest i can attempt and learn

    Report
  • Comment Link Graeme Thursday, 14 September 2017 08:40 posted by Graeme

    Hi Matthew, It seems there has been a few issues with networking in stretch. I have been working on it, my wifi works fine and I can use the net on the desktop but the networking.service fails to start because some background files are not being cleared by the system, which means the wpa_cli status line have no response. I have used the iw dev wlan0 link | grep "Connected" which works fine but then the last time I tried it the hotspot won't come up which was working. wlan0 is still used but there will be issues with usb wifi dongles. Looking around the net others have slightly different issues to me, as it seems yourself also. My interface file was blank but I have populated it, but my issue was happening before that.
    I will get a fresh image at the weekend and try again but at this stage I feel what works for me may not work for others. Any feed back welcome, thanks

    Report
  • Comment Link Matthew Thursday, 14 September 2017 06:52 posted by Matthew

    I took a stab at getting this up and running on Stretch but haven't had any luck. It creates the AP just fine but wont connect to my SSID when at boot or when I run the script manually. I looked into the new naming scheme on Stretch but it didn't shed much light on the problem. Eth0 has been renamed enx"mac:address" but my wlan0 is still the same. I think it has something to do with the etc/network/interfaces file since its blank in stretch but wpa_supplicant has this extra line at the top ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    I'm happy to keep messing around trying to get this running and learning more about linux but I was hoping you might have some insight on why it would break the SSID connection and not the AP mode. Maybe you can point me in the right direction?

    Report
  • Comment Link Graeme Monday, 28 August 2017 12:00 posted by Graeme

    Hi Brandon, I haven't got as far as testing it with Stretch yet. Must be some changes to the network setup. I will add that to list of tasks, thanks for the info and I will look into that next time I have chance.

    Update: just looked into it and the network device names have changed in Stretch which is why it wouldn't connect.

    Report
  • Comment Link Brandon P Monday, 28 August 2017 06:37 posted by Brandon P

    Graeme,

    It looks like the problem was from using the new RASPBIAN STRETCH WITH DESKTOP image. The /etc/network/interfaces file only included the following text.

    # 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

    I assumed that if I added the following that it would work.

    auto lo wlan0
    iface lo inet loopback
    iface eth0 inet manual
    allow-hotplug wlan0
    iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    allow-hotplug wlan1
    iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

    After installing the 2017-07-05-raspbian-jessie and following your directions it works perfectly. Thank you again for creating this.

    Report
  • Comment Link Brandon P Sunday, 27 August 2017 16:23 posted by Brandon P

    Graeme,

    It turns out that it has been connecting to my home wifi, however it will disconnect and create the hotspot after the 10 second sleep duration.

    After changing the sleep from 10 to 120, the wifi icon will stay by the clock for 120 seconds and then change to two arrows facing opposite directions.

    If there is anything I can send you to assist with this please let me know.

    Thanks,
    -Brandon

    Report
  • Comment Link Graeme Sunday, 27 August 2017 13:57 posted by Graeme

    Hi Brandon, sorry for slow response I have not been able to spend time on the site recently. You may have wifi interference. When trying to connect to the home wifi, if no ip is issued by the router within 10 seconds it will drop back to the hotspot. Try changing the channel in the hostapd.conf file from 6 to 11 or another channel.
    You can also try increasing the sleep from 10 seconds in the ChkWifiUp section of the autohotspot script.

    For the wired home connection to work a wifi signal must be available as well otherwise it will go to a hotspot.

    You can disable the script with the command in the script removal section. To re-enable use sudo systemctl enable autohotspot instead of disabling wifi.

    let me know if this doesn't help and I will look into it further, responses may be a bit slow at moment but will try to help

    Report
  • Comment Link Brandon P Sunday, 27 August 2017 00:39 posted by Brandon P

    Graeme,

    Looks like I spoke too soon, it is now back to enabling the hotspot after a reboot. Sorry for all of the comments. I don't mind having to turn wifi on and off but was hoping to simplify the process. Again thank you for creating this.

    Report
  • Comment Link Brandon P Saturday, 26 August 2017 22:34 posted by Brandon P

    Graeme,

    I sent you a message earlier today, after the changes below it now works.

    I changed the autohotspot script to only look for one ssid=('mySSID1') and put a # infront of all four lines above.

    Thanks again for this great info.

    Report
  • Comment Link Brandon P Saturday, 26 August 2017 20:42 posted by Brandon P

    Graeme,

    My goal is preferably to have a wired connection at home and a wifi hotspot with no internet in the car so my kids can connect to a Raspberry PI 3 and watch videos via a PLEX server.

    After a reboot, my hotspot turns on even when I'm within distance of my home wifi. For some reason, it won't connect to my home wifi. I haven't made any changes to the wpa_supplicant.conf file and have also confirmed that the ssid and password are correct.

    Once the hotspot is enabled I lose internet access, my workaround for this is when I’m at home I will disable the wifi via dtoverlay=pi3-disable-wifi in the /boot/config.txt and only use the ethernet port. When I want to use it in the car I then enable wifi.

    Any assistance in this would be greatly appreciated.

    Report
  • Comment Link Graeme Saturday, 05 August 2017 12:40 posted by Graeme

    Hi JM

    Thanks for the reminder, I have put the bug note before step 1 now.
    Interesting you can change the signal strength. Good tip for battery saving features if only planning to be close to the pi in hotspot mode. Thanks

    Report
  • Comment Link JM Saturday, 05 August 2017 02:09 posted by JM

    Graeme, this is a followup on my earlier problem report which was resolved when I purged dns-root-data from the ver 2.76 dnsmasq BEFORE preceding with the actual installation instead of after the original install. I emailed you about that but wanted others to know that it then worked perfectly; many thanks for a very useful script. My application is a remote audio recorder sequenced by cron using the ZeroW. I had a problem with RFI from the WiFi Tx getting into the mic preamp and fixed that by reducing power to 1mW by adding: "iw dev wlan0 set txpower fixed 1" to the script ...... no more RFI and battery capacity let's me run for over 20 hours in the field.

    Report
  • Comment Link Graeme Wednesday, 02 August 2017 20:18 posted by Graeme

    Hi David, Thank you. Happy to share something I needed and was sure others could make use of.

    Report
  • Comment Link David Clapp Wednesday, 02 August 2017 02:33 posted by David Clapp

    Very nice! Both the concept and the instructions.

    Report
  • Comment Link Graeme Tuesday, 01 August 2017 20:53 posted by Graeme

    Hi LQ, thanks, your welcome.

    Report
  • Comment Link LQ Tuesday, 01 August 2017 14:20 posted by LQ

    It worked out perfectly for me, just what I was looking for.

    Thank you so much for writing this up!

    Report
  • Comment Link Graeme Monday, 31 July 2017 21:34 posted by Graeme

    Hi Steve, It sounds like it's being put to good use :) Just wanted to check that the setup didn't cause issues I didn't account for.

    What I can do if it is of any use to you is give you a script that will force it to the hotspot without a reboot, regardless of wifi available. You can run this from any folder you choose. Then when you need wifi back run sudo /usr/bin/autohotspot and it will bring back wifi if it is in range.

    If you are using a timer or trigger to run the autohotspot script when it is in forced hotspot mode it will find the wifi network again.

    Let me know if that's of use and I will put it together for you.

    An alternative option is to not put your Uni Wifi in wpa_supplicant.conf so it doesn't use it as standard. If you put the detail in a separate file such as wpa_supplicantUNI.conf. Duplicate the autohotspot script and change the name. In the copy change the references for wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf >/ to the alternate wpa_supplicantUNI.conf

    Then when you manually run the duplicate autohotspot script it will only connect to SSIDs in the wpa_supplicantUNI.conf file.

    When you have finished the downloads run the normal autohotspot script and it will go back to a hotspot as it will use the standard wpa_supplicant.conf file.

    Report
  • Comment Link Steven Monday, 31 July 2017 15:24 posted by Steven

    Hi, Graeme. Thanks for your reply. The issue is related to the way that I am using the Pi. We (my professor and I) are using 3 Raspberry Pis for the computing portion of three different sensors that will be used for non-destructive structural testing. We will use them in the field where no WiFi exists. One of the Pis will be the master, broadcasting an access point. The other Pis will connect to the master. We will be able to SSH to all three with a laptop or a phone in order to run our data collection programs.

    I needed your tutorial, because if the master Pi dies, the other Pis need to automatically create their own network so that we can still SSH to them (no monitor in the field, making it difficult/time consuming to change the configuration manually). The reason I asked this question is because we configure the software in a computer lab at the university. While we are doing that, we need the Pi to ignore the university WiFi in order to simulate the no-WiFi conditions of the field. However, occasionally we need to connect the Pis to the internet through university WiFi in order to download packages, etc. I could probably just add/remove the university network from the list of recognized SSIDs, but I wanted to see if there was an easy way to switch back and forth. This way, I will not list the university network on the list of recognized SSIDs. The Pis will ignore the university WiFi while we are fine tuning the software. I can, however, disable the autohotspot, download a new package from the internet, and then re-enable autohotspot really easily if I need to. Thanks again!

    Report
  • Comment Link Graeme Saturday, 29 July 2017 09:42 posted by Graeme

    Hi Steven, Thanks im glad you find it useful. As you say 'sudo systemctl enable autohotspot' or 'sudo systemctl enable autohotspot.service' will reactivate the script on the next reboot. If you use the cron timer then you will need to also add or remove the # to the line added because manually running sudo /usr/bin/autohotspot will cause it to generate a hotspot if no know ssid is in range.

    Out of interest what issues does this script cause you if you have it running all the time? For my use, other than not being able to scan for new wifi networks when round a friends for example, the wifi mode is the same as when the autohospot script is disabled.

    Report
  • Comment Link Steven Friday, 28 July 2017 21:50 posted by Steven

    Excellent, this is exactly what I am looking for. Thank you for writing it. Question regarding Script Removal: After disabling autohotspot, can I re-enable it with the line 'sudo systemctl enable autohotspot'? I want to be able to switch back and forth between this wifi/hotspot capability and standard wifi setup, because I will need to do this periodically.

    Report
  • Comment Link Graeme Wednesday, 26 July 2017 19:58 posted by Graeme

    Hi Murray, All my Pi's have a hostname setup which I use to connect with in network mode so not knowing the ip is not an issue. Usually not a problem but on rare occasions I can't connect with the host name so have to look up the IP first from my pc or tablet, but in general I connect via ssh with pi@hostname and pi@10.0.0.5 in hotspot mode.
    Hostname can be setup with the raspberry pi configuration menu in preferences or editing the /etc/hostname file. It should just contain the name you want to use.

    Is this what you are after or are you trying to have the same ip/hostname for network and hotspot?

    Report
  • Comment Link Murray Wednesday, 26 July 2017 15:11 posted by Murray

    Hi Graeme, great tutorial, thanks! I have it working across a couple of different networks and as a hotspot. For the sake of consistency when using SSH, I'd like to use a static address. I've managed this when creating the hotspot, but my previous solution doesn't work when connecting to other networks and using "denyinterfaces wlan0" causes it to miss the other networks and create the hotspot anyway. Is there a way to get static IPs using id_str and etc/network/interfaces or is there a different solution?

    Report
  • Comment Link Graeme Sunday, 23 July 2017 12:13 posted by Graeme

    Hi Jim, ok that makes sense. I use the default Pi user account and change the password, sudo is setup by default. I will add a note. Thanks for pointing that out.

    Report
  • Comment Link Jim Buzbee Sunday, 23 July 2017 01:42 posted by Jim Buzbee

    Graeme- I'm using a current version of Raspbian (8, Jessie). But I manually added my account and manually gave that account sudo privileges. Maybe when you go through the guis to create accounts and add sudo privileges, you get sudo access without requiring a password.

    Report
  • Comment Link Graeme Saturday, 22 July 2017 22:57 posted by Graeme

    Hi Jim, glad you have it working now. with sudo I don't get that issue using the cron. I use Ubuntu on my PC and as you say you have to enter a password with sudo but on Raspbian I don't believe I have ever had to enter a password with sudo on any of my Pi's using upgraded images or the latest images. I get the images from the foundations website. Is your version of Raspbian from the foundations website or from another source?

    Report
  • Comment Link Jim Buzbee Saturday, 22 July 2017 16:15 posted by Jim Buzbee

    Graeme- You might also make a note that your described usage of the cron job requires that the user has configured sudo to not require a password prompt. This was not the case for me. In my case, I just added the cron job to the root account instead of my local account. Also, thanks for the work on this process. It's working fine for me now!

    Report
  • Comment Link Graeme Saturday, 22 July 2017 12:58 posted by Graeme

    Hi Jim, your correct there is an issue with that line. A slight oversight on the version that got posted to the site. I have updated the article with the change. Thanks

    Report
  • Comment Link Jim Buzbee Saturday, 22 July 2017 01:21 posted by Jim Buzbee

    I also had issues with an SSID containing blanks until I changed the line:

    if [ $ssidChk != "NoSSid" ]

    to

    if [ "$ssidChk" != "NoSSid" ]

    Consider an SSID of "this is a test". Without quoting the variable, the "if" statement would resolve to:

    if [ this is a test != "NoSSid" ]

    Which would bomb out with an error of "[: too many arguments"

    Report
  • Comment Link Graeme Thursday, 20 July 2017 19:33 posted by Graeme

    Hi Dave, No it won't work with a ' in the ssid as these have to be removed in the string handling of the ssid before the signal is checked.
    If you are manually re-running the autohotspot script after the above error then you will get a RTNETLINK error as the system is in the wrong state to clear it when it needs to. A reboot would get things back under control.

    To flush the system to get it in a usable state instead of reboot requires 4 commands;
    wpa_cli terminate
    ip addr flush wlan0
    ip link set dev wlan0 down
    sudo m -r /var/run/wpa_supplicant

    Then you "should" be able to rerun the script and it will manage the wifi again. But easier to reboot.

    Is there a single quote in your ssid by default? so far I have only come across spaces.

    Let me know if you have further issues and I will see what I can do.

    Report
  • Comment Link Dave Thursday, 20 July 2017 06:42 posted by Dave

    I almost have this working, I'm still trying to fix the v2.76 dnsmasq issue, but in the meantime, it looks like there is a problem parsing ssid's that contain a single quote/apostrophe. (The default apple "Joe Smith's Network" format, for example.) This will result in a " too many arguments" Error. (Line 75 or so?).

    Once I removed the single quote from my wpa_supplicant.conf, I started seeing "RTNETLINK answers: File exists" Errors. I added an extra flush before the down and up in createAdHocNetwork, which might or might not be causing the probs I'm seeing w/ dnsmasq, but at least the script runs for me now. :-)

    I'm doing a full apt-get upgrade (skipped that earlier due to painfully slow connectivity) and will see how things look in the morning.

    Thanks for putting this together, it will definitely solve a lot of problems for me once I get what I am doing wrong figured out!

    Report
  • Comment Link Graeme Tuesday, 18 July 2017 19:46 posted by Graeme

    HI lixi, It should work when the ssid has spaces, it worked fine testing the script.
    Could you add an extra line to the autohotspot script to see what it is searching for.

    Underneath these lines;
    #Check to see what SSID's and MAC addresses are in range
    ssidChk=('NoSSid')
    for ssid in "${ssidsmac[@]}"
    do

    enter echo "Checking SSID: " $ssid

    so it should read

    #Check to see what SSID's and MAC addresses are in range
    ssidChk=('NoSSid')
    for ssid in "${ssidsmac[@]}"
    do
    echo "Checking SSID: " $ssid
    if { iw dev wlan0 scan ap-force | grep "$ssid"; } >/dev/null 2>&1


    then run the script manually with the command
    sudo /usr/bin/autohotspot

    Does the full ssid show or is it split across two lines?

    could you also give me an example of how your ssid is written.

    Thanks

    Report
  • Comment Link lixl Tuesday, 18 July 2017 16:20 posted by lixl

    Great script - thank you very much, but:

    doesn't work with SSIDs which contain a space.
    please be aware of that if the RasPi Starts a Hotspot although the wanted SSID is in reach.

    Report
  • Comment Link Graeme Monday, 17 July 2017 22:17 posted by Graeme

    Hi Jules M, The error you list suggest the service can't run the script /usr/bin/autohotspot but if that was the case you shouldn’t see the wifi RPiHotSpot on windows. Can you try a few things and send me some extra info please.
    in terminal enter sudo /usr/bin/autohotspot this should run the script and give you some feedback, if it works ok try to connect in windows again.

    could you also email me your outputs from:
    sudo systemctl status -l autohotspot

    and in hotspot mode for:
    sudo systemctl status -l hostapd
    sudo systemctl status -l dnsmasq

    Other things to try is check the autohotspot script is executable, enter ls -g /usr/bin/autohotspot
    what it returns should start with -rwxr-xr-x if the x's are missing enter sudo chmod +x /usr/bin/autohotspot and reboot the Pi and see if that works.

    As a double check disable hostapd and dnsmasq again so they don't start at bootup with
    sudo systemctl disable hostapd
    sudo systemctl disable dnsmasq

    When you are in range of your router does it connect ok?

    email: admin@ this site
    and I will look into your issue further.

    Graeme

    update: issue resolved - dnsmasq bug, cleared root dns also interference on channel 6, channel changed. Now working.

    Report
  • Comment Link Jules M Sunday, 16 July 2017 22:45 posted by Jules M

    Tried with a windows XP laptop and Vista laptop .. configured hotspot with and without encryption with the same result ..limited or no connectivity .. had the same problem on an earlier hotspot using dhcp-server but that version would connect if Pi's eth0 port was connected to my home network ???

    Report
  • Comment Link Jules M Sunday, 16 July 2017 21:50 posted by Jules M

    Running the Zero-W with latest update/upgrade as of 0716. Double checked all edits and scripts. dnsmasq was 2.76 so ran purge dns-root-data. The Pi appears on my laptop wireless network list but connection is 'none or limited' and I get error: raspberrypi systemd[1]: autohotspot.service: main process exited, code=exited, status=203/EXEC ... failed state.... any ideas? Thanks.

    Report
  • Comment Link Graeme Tuesday, 11 July 2017 19:54 posted by Graeme

    Hi Mikkel, The example is right, that line should have been altered. Thanks for pointing it out.

    Report
  • Comment Link Mikkel Monday, 10 July 2017 22:17 posted by Mikkel

    In step 2 it is mentioned that any line containing wlan0 should be commented using #, but in the below example there are lines containing wlan0 without the #.
    Is the example or the description correct?

    Report

Additional information