Getting devices on a wifi network easily is one of the most annoying things about IoT type devices, so being able to do this in a user-friendly manner is very useful. This is what this script does. When the Pi boots up, if it can’t find a wifi network it knows about, it turns into an access point (AP), which you can then connect to using a phone or laptop. That pops up a captive portal in your browser, in a page that lists the wifi networks available. You type in the password for the one you want, reboot within that interface, and when the Pi comes back up, it’ll be on the wifi.
The idea is that this could be part of a physical device (such as an internet radio) with very little in the way of interface of its own – instead it uses your phone or laptop as the interface, both for configuration and for control. Chromecasts do something similar. Unlike the Chromecast, this one doesn’t need an application, just a web page, so it’s a little more manual.
Andrew Nicolaou wrote the code – although he now prefers a resin version, as he describes on his blog. I’m stubborn, so against his advice, I tried the Radiodan version on Jessie. It works, although the Jessie version of hostapd seems to be broken for our purposes, so I had to use an old one.
- Runs an access point if it can’t connect to a network, making 6 passes to detect network names
- Runs a captive portal popup using nginx, showing the list of available network names
- Allows you to enter a password for one, which after a reboot will connect it to the network
Instructions from scratch on a Pi 2
Download Jessie img from the Raspberry Pi site and unzip it.
diskutil list diskutil unmountDisk /dev/diskn sudo dd bs=1m if=~/Downloads/2015-11-21-raspbian-jessie.img of=/dev/rdiskn
(I’ve put “diskn” to indicate the disk number you get from diskutil list – for me it was “disk2”. Here are the official instructions for installing operating systems for the Pi).
When it’s done, put it in the Pi, boot up and ssh or log in.
Expand the filesystem by running
and picking the appropriate option.
Reboot and ssh in again.
Download the Radiodan installation scripts
sudo mkdir /var/log/radiodan git clone https://github.com/radiodan/provision cd provision sudo ./provision avahi wpa nginx
This installs avahi, an old hostapd (hostapd_1.0-3ubuntu2.1_armhf.deb), wpa_supplicant and nginx, plus a custom shell script that does the actual work, and init.d scripts to make it run on boot, plus a hostapd config file and nginx files. It modifies /etc/rc.local to reference the shell script.
Then reboot, unplugging ethernet if that’s what you’re using to talk to it.
After a minute or two you’ll see a wifi network name “radiodan-configuration“. Join that and a captive portal will pop up in your default browser. Click on the network you want to connect it to and type in the password. Reboot by clicking the red “restart now” button.
When the Pi reboots it will connect to the wifi you specified (it rewrites the /etc/wpa_supplicant/wpa_supplicant.conf config file). WiPis handily glow blue when they connect (they don’t glow in access point mode).
If you are also on that network, you can ssh to it using firstname.lastname@example.org because we’ve installed avahi.
I’ve also found it useful just to have the Pi in access point mode for various things (you can connect to it over ssh on its fixed IP in this mode – 10.0.0.200).
Because this is designed for Radiodan, once rebooted, the nginx config file assumes a server running on port 5000. Obviously you can change this for your own purposes. If you want to change the AP name, you can do so in /etc/hostapd/hostapd.conf.