Raspberry Pi emulation

Taking a quick break from my PiPod project, which is essentially done, bar photos and videos of it actually working!

I’ve decided that having a “virtual Pi” would be handy so that I can mess about with the OS without worrying too much! Plus, it means I don’t have to carry my Pi around with me all the time if I just want to try some Python etc out.

Important note:
For Windows users, it seems that the recommended solution for emulating the Pi is to use qEmu on Windows. Take a look at this Sourceforge project: Raspberry Pi Emulation in Windows.

Virtual Box

Just so you know what you’re installing on your main computer… VirtualBox is a wonderful, free, virtual computer creator and viewer. There are a variety of platforms it works on. I’m actually using a version I already have on my Windows 7, 64-bit machine.

Emulating the Pi

I’m referencing this guide from the Southend Linux User Group to a certain extent. I know, random, but that’s what Google came up with!
I should say before we begin that I’ve tried both the ‘Lenny’ and ‘Sarge’ Debian installs available from virtualboxes.org and neither of them worked (Lenny stopped at “Select and install software”, Sarge gave a rather horrid “Kernel panic”) hence I now use the official Debian site download instead!
  1. Download VirtualBox for your platform.
  2. Download a net install ISO image from debian.org itself.I’ve chosen the i386 small CD in the top-left area. 191MB to download…
  3. Create a new machine on VirtualBox, take the defaults, choose to create a new disk for it.
  4. Start the machine up.
    1. You get the first-time start-up dialogue.
    2. Choose to boot from the ISO image you downloaded.
    3. Choose your language/country etc. Let it run for a bit.
    4. Now, this installer looks very similar to the one that stopped on the Select and Install Software dialog, so hopefully it’ll work better than that one!
    5. Take the defaults all the way through, no need to be picky unless you want to. Set the root password, create a new account, set that password…
    6. Use the guided partitioning mode at the top.
    7. Choose to put all the files on one partition and write the changes to the disks.
  5. Off it goes, partitioning away, installing the ‘base system’. This is where it starts extracting the downloaded packages and going off to download and install the rest of the OS.
  6. This is going to take a while, I can tell!
  7. Now we’re onto ‘Select and Install Software’… Holding my breath…Ooooh. It’s gone to 12% and still running… This could be good. Yep, “Retrieving file” counter still running… Goooood.
  8. Choose the options you like. I just left it as a ‘GUI’ and the recommended/default options.
  9. Some time later…
    Yes, I think I will install the GRUB boot loader. If only because I saw that the absence of this affected installation attempt number 2.
  10. Yay! Installation is complete. I’ll believe it when I see it 😉
  11. Now it’s running clean-up… Oh, I hope it moves on from 65% soon, otherwise… Ah. There we go.
  12. Rebooting automatically.
  13. Hmmm. Get the boot choice again… does that mean it’s going to start the install again? God, I hope not!
  14. I have a black screen… some kind of progress swirly…. Ah! Desktop!
  15. Go ahead and login. Don’t use root, use the ‘other’ account you created.
  16. Love the retro rocket-and-planet motif. Taking a while for anything to happen… Probably just a first-login thing.
  17. Hmmm. Okay… That’s a crappy GUI… no start bar or icon dock or anything… I can soon install something new of course. Oh, I see… there’s a menu bar at the top. Kind of merges into the VirtualBox menu. Some kind of message about Guest Additions came up but I missed it.
  18. Right, let’s see what we’ve got and if we can carry on with the emulation stuff…

Terminal Time

  1. From the Applications->Accessories menu, start up a terminal.
  2. You should get a shell prompt with your ‘other’ username attached to it.
  3. Right, install the qEmu stuff.
    1. su root (use the password you specified during install)I’m doing this because I can’t sudo… because I’m not in that group of users, which is unhelpful.
    2. apt-get update(i.e. update the package database).
    3. apt-get install qemu
      (Go and get and install the qemu package)
    4. Now, qemu is installed. That page I am referencing as a guide (the Southend one) makes it a lot more complicated than it needs to be!
  4. Now, you need to cd ~<other username> to go into the home folder of your ‘other’ user. We need to create a folder to mess around in and store kernels and disk images.
  5. mkdir pi_emu
  6. cd pi_emu
  7. wget xecdesign.com/downloads/linux-qemu/kernel-qemu
  8. This will get the qEmu kernel, which makes everything run. I hope.
  9. You now need to get the current Linux image for the Pi that is recommended by the RPi foundation. The one I am using is the first release of Raspbian Wheezy. But, always check for the latest one at their downloads page. Get the URL of the release and do:
  10. wget <url>
  11. This is NOT a small file – it’s about 440M, so go and get a cup of coffee.
This takes around 10 minutes to install at work, and we’re on a T1 connection. The reason it’s so slow is probably because of where the file is coming from rather than the connection itself.
And we’re back…
  1. Unzip the file your downloaded (you will have to change this filename to whatever is now in your folder.
    1. To get a list of files, the Linux command is ‘ls’.
    2. unzip yyyy-mm-dd-whatever.zip
      (to unzip it)
    3. This takes a while as well as it needs to expand the 440M file to a roughly 2GB file. Gawd bless file compression.
    4. Now, if you are logged in as root (and I was, so you should be!), you will need to make sure that the files in that folder are ‘owned’ by your other user, which will shall call ‘otheruser’. If you don’t do this, the .img file (and therefore the Virtual Raspberry Pi) will be ‘readonly’ when you run it using your otheruser account.
      1. chown otheruser *
      2. chown otheruser *.*
      3. exit
    5. You should now be back at a prompt that looks like
  2. Now, going to use a modified version of the command from the Southend site to start it up. Here goes:
    1. qemu-system-arm -kernel kernel-qemu -cpu arm1136-r2 -M versatilepb -no-reboot -append “root=/dev/sda2 panic=1” -hda 2012-07-15-wheezy-raspbian.img
    2. The last argument there is the .img file that you decompressed just now.
    3. Apparently, you can give it more memory by adding the “-m 512” argument to the end, but I just want it started up for now.
    4. I’m getting a few ‘readonly file system errors’, so it’s possible I might have to change permissions on the .img file before it works, but let’s just see…
    5. Not bad so far. Oooh! The usual Raspberry Pi first start-up screen! Yay!
    6. Choose the ‘update raspi-config’ option at the bottom just so you are up-to-date.
    7. Now re-run that utility:
      sudo raspi-config
    8. Choose the SSH server option and enable it. This will let you open a remote terminal to the Pi if you need to. Choose OK afterwards. Then choose ‘Finish’.
    9. Now we do ‘startx’
It’s interesting… the desktop comes up (although it’s blue, rather than raspberry-coloured… Odd). It’s a problem others have come up against.

For Windows users, it seems that the recommended solution is now: Raspberry Pi Emulation in Windows.

Never mind, it was an interesting practical experiment, which sort-of works apart from the colour issue. Non-Windows users may find these instructions still useful, so I will publish this post anyway!

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:
      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.