How to: use PiUi

Update: I checked (16-03-2016) and PiUi development appears to have stopped 3 years ago. I suggest trying to setup your own Apatche system instead

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.

PiUi

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.

  1. Update your Pi libraries with (all commands are in the LXTerminal) sudo apt-update
  2. Install the following packages
    1. rfkill sudo apt-get install rfkill
    2. zd1211 sudo apt-get install zd1211-firmware
    3. hostapd sudo apt-get install hostapd
    4. hostap-utils sudo apt-get install hostap-utils
    5. iw sudo apt-get install iw
    6. dnsmasq sudo apt-get install dnsmasq
  3. Edit the hostap file with sudo nano /etc/network/interfaces and make sure it looks like the below (you can save with Ctrl+O);
    auto lo
    iface lo inet loopback
    iface eth0 inet dhcp
    iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
  4. Restart the Raspberry Pi using the command sudo reboot or just unplugging it and plugging it back in again (the first method is considered a bit more technical than the second)
  5. Edit the hostap config file with sudo nano /etc/hostapd/hostapd.conf this may be blank but make sure it has the following contents  (you can save with Ctrl+O);
    interface=wlan0
    driver=nl80211
    ssid=Testy-fi
    hw_mode=g
    channel=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=supergoodpassword
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP CCMP
    rsn_pairwise=TKIP CCMP
  6. Restart hostapd with the command sudo service hostapd restart following this you should be able to see your new wi-fi network being broadcast on other devices
  7. Edit the dnsmasq config file with sudo nano /etc/dnsmasq.conf this 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.
    domain-needed
    interface=wlan0
    dhcp-range=192.168.1.5,192.168.1.254,254.255.255.255.0,12h
  8. Restart hostapd with the command sudo service dnsmasq restart this will now let you log into the network and you should now be assigned a proper IP address.
  9. 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/pipoint and fill it with the following contents.
    #!/bin/sh
    # 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
    # Default-Stop:
    # Short-Description: Wifi Access Point configuration
    # Description:starts hostap
    ### END INIT INFO
    # start the access point
    hostapd -B /etc/hostapd/hostapd.conf
  10. 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.

  1. Add mapping for PiUi to the hosts file load up in the nano editor with sudo nano /etc/hosts and add the line
    192.168.1.1 piui
  2. Install the nginx package with the command sudo apt-get install nginx
  3. Get the PiUi code from github with sudo git clone https://github.com/dps/piui.git
  4. 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/
  5. Copy the startup script ot the start up folder with sudo cp /home/pi/piui/supervisor/piui-supervisor /etc/init.d
  6. 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]
  7. Reboot Raspberry Pi
  8. Run the demo in the PiUi directory with python /piui/piui_demo.py
  9. 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
  10. 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.

  1. 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
  2. This file will be pretty long and full of def functions but the one you want to start playing with is def onupclick and def ondownclick these are the commands associated with the ‘Buttons’ page in the demo
  3. 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.
    1. First add the line from subprocess import call to the very top of the piui_demo.py file with all the other import commands
    2. 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
    3. 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.

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.

13 Comments for “How to: use PiUi”

Mel

says:

Hey Matthew

I am really interested in using the PIUI app to connect to the raspberry Pi for a project for university but i am fairly new to using the Raspberry Pi. I tried your step 1 and tried playing around with it but i cant seem to get the access point working or showing up on my phone. Are there any tips you can tell me about what i can do to get it to work. also i couln’t get iw installed could that have anything to do with it??:?

says:

Hey there followed ur steps (with a few changes) and got my raspi to be an woreless AP and got the app on my phone to connect to the raspberry pi but stuck on step2.6. i keep getting an error message and cant figure out why the file wont open even if its there.

pi@raspberrypi ~ $ sudo python /piui/setup.py install
python: can’t open file ‘/piui/setup.py’: [Errno 2] No such file or directory

Really desperate to get this to work and any help would be awesome

Cheers
Melroy

says:

Hi Melroy,

I know you contact me on twitter eventually to solve this but I wanted to post the fix you helped find here..

In the How-To the command: sudo python piui/setup.py install had an extra back-slash in the wrong place. I’ve removed it and hopefully it will now work for everyone.

Matthew

John

says:

I’m having the same problem as “Mel” above. Got to step 1.6 and cannot see the SSID being broadcast. Any ideas?

Thanks to the author for the tutorial. Some corrections/additions:
1. Step 1.2.D has a misspelling “sudo apt-get install hastap-utils” should be “sudo apt-get install hostap-utils”.
2. Step 1.3 and 1.5 require the user to type “CTRL-O” to save what they edited before closing. That was not in the bottom menu and could be useful to others.

mathew

says:

how do I STOP a session

says:

Drop it out the window?
Hit it with a hammer?
Pour a glass of water on it?

In all seriousness, this article is 2.5 years old I can’t remember any more. Also I note that PiUi development seems to have stopped about 3 years ago so I’m not even sure PiUi is supported anymore. Sorry I can’t be more help.

There’s a how to do it your self here which is my suggestion: http://www.instructables.com/id/Simple-and-intuitive-web-interface-for-your-Raspbe/step2/Installing-and-using-the-Wiring-Pi-library/

Leave a Reply

Your email address will not be published. Required fields are marked *