Setup automatic switch from WiFi to Hotspot

My robot has and EdiMax WiFi adapter, and at startup it automatically connects to my home network, with a static ip.

But when I take the robot to a new location, It cannot automatically connect to a new WiFi, cause it doesn’t know the Network name and password.

Luckily someone has already solved this problem. At startup it checks and tries to connect to a known WiFi network.

If it cannot connect to a WiFi network, it setup a Hotspot.

This makes it possible to connect to the Robot without a wired connection or keyboard and a screen.

I used this guide to setup a Hotspot for the EdiMax WiFi adapter.

I used this guide to test at startup if it could connect to a known WiFi and otherwise setup a Hotspot.

Here are the files that I changed:
sudo nano /etc/default/isc-dhcp-server

# Defaults for isc-dhcp-server initscript
# sourced by /etc/init.d/isc-dhcp-server
# installed at /etc/default/isc-dhcp-server by the maintainer scripts

# This is a POSIX shell fragment

# Path to dhcpd’s config file (default: /etc/dhcp/dhcpd.conf).

# Path to dhcpd’s PID file (default: /var/run/

# Additional options to start dhcpd with.
# Don’t use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. “eth0 eth1”.

sudo nano /etc/dhcp/dhcpd.conf
Only uncommented code is shown


subnet netmask {
option domain-name-servers,;
option domain-name “local”;
option routers;
option broadcast-address;
default-lease-time 600;
max-lease-time 7200;

sudo nano /etc/network/interfaces

auto lo wlan0

iface lo inet loopback

allow-hotplug eth0

iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0 inet static

#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

#iface static_ip inet static
# address
# netmask
# gateway

up iptables-restore < /etc/iptables.ipv4.nat


sudo nano /etc/rc.local

Replace networkname with network it should try to connect to.

# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ “$_IP” ]; then
printf “My IP address is %s\n” “$_IP”

sudo mount -t cifs -o user=pi,password=raspberry,rw,file_mode=0777,dir_mode=0777 $

echo “Creating AccessPoint network”
echo “Attempting to start hostapd and dhcp server”
service hostapd start
service isc-dhcp-server start
echo “Accesspoint created”

echo “=================================”
echo “RPI Network conf bootstrapper 0.1”
echo “=================================”
echo “Scanning for known WIFI networks ”

if iwlist wlan0 scan | grep network-name > /dev/null
echo “WiFi in range has SSID: network-name
echo “Starting supplicant for WPA/WPA2”
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/$
echo “Obtaining IP from DHCP”
if dhclient -1 wlan0
echo “Connected to WiFi”
echo “DHCP server did not respond with an IP lease (DHCPOFFER)”
wpa_cli terminate
echo “Not in range, WiFi with SSID: Trevangsvej 197”

if ! $connected; then

exit 0

sudo nano /etc/sysctl.conf

I did not uncomment the line

# Uncomment the next line to enable packet forwarding for IPv4
# net.ipv4.ip_forward=1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s