Last week I posted a vine video of one of the cool raspberry pi features I just got working for cBAM and a few people asked for more details about how I did it e.t.c. The short answer is ‘I used the fantastic PiUi plugin provided by David Singleton’ the long answer is the short answer plus lots of swearing at slightly inaccurate how-to guides. So I thought a quick combined write up of these guides might also help anyone else Googling around for solutions.
Things you will need
- RaspberryPi (+monitor, keyboard, mouse, operating system, power supply, power, a surface to put it on, a roof to keep water off, etc..)
- The internet
- Some kind of other internet linked device
Step 1: Set up connection
So the first step in using PiUi is making sure that you can login to Raspberry Pi from another device. All you need to do is setup the Raspberry Pi so that it is broadcasting a wi-fi network you can log into. That one simple sentence belies how difficult that actually is to do. Thankfully the process is made a little simpler by the existence of a module called Pi-Point which handles most of the details. Unfortunately the documentation for pi-point is a little patchy and hilariously the ‘Online docs‘ and the ‘Docs in PDF format‘ don’t actually contain all the same instructions. So to speed this up I’ve reproduced a combination of them in the steps below – if you have any problems please go read both of them as they contain more details.
- Update your Pi libraries with (all commands are in the LXTerminal)
- Install the following packages
sudo apt-get install rfkill
sudo apt-get install zd1211-firmware
sudo apt-get install hostapd
sudo apt-get install hostap-utils
sudo apt-get install iw
sudo apt-get install dnsmasq
- Edit the hostap file with
sudo nano /etc/network/interfacesand make sure it looks like the below (you can save with Ctrl+O);
iface lo inet loopback
iface eth0 inet dhcp
iface wlan0 inet static
- Restart the Raspberry Pi using the command
sudo rebootor just unplugging it and plugging it back in again (the first method is considered a bit more technical than the second)
- Edit the hostap config file with
sudo nano /etc/hostapd/hostapd.confthis may be blank but make sure it has the following contents (you can save with Ctrl+O);
- Restart hostapd with the command
sudo service hostapd restartfollowing this you should be able to see your new wi-fi network being broadcast on other devices
- Edit the dnsmasq config file with
sudo nano /etc/dnsmasq.confthis is a really really long file with 99% of it commented out (has a # at the start) but you need to look for these lines, un-comment them (delete the #) and make sure their values match up to the list below.
- Restart hostapd with the command
sudo service dnsmasq restartthis will now let you log into the network and you should now be assigned a proper IP address.
- Finally you need to make sure that this all runs and starts when the Raspberry Pi starts, so you need to create an init.d script . To do this, create a new script with
sudo nano /etc/init.d/pipointand fill it with the following contents.
# Configure Wifi Access Point.
### BEGIN INIT INFO
# Provides: WifiAP
# Required-Start: $remote_fs $syslog $time
# Required-Stop: $remote_fs $syslog $time
# Should-Start: $network $named slapd autofs ypbind nscd nslcd
# Should-Stop: $network $named slapd autofs ypbind nscd nslcd
# Default-Start: 2
# Short-Description: Wifi Access Point configuration
# Description:starts hostap
### END INIT INFO
# start the access point
hostapd -B /etc/hostapd/hostapd.conf
- You should now be able to start the Raspberry Pi and immediately log into the wi-fi network it is broadcasting. If not, then keep randomly changing things until it works 🙂
Step 2: SetUp PiUi
PiUi is a python based set of packages that essentially sets up a webpage with a range of controls that feedback to the Raspberry Pi. The read-me file for PiUi is pretty good and available here. However, for the sake of keeping everything together, I have also produced a quick-start step by step guide below.
- Add mapping for PiUi to the hosts file load up in the nano editor with
sudo nano /etc/hostsand add the line
- Install the nginx package with the command
sudo apt-get install nginx
- Get the PiUi code from github with
sudo git clone https://github.com/dps/piui.git
- Copy the nginx file to from the PiUi code to the config directory with
sudo cp /home/pi/piui/nginx-conf/nginx.conf /etc/nginx/
- Copy the startup script ot the start up folder with
sudo cp /home/pi/piui/supervisor/piui-supervisor /etc/init.d
- Run the PiUI setup script which installs any other required files with the command
sudo python piui/setup.py install[Thanks to Adam for pointing out this additional step 24/07/13]
- Reboot Raspberry Pi
- Run the demo in the PiUi directory with
- Using any device with a browser login to the wi-fi network broadcast by the Raspberry Pi and go to the 192.168.1.1
- Enjoy god like Raspberry Pi powers at your finger tips!
Step 3: Make it dance
Now we’ve got it up and running the last step is to modify the code to run other scripts. This is pretty basic python stuff and depends on what you want to link it to. However, the following steps are a good place to start playing around for anyone a little un-sure of how to use it.
- Using an editor load up the piui_demo.py code on the Raspberry Pi. This can either be done using Geany or using the nano editor we used before with the command
sudo nano /piui/piui_demo.py
- This file will be pretty long and full of def functions but the one you want to start playing with is
def ondownclickthese are the commands associated with the ‘Buttons’ page in the demo
- You can then modify these commands by adding in your own lines of code which will run every time you press these buttons. The simplest way to get these buttons to run code is to have it call other pre-prepared python scripts. You can do these several ways but a very reliable way is to use the call function.
- First add the line
from subprocess import callto the very top of the piui_demo.py file with all the other import commands
- In the def function (either up or down it doesn’t matter) add the line
call (["sudo python /home....location and name of your python file"],shell=True)
filling in the name and location of the python file you want to call
- Now when you run the piui_demo.py file as at the end of step 2 pressing you chosen button in the browser will now execute the python file you’ve setup.
- First add the line
I hope this is of some help to anyone wanting to repeat what I’ve managed to do. If you have any problems,
just post them in the comments section and I’ll do my best to answer them. then good luck fixing them, it’s been years and I’ve forgotten how all this works.