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).
#DHCPD_CONF=/etc/dhcp/dhcpd.conf

# Path to dhcpd’s PID file (default: /var/run/dhcpd.pid).
#DHCPD_PID=/var/run/dhcpd.pid

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

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

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

authoritative;

subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.130 192.168.0.150;
option domain-name-servers 8.8.8.8, 8.8.4.4;
option domain-name “local”;
option routers 192.168.0.128;
option broadcast-address 192.168.0.255;
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
address 192.168.0.128
netmask 255.255.255.0
gateway 192.168.0.1

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

#iface static_ip inet static
# address 192.168.0.128
# netmask 255.255.255.0
# gateway 192.168.0.1

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

 

————————————————————————
sudo nano /etc/rc.local

Replace networkname with network it should try to connect to.

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

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

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

createAccessPoint(){
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 ”
Interface=’wlan0′
HostAPDIP=’10.0.0.1′
connected=false

if iwlist wlan0 scan | grep network-name > /dev/null
then
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
then
echo “Connected to WiFi”
connected=true
else
echo “DHCP server did not respond with an IP lease (DHCPOFFER)”
wpa_cli terminate
fi
else
echo “Not in range, WiFi with SSID: Trevangsvej 197”
fi

if ! $connected; then
createAccessPoint
fi

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

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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