Bluetooth on the Pi

Here we enter the murky world known colloquially, by me, as “Oh, Bluetooth, why do you mock me?”
This has been the biggest pain in the butt to get working, but hey, no-one ever said that the RPi was going to be easy.

Nice logo, isn’t it. Took me all of 30 seconds to search for it on Google images.

Installing the Bluetooth stack

Let’s remember, folks, Bluez is the official bluetooth stack for Linux! So it should work on everything, yes? Yeah, right. Here we go with the steps…

  1. Start the Pi, login as root.
  2. apt-get install bluetooth bluez-utils blueman
  3. From left to right, that’s:
    1. The stack. (A very large thing indeed)
    2. Command line utils. (A very useful thing indeed)
    3. X-windows interface. (A very slow thing indeed).
  4. Go and make yourself a coffee. This takes ages, especially over wi-fi. In fact, you could probably go and cook dinner.

Scanning and connecting

Onto the good bit. Getting your bluetooth device connected. In my case, I have a Rii bluetooth keyboard/touchpad.

My one came with a nano USB bluetooth dongle. In other words, it’s very small. Which is good.

  1. Okay, so you have the stack installed.
  2. Turn your Rii on and put it in discoverable mode.
  3. On your Pi, type “hcitool scan” to see if you can find your device.
  4. You should get a list of devices back in your area. The Rii comes up as “Macro Keyboard”. A stunning name, to be sure.
  5. Make a careful note of the string of numbers, colons and letters.
  6. Do the following to create a connection script:
    1. cd ~
    2. mkdir bin
    3. cd bin
    4. Create a file called auto_connect_bluetooth.sh and open it in your editor. Add the following lines. Put the string of numbers, colons and letters that you noted down before instead of XX:XX etc.
      #!/bin/sh/usr/bin/bluez-test-device trusted XX:XX:XX:XX:XX:XX yes/usr/bin/bluez-test-input connect XX:XX:XX:XX:XX:XX
    5. Save the file and exit from the editor.
  7. chmod +x auto_connect_bluetooth.sh
  8. (This makes the file executable so you can do the next bit).
  9. Open /etc/rc.local in the editor. Add the following command before the exit command at the end of the file.
    1. sudo -u root /root/bin/auto_connect_bluetooth.sh
  10. This should run your connection script before the login prompt comes up.
  11. Before you do a reboot, make sure your bluetooth device is still in discoverable mode.
  12. Reboot and see if your device connects!
  13. Well… it doesn’t.
  14. You need to go into X windows (startx) and use the blueman-manager applet to pair up the device, trust it and connect to the input service.
  15. Once you’ve done that, and reboot, and it should connect this time.
If you find those last few steps unfollowable, comment on this post and I’ll try and flesh it out a bit.

Setting up VNC server on the Pi

Install VNC and Auto Start

Here’s how to install VNC and get it working with the Pi across a network

First of all, make sure you expand the SD card – you don’t want to run out of space!

  1. apt-get install tightvncserver
  2. Yes, you want it to install, so accept the space it tells you it’s going to use!
  3. It’ll go through the usual install procedure and eventually put you back at the shell prompt.
  4. Type:
    1. tightvncserver :1
    2. (This will create a few files it needs to run)
    3. Set a password when it asks. Don’t bother with a view password unless you really want one – it’s just a pain.
  5. Edit the /etc/rc.local file and add the following before the ‘exit’ command at the bottom.
    1. sudo -u root /usr/bin/vncserver :1 -geometry 1024×768 -depth 16 -pixelformat rgb565 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/
  6. Save the file, back to the prompt you go.
  7. This will mean that your VNC server starts up before you need to login on port 1 and a screen size of 1024×768.
  8. Reboot. Watch that magic happen. Dude. S’up.

The reason you do sudo is that it needs to know who to run the server as – remember, you’re not logged in before the login prompt comes up!

Now, you should be able to reboot and connect to the VNC server from your laptop, for instance.
You’ll need to download the Tight VNC viewer to do this.

Adventures in WiFi

Using wicd-curses

I know of no better way than getting WiFi to connect automatically than using the wonderful wicd-curses package. So, here’s how to do it:

  1. Plug your wifi dongle in. I cannot impress on you enough how important this is 😉
  2. If you’ve not done it already, login as root and install wicd-curses:
    1. apt-get install wicd-curses
  3. Run it. Just type
    1. wicd-curses
  4. You’re given the wicd interface. You probably haven’t got anything showing yet, so do Shift-R to refresh the network scan.
  5. Use the arrow keys to highlight your router and press –>
  6. Go down and hit space when you reach the Automatic connection checkbox.
  7. Then, go down to the password field and put your wifi router password in.
  8. Hit F10 to save the settings and you’ll go back to the first screen.
  9. Highlight your router if it’s not already and hit shift-C to connect.
  10. Hopefully, this will connect you. If not, check your password!
  11. Do shift-Q to come out of wicd.
  12. Type ifconfig -a and you should see your wlan0 connected to your router and your ip address displayed.
The beauty of doing it like this rather than via the command line is that wicd runs as a daemon when your Pi starts up and will automatically connect to the network without you doing anything else.
NB: You may have to wait a minute or so for it to connect.

Operating Systems… So many, and yet so far

Oh, the trials and tribulations (or Tribbleations) of installing an operating system on the Pi. I tried about 5 operating systems before succeeding in my goals including Debian Wheezy, Fedora, Arch, Risc OS… They all failed in some way, mostly to do with a) WiFi and/or b) Bluetooth.

Eventually, I found an image that did work: Debian 6 19-04-12. And, yes, I do realise that this was the recommended one at the time… That’ll teach me to go cutting-edge! With this distro, I got everything working. Since then, the recommended distro has changed to the Raspian version of Debian Wheezy. Here’s the original RPi foundation post.

So, into the wonderful world of Raspian we go! Now, may aim here is to install everything without getting up from the sofa. i.e. No LAN connection

Here are the steps I took to get the Pi up-and-running. I might change tenses throughout, but hopefully I haven’t missed any steps! I’m on Windows 7, 64 bit, so any problems I have running apps on Windows may not affect you.

Installing the Operating System before the Pi starts up

  1. Download the Raspian image from the Raspberry Pi foundation.
  2. Get the SD card out of your Pi and put it into your laptop. 
  3. The following steps are only if you have a current SD card OS for the Pi:
    1. Download Winimage. This is far better than the Win32diskimager, in that it starts up more reliably each time. I do, however, think that it’s a bit of a rip-off at $30. You may find other packages that can backup and restore images, and most of them are fine.
    2. Backup your card with Winimage (or whatever you have).
  4. Carrying on, we’re going to install Raspian onto the card.
    1. Using Winimage/Win32DiskImager, restore the image you downloaded onto the SD card. !!! Make sure you are restoring to the card and not your hard drive 😉
    2. If your SD card is larger than 2GB, you’re going to want to expand the card so that the OS has more room to breathe.
      1. Insert a USB stick, preferably one that is 2GB or more.
      2. Download the gparted Linux ISO.
      3. Using either Winimage, Win32DiskImager, unetbootin or the LinuxLive USB Creator (my preferred method), install the ISO you downloaded to the USB stick.
      4. Reboot your laptop with the USB stick in and have the laptop boot from the USB stick. You can do this in a variety of ways, depending on your laptop. Sometimes, you have to go into the Bios to make this work.
      5. Once the gParted OS is up, just follow the instructions and steps until you get the desktop.
      6. At this point, a window will open showing the partitions on your SD card. You should have a FAT format bit at the beginning, then a Linux partition and then an unused part at the end.
      7. Right-click the Linux partition and choose to Move/Resize. Click and drag the extent of the partition size bar until you have used ALL the disk EXCEPT about 1-2 GB at the end. Click OK or Continue.
      8. Now right click on the smaller unused space and make a new partition. Just use ext2 for now. This will eventually be our swap space (I hope!).
      9. Make sure you apply the changes and wait for it to finish.
      10. Shutdown the gParted OS desktop. This should turn your laptop off.
      11. Remove the USB stick. Your SD card should stay in for now.
  5. Start up your laptop again to Windows.
  6. Download the Linux firmware for the USB WiFi dongle you are going to use.
    1. My dongle runs on the ZD1211 firmware so I downloaded it from Sourceforge.
    2. Unzip/untar the firmware into a folder on Windows.
    3. Copy the folder onto the SD card.
  7. Remove the SD card safely.

First Pi start-up

  1. Put the SD card carefully into the Pi.
  2. Insert your dongle (oo-er, Mrs) into the Pi, and plug in a keyboard and a monitor. You will not get anywhere unless you can see what the Pi is doing and can type stuff in!
  3. Put the power lead in, try not to get motion-sick from the Linux start-up scrawl.
  4. When the prompt comes up, use the ‘root’ username. There won’t be a password at the moment.
  5. This should give you the shell/command line prompt.
  6. Now, you need to mount the SD card, despite it actually being what you’re running off. This is because you want access to the FAT partition where the firmware is.
  7. First of all, you want the number one partition on the SD card. I can’t remember what that is at the moment, but it’s the one in /dev/ that begins with mmc and has a ‘1’ in it.
    1. ls /dev – get the name of the mmc device to put into the mmc*** space below.
    2. mkdir /mnt/sd
    3. mount /dev/mmc*** /mnt/sd
    4. mkdir /lib/firmware/zd1211
    5. cp /mnt/sd/zd1211/* /lib/firmware/zd1211/
  8. You should now have your firmware in the right place.
  9. At the prompt, type ‘reboot’. This will, surprisingly enough, reboot the Pi. Hopefully. If it fails to come back up, pull the power, wait a few seconds and re-insert.
  10. When the Pi comes back up, your firmware should have loaded and your dongle should now be active.
  11. Fortunately, the library necessary for connecting to my WPA-secured router is included in this distribution. If it wasn’t, I’d have to plug it into the LAN and do:
    1. apt-get install wpa-supplicant
  12. But it is, so I don’t have to.
  13. You now need to know the ID of your router (the SSID to be more exact) and the connection password.
  14. Now, you will need to edit the file /etc/wpa_supplicant.conf
    1. I use vi, you don’t have to. I’m going to assume you’re using ‘nano’ which is easier to use.
    2. nano /etc/wpa_supplicant.conf
    3. This will create a new file.
    4. Add the following to the file:
      network={
      ssid=”your router SSID”
      psk=”your router connection password”
      }
    5. Save the file & exit.
  15. Type the following at the prompt:
    1. wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf -Dwext &
    2. (The & means it is a background process – if you don’t do this, you end up stuck in the command).
    3. Once it comes up with text that makes it sound like you’ve successfully connected, type:
    4. dhclient wlan0
    5. (This gets your IP address)
  16. Type ‘ifconfig -a’ at the prompt. The bottom one should be your wifi adapter and you should be able to see an ip address. If you don’t, well, you probably got the ssid/password wrong in the conf file.
  17. You should now be on the Internet.

Update the operating system and get a decent WiFi interface

  1. At the prompt, type:
  2. apt-get update
  3. This will update the package database so you can update your operating system.
  4. Once that’s done, do:
  5. apt-get upgrade
  6. This will upgrade any module that needs it. It will take some time.
  7. Once that’s done, do:
  8. apt-get install wicd-curses
  9. Say yes to it using up the space on the disk. Thanks to the steps with gparted you will have lots of space to play with.
  10. wicd-curses is an interface to wireless networking that you can use to see what other WiFi networks there are in the area and gives you a handy interface to enter passwords, automatically connect etc.
  11. Do another ‘reboot’ and when the Pi comes back up, login as root again and do:
  12. shutdown -h 0
  13. This will shut down your Pi. Wait until it says “System Halted” and then pull the power. (It may just switch off by itself, but once you see that message, it’s safe to pull the plug.

Next time…

We will add the wpa_supplicant commands to rc.local to automatically start-up when we boot. And we will look at getting the Bluetooth keyboard/mouse all working nicely!

The Pi Pod

Aims of the Project

  • To become familiar with the Pi and the Linux operating systems available.
  • To have a fairly self-contained unit with:
    • The Pi
    • A screen
    • A keyboard
    • A mouse
    • WiFi.
  • To not blow the thing up before I’ve had it for five minutes!

Components of the Project

The Raspberry Pi itself!
Purchased from RS Components
Waited 4 months for it to be produced and delivered.
Apart from that, I cannot recommend them at all – their after-sales service is email only and they will refuse to speak to you on the phone regarding anything to do with the Pi.
Cost: £35 (I think!)
An 8GB micro SD-card with adapter
This was a bit of a risk, but forced on me by needing more space than the 2GB SD card I had lying around. As it turns out, this was a good solution.
Again, I had it lying around so it was free.
You should be able to pick one up for under £20, possibly a lot lower.
I recommend starting out with something larger than 2GB as you will need to install packages and never know if you’ll need more room.
A Rii Bluetooth mini-keyboard with in built touchpad
Neat little device this. Even came with a tiny-tiny bluetooth dongle, stored inside the actual keyboard itself. Very neat.
Got mine from eBay seller ‘cigbargain’.
Delivery would have been fairly quick if Royal Mail hadn’t screwed it up first!
Cost: £19.
A “Really Useful Box”
think mine was a 1.75 litre box.
Purchased from Hobbycraft in person.
Cost: £4 (approx)
A 3.5″ car rear-view parking camera screen
Purchased from eBay seller easybuying168
This little piece of kit comes with all the cables except the actual power cable. It’s supposed to be used inside a car so the power cable you get is supposed to be wired in to one of the tail lights. However, it is merely a 12V power supply and they are fairly cheap to get. You may even have an old one lying about from other gadgets like speakers, that kind of thing.
One of the best things about this is: it’s the only I’ve found that is foldable – it flips down so the screen is protected.
Cost: £21.
A non-slip mat
This came with the screen, but is very handy when the Pi is slipping all over the bloody place.
A Belkin WiFi Dongle
I just had this lying about from the good-ol-days of needing a dongle for WiFi. You should be able to get one for under £10, for example, on eBay.
A craft knife
I knew I’d need one. I already had it. Let the hacking commence!
A pack of cable ties
Yes, I know. Very Heath-Robinson. Still, they do come in handy and you can pick them up in stores like the Poundstretcher chain.
I can also recommend the Rapstrap if you want something a bit easier/flexible to work with.
Cost: £2.99 for a big ol’ pack, various colours and lengths.
A micro-USB charger
That’s the connector that’s really flat and small. It’s what powers the Pi. Do not go cheap – the better adapter you have, the better and more stable the power supply will be. You’ve probably already got one, as I did, if you’ve been through a few phones. Samsung use them extensively.

Next time…

I install the operating system. Well… Systems. Repeatedly.

Raspberry Pi – booting from USB

So! RPi post 3!
NOTE: This was pretty much an abject failure and is only included here for completion purposes (i.e. it was on the wrong blog and I wanted it moved!)

Objective
Get the Pi booting from a USB flash drive.

Why?
For reasons of speed and to allow more to be installed on the operating system.

Caveat
It’s impossible to boot from a USB flash drive directly. What you will be doing is to use the SD card as a ‘bios’ and then bootstrap it to jump to the flash drive.

Main reference
I decided not to try and do this all by myself. I tried the guide here:

but found that it didn’t give me enough information to do anything with.
I then found this guide from Andrew Ord in Somerset, UK:

http://andreword.co.uk/Applications/Raspberry_PI/Entries/2012/6/4_Preparing_the_destination_drive.html

It’s not completely accurate (or wasn’t when I ran through it on July 4/5 2012, so here’s an updated version.

My set-up

  • Raspberry Pi model B.
  • 2GB SD card (from MobyMemory, their own brand).
  • 16GB flash drive. (from Play.com)
  • USB keyboard.
  • Ethernet cable.
  • A 3.5″ car rear view camera screen. (You can use any monitor/screen that will connect)
  • Laptop running Windows 7 and the Putty terminal.

A – Preparation
Install the Debian Wheezy Beta distribution. This was done on Windows. You may find that the image writer doesn’t work. There are alternatives, but the most import thing is to get this done. You can’t go any further unless you do.

  1. Download from the link above.
  2. Download the newest version of win32 diskimager from  https://launchpad.net/win32-image-writer/+download
  3. Using it, write the image you downloaded to your SD card.
  4. Put the card in pi.
B – Devices and Partitions and Swaps, Oh My!
Sort out partitions and swaps and mount points. NB This will wipe your USB card
  1. Boot up the Pi with your flash drive and other cables plugged in. All the steps from here on (up to the networking step) were done over a Putty terminal.
  2. Log in as root with password root. (You’re going to sudo loads of stuff anyway, so logging in as root seems sensible).
  3. ls /dev and look for a device called ‘sda’. This is the whole USB flash drive. You are now going to sort it out so you can actually use it.
  4. fdisk /dev/sda
  5. Do p <enter> to see the partition table.
  6. Delete any existing partitions by doing d <enter> and picking the partition if necessary.
  7. Create new partitions:
  8. Do n <enter> p <enter> 1 <enter>
  9. Hit <enter> so it takes the default ‘start’ position.
  10. Choose an appropriate size for the main boot partition by following the instructions on that line. I think it’s something like size=+12GB or something. It says it on screen.
  11. Once you’re back to the fdisk console, do n <enter> p <enter> 2 <enter>.
  12. Use the default ‘start’ and ‘end’ positions (so that it automatically takes up the whole disk).
  13. This gives you two partitions. Exit from fdisk.
  14. At the prompt, type mkfs.ext4 /dev/sda1 (formats the main partition)
  15. Then do mkswap /dev/sda2 (which formats the swap drive).


Using the most recent Debian SD card image, I’ve booted up with the external drive attached to the second USB port

1.Logged in to debian with the pi/raspberry account
2.sudo su (switch to su mode without the root password)
3.ls /dev
(list all devices, depending on the type of drive)
Look for /dev/sda
Note your SD card will be listed as /dev/mmcblk0 (with partitions p1, p2, p3)
4.fdisk /dev/sda (obviously change this to match your HDD configuration)
5.p (print the partition table)
6.d <Enter> <number> (for each partition to delete them)
7.n <Enter> p <Enter> 1 <Enter> (Choose appropriate size for your main file area)
8.n <Enter> p <Enter> 2 <Enter> (make sure to have left yourself some swapspace)
9.You can set the boot flag if you want, but it isn’t needed since your booting from the SD card
10.mkfs.ext4 /dev/sda1 (format the first partition)
11.mkswap /dev/sda2 (format the swap space)

Now we need to prepare the installation onto this new drive ready to boot
1.mkdir /a (create a folder called a in the root, this is where we will build our new install)
2.mount /dev/sda1 /a (mount our new drive on /a)
3.apt-get update (update our package sources)
4.apt-get install debootstrap (install debootstrap)
5.debootstrap squeeze /a http://192.168.1.11:3142/ftp.debian.org/debian/
(do a minimal build onto our folder)
I use apt-cacher-ng on another machine, use http://ftp.debian.org/debian/

Once the packages have downloaded and installed we should have an extremely minimal debian install in /a
Let’s move across into this new environment and start to build it up to be able to operate on it’s own

Download the kernel file from http://www.bootc.net/raspberrypi/linux- … _armel.deb
(Note : You may find that the version has been updated since I wrote this note, please go to http://www.bootc.net/raspberrypi/ to find the latest file name if needed)

1. wget http://www.bootc.net/raspberrypi/linux- … _armel.deb
Copy the downloaded files, linux-image-3.2.18-rpi1+_5_armel.deb and linux-raspberrypi-3.1.9-18-arm.pkg.tar to your new area (/a/root/installation was where I put the files)

1.chroot /a (Change root to our new folder)
2.passwd <Enter> (follow instructions to set the new passwd for root on your new install)
3.apt-get update
4.apt-get install aptitude openssh-server locales ntp
5.dpkg-reconfigure locales

This should provide us with a remotely accessible platform once we get the kernel installed

Let’s get the kernel and modules installed
1.cd /root/installations
2.dpkg -i linux-image-3.2.18-rpi1+_5_armel.deb

The new kernel is now stored in /boot. In order to update your SD card to use this kernel you now need to do a little slight of hand

The file you need is in /boot called vmlinuz-3.2.18-rpi1+, and to use it we need to copy this file and rename it to kernel.img and store it on the SD card

When I applied the kernel update on my SD card, I did the following
1.mkdir /tempSD
2.mount /dev/mmcblk0p1 /tempSD
3.mv /tempSD/kernel.img /tempSD/kernel.img.original
4.cp /boot/vmlinuz-3.2.18-rpi1+ /tempSD/kernel.img
5.nano /tempSD/cmdline.txt
6.(modify the boot command by changing root=/dev/mmcblk0p2 to root=/dev/sda1)
7.CTRL+O,<Enter>,CTRL+X
8.umount /tempSD
9.rmdir /tempSD

These changes now mean you have the up to date kernel on the SD card and you’ve modified the boot commands to use your USB HDD as the root file system

Now do a reboot (reboot <Enter>) and let’s see what happens

The first thing you should notice is that it boots up fairly quickly. Always a good sign I think

The second is that you should be able to logon as root, using the password you set earlier

Now we need to make a few modifications to the configuration in order to automatically get the internet connection up and running

1.Log on to the PI
2.nano /etc/network/interfaces
3.Add the following lines to the file if they are not already present
auto eth0
iface eth0 inet dhcp
4.Before we forget, we need to turn on our swap if it hasn’t already
5.nano /etc/fstab
add the following line
none    /dev/sda2    swap    sw    0    0
6.exit
7.swapon /dev/sda2
8.To address a bug I keep finding with the network connection, edit the sysctl.conf file in /etc and include the line vm.min_free_kbytes=32768
9.Now you should be able to reboot and have networking available

XBMC time (incomplete text, still trying to make this bit work correctly)

Edit apt sources.list file to include a local mirror for debian-multimedia

When you try to do an apt-get update you will get an error due to the gpg key, use the following command and substitude the code returned in the error message in order to correct the problem
1. wget -q “http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x4874D3686E80C6B7” -O- | sudo apt-key add –

Now we can install xbmc using “apt-get install xbmc lxde”. File sizes are around 260Mb alone for XBMC, probably about the same for LXDE packages so be prepared to wait

1. dpkg-reconfigure x11-common
2. Choose Anybody