Return to Robotics Tutorials

Raspberry Pi - Wireless hotspot

Control a Raspberry Pi from anywhere!

While building a retro Halloween robot costume with a Raspberry Pi (RPi), I realized that being able to control the RPi from an iPhone (outside of your home) or other device would enable a lot of cool features! For example:

  • Remote sensor telemetry / logging
  • Command-line / SSH login to RPi
  • Speech synthesis / control remotely
  • ... and many other fun things

Since I was running the RPi headless (ie. not connected to a physical monitor or keyboard), I needed some way to log in to it. SSH is a great solution, provided that your iPhone or desktop PC can see the Raspberry Pi on the local network. If you want to connect outside of your local network, then you may need to set up a wireless hotspot or access point (AP) on the Pi itself.

Using iPhone Personal Hotspot

At first, I thought it may be possible to use an iPhone's Personal Hotspot and then connect from the iPhone to the Pi. After creating the personal hotspot, it is easy to connect the Pi to the iPhone. Unfortunately, it does not appear that the phone can also SSH to the Pi when in this mode.

Creating a Wireless Access Point on the Pi

An access point (AP) provides wireless access to a wired network (LAN). The wired network is usually connected to a modem or router with access to the Internet. The typical use-case is for providing mobile devices access to the internet.

In reality, if you are only trying to log in to your Pi when outside the home, you can use a simpler solution: a wifi hotspot. A hotspot is easier to configure than a full access point.

Creating a WiFi Hotspot on the Pi

If you create a WiFi Hotspot on the Pi, then you will be able to connect your phone or desktop computer to the Pi and control it (eg. via SSH, telnet, etc.). This is extremely useful if you have a hobby project that you have created with the Pi that you would like to control outside of your home network. With this goal in mind, you are not looking for the Pi to provide internet access to your mobile devices; instead you are only aiming to permit wireless connections so that you can log in to the Pi -- a hotspot is an ideal solution.

There are many pages on the web that describe setting up a wireless access point on the Pi, but I found that the majority of them didn't work without a number of modifications. The aim of this page is to demonstrate a minimal set of steps to get the hotspot running on the Pi.

  • Begin from NOOBS or Raspbian Wheezy OS installation
  • Use a suitable WiFi USB dongle (eg. RAlink RT5370)
  • Define a new subnet for the RPi hotspot (to differentiate it from your home WLAN)
  • Assign static IP address to RPi (this is not required, but makes connecting easier)
  • Configure the access point authentication
  • Install an access point daemon (hostapd) with dynamic IP address support

Suggested access software

  • Free SSH client on iPhone: vSSH Lite
  • Free SSH client on Windows: putty
  • Free FTP/SFTP client on Windows: WinSCP

Summary of Steps

In order to accomplish the above, we need to modify a few files. I recommend backing up the files first so that we can revert later and switch between modes (eg. RPi connecting to router vs RPi providing a hotspot).

  • Optional: Install Raspbian OS
    If not already installed, use the NOOBS installer
  • Optional: Configure Pi to access home router network
  • Install hostapd and dnsmasq
  • Modify /etc/network/interfaces
    This file defines the IP address we will use for the hotspot, along with the subnet that it will appear on. For example, I am using a subnet of 192.168.10.X and a static IP address of 192.168.10.1. The default config will configure the Pi to use DHCP to connect to your router (dynamic IP addresses). In my config, I have also forced a static IP address instead (optional). This file also provides a link to the wpa_supplicant config which details the authentication details of your WPA or WPA2 network.
  • Modify /etc/default/hostapd
    Defines the location of the access point config file (hostapd).
  • /etc/hostapd/hostapd.conf
    By default, this config file may not exist, so we will create one. It defines the authentication (ie. passwords, key exchange, etc.) that you will use to connect to your Pi. You will edit this file to define your passphrase. It also defines the driver which will be used (nl80211 in my case).
  • Modify /etc/init.d/hostapd
    Defines startup configuration of hostapd.
  • Modify /etc/dnsmasq.conf
    dnsmasq provides a number of capabilities -- the most important one for us is the DHCP server. The DCHP server is responsible for defining the range of IP addresses granted to devices connecting to the wifi hotspot (ie. dynamic IP addresses).
  • Configure hostapd to start after boot

Detailed Step-by-step Guide

Install the Raspbian operating system

This guide assumes a fresh operating system installation. If you need to install the Raspbian OS, I recommend using the NOOBS installation:

(Updates coming soon)

Confirm the wireless device type

Assuming that a wireless USB device has been plugged in, we can determine the type of device by running the following command: lsusb. The device name will help one determine what drivers are needed (if the nl80211 driver doesn't work for you).

pi@raspberrypi:~ $ lsusb


Bus 001 Device 005: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 012: ID 10d5:55a2 Uni Class Technology Co., Ltd 2Port KVMSwitcher
Bus 001 Device 011: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 010: ID 045e:00db Microsoft Corp. Natural Ergonomic Keyboard 4000 V1.0
Bus 001 Device 009: ID 058f:9254 Alcor Micro Corp. Hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
					

In the above report, we can see RT5370 is the wireless adapter in use.

Optional: Set up wireless access to router

Within the raspbian graphical operating system (launched from the command line via startx if it doesn't come up automatically on boot), you can usually find a networking icon in the top-right corner of the desktop (dhcpcdui). Mousing over this icon may report something like the following:

eth0: Link is down
wlan0: Not associated
					

The above means that we are not connected to a wireless network at the moment. We can left-click on the icon and see a list of SSIDs in the area. Click on the SSID for your home network and then enter the Pre Shared Key. This is the password you have already configured for your home router. Click OK.

Now, upon mousing over the wireless icon, you may see a report like the following:

wlan0: Associated with mynetwork
wlan0: Configured 192.168.1.197/24
eth0: Link is down
					

For reference, configuring your wireless access via the GUI will generally make changes to the following config files:

  • /etc/network/interfaces
  • /etc/wpa_supplicant/wpa_supplicant.conf

At this point you should have access to the Internet from the Pi (a quick test of the built-in web browser should confirm this). Alternately, if you were to type ifconfig at the command-line, you should now see a new line starting with inet addr: that details the IP address the Pi has been allocated.

I recommend making a backup copy of the /etc/network/interfaces file so that we can swap back to router mode later.

Optional: Force static IP address

Generally, it is must easier to connect to your Pi later if it was assigned an IP address that was always the same. The default setup will allow the router to provide an IP address dynamically which means that it could change from time to time. By forcing a static IP address, you can usually connect to the Pi without having to take additional steps to determine what address was assigned to it.

Modify the /etc/network/interfaces file and look for the following line:

iface wlan0 inet manual					
					

Let's change it to the following, which will request an IP address of 192.168.1.153:

iface wlan0 inet static
  address 192.168.1.153
  netmask 255.255.255.0
  gateway 192.168.1.1					
					

After rebooting, we can again confirm that the static IP address has been granted (by looking at the boot log, checking the "configured" status under dhcpcdui or running ifconfig from the command line).

At this point , I recommend making a backup of the /etc/network/interfaces file so that we can swap back to this router configuration later.

Install hostapd and dnsmasq

Enter the following command to install both tools:
sudo apt-get install -y hostapd dnsmasq

Modify the config files

If you previously modified the interfaces file to force a static IP address, then:

  • Modify \etc\network\interfaces
    Insert the following line near after the iface eth0 line:
    auto wlan0
    Comment-out (with '#') the following line that appears after iface wlan0 inet:
    ##wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

If you didn't modify the interfaces file to force a static IP address (ie. you're going to use dynamic IP addresses on the Pi), then:

Configure hostapd to startup after boot

Enter the following command:
sudo update-rc.d hostapd enable

Reboot

Enter the following command to reboot:
sudo shutdown -r now

Usage

To keep things simple, when I change the Pi to hotspot mode, the Pi is no longer able to connect to other networks (eg. my home router or the internet). This means that during development I run in home router mode, but when I plan to leave the home and use the Pi outside, then I switch to the hotspot mode. A simple shell script is used to switch between the two modes (restart required).

I have provided links to these shell scripts below under "Sample Files".

  • Normal mode (Pi connect to router)
    ./do_swap_net_router.cmd
    sudo halt (to restart)
  • Hotspot mode (devices connect to Pi)
    ./do_swap_net_hotspot.cmd
    sudo halt (to restart)

Sample Files

(File downloads coming soon)

References

The following web articles helped provide portions of the configuration used to get this working:

 


Reader's Comments:

Please leave your comments or suggestions below!

 


Leave a comment or suggestion for this page:

(Never Shown - Optional)
 

Visits!