There are a number of instances where it is useful for your Raspberry Pi to act as its own wifi hotspot, in case you need to access the raspberry but there is no network (or router). These instructions show you how to set this up; along with some scripts which allow you to switch back and forth between settings for the hotpost and accessing your normal home network. I use this with my AndyPi MultiCam, and it’s great for using robots outside too.

It’s worth noting that if you set up these instructions over ethernet, you need to unplug the ethernet cable and reboot before trying to connect to the hotspot, otherwise it will still be your router giving out a the IP addresses, and you’ll fail to connect hotspot without any indication.

1. You’ll need to have Raspbian installed, and have set it up to use a wifi dongle for internet access (connecting to your router in the standard way). You also need to enable the camera (sudo raspi-config, and select option 5).

2. Install hostapd, this is for the RTL8188 chipset found in many wifi adaptors. You might need to do something a little different if your adaptor is not the same

sudo apt-get update

wget http://download.andypi.co.uk/v1.1.tar.gz
tar -zxvf v1.1.tar.gz
cd RTL8188-hostapd-1.1/hostapd
sudo make
sudo make install

3. Create the hostapd configuration file:

sudo nano /etc/default/hostapd

Add the line:

DAEMON_CONF=”/etc/hostapd/hostapd.conf”

4. Edit the hostapd.conf configuration file, to choose your own “ssid”, “wpa_passphrase” (8 – 63 charaters long). Please note – don’t mess about too much with this file – if you find that hostapd fails to start, it is most likely to be an issue with this config file, for example you must have a reasonably long password otherwise hostapd will fail to start without any indication as to why.

sudo nano /etc/hostapd/hostapd.conf

 

5. You need to install dnsmasq, this provides IP addresses to any device that connects to the pi. Your router normally does this job! We’ll create a backup of the standard config file and then enter our own details. Make sure the IP address matches that in your /etc/network/interfaces file

sudo apt-get install dnsmasq
sudo service dnsmasq stop
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.bak
sudo rm /etc/dnsmasq.conf
sudo nano /etc/dnsmasq.conf

Enter the following lines, Ctrl-x to exit and Y to save:

interface=wlan0
expand-hosts
domain=local
dhcp-range=10.0.0.10,10.0.0.20,24h
dhcp-option=3,10.0.0.1

6. Create TWO network interfaces files with the following names, in your home directory:

Firstly, interfaces_home:

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

Secondly, interfaces_hotpsot:

auto lo
auto wlan0
iface lo inet loopback
allow-hotplug wlan0
iface wlan0 inet static
address 10.0.0.1
network 10.0.0.0
netmask 255.255.255.0
broadcast 10.0.0.255

6b. We also need to edit wpa_supplicant, (make sure you put in the SSID and passphrase for your router, this one shows an example for an open wifi network with no password)

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

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

network={
ssid=”YOURSSID”
key_mgmt=NONE
auth_alg=OPEN
}

 6c. We also need to create a copy and backup of rc.local which we can switch between. The only reason that this is needed is because hostapd doesn’t seem to work on first boot, so we set rc.local (hotspot mode) to restart the networking service, and then restart hostapd:

sudo cp /etc/rc.local rc.home
sudo nano /etc/rc.local

before the displaying the ip address, enter the folowing lines
sudo service networking restart
sudo service hostapd restart
Ctrl-X and Y, press enter to save

sudo cp /etc/rc.local rc.hotspot

7. Next, create two scripts to switch between the two network options, again in your home directory:

Firstly, wifi2home.sh

#!/bin/bash
# — Switch wifi to home network
sudo rm /etc/network/interfaces
sudo cp interfaces_home /etc/network/interfaces
sudo rm /etc/rc.local
sudo cp rc.home /etc/rc.local
# turn off hostapd
sudo update-rc.d hostapd remove
sudo update-rc.d dnsmasq remove
sudo shutdown -r now

Secondly, wifi2hotspot.sh

#!/bin/bash
# — Switch wifi to hotspot mode
sudo rm /etc/network/interfaces
sudo cp interfaces_hotspot /etc/network/interfaces
sudo rm /etc/rc.local
sudo cp rc.hotspot /etc/rc.local
# turn on hostapd and dnsmasq
sudo update-rc.d hostapd defaults
sudo update-rc.d dnsmasq defaults
sudo shutdown -r now

8. Use the script and files we have just created to switch to your raspi + camera to a wifi hotspot. At this point, test everything is working correctly, i.e. that you can use your laptop/tablet to connect to this wifi hotspot, and that you can access the raspberry pi’s home folder as a network folder. Run the other script to switch back to your home (router) network, and check this works too. It’s worth doing this since when you’ve got the camera in a nice box, you don’t want to have to unscrew things all the time if you have a software / network access issue!

sudo bash wifi2hotspot.sh (to switch back use the other script)