Tuesday, 5 June 2012

Getting started with a Raspberry Pi, and setting up X11 display export

Getting started with my new Raspberry Pi Model B has proven to be pretty easy. It seems that my grasp of Linux is more than enough to be useful, and there are plenty of easy-to-find notes on setting various things up on a Raspberry Pi. This post covers a few things I've done with my Pi right out of the box. So far, I don't think anything I've done is original. This is meant to be a record for me as much as anything else.

I had some 4Gb SD cards from my camera, so having unpacked my Pi from its little carboard box and antistatic bag, the next job was to put a Linux image onto the SD card, so that I could boot it up and have a play. Starting at http://www.raspberrypi.org/downloads, I grabbed a copy of both Win32DiskImager and the Debian “squeeze” SD card image (debian6-19-04-2012.zip) and followed the instructions on that page to set up the SD card. That went smoothly.

I popped the card into the Pi, attached all the cables:
  • Power from a Blackberry charger (5V 0.7A Model PSM04R-0500CHW1(M), RIM Part Number HDW-17957-003)
  • HDMI cable (can't remember where from, normally connected to an XBox) plugged into my Dell 24" flat panel monitor
  • Microsoft Wired Keyboard 600 (£8.69 from Amazon, bought specifically for the Pi as my backlit Logitech G15 was unstable to the point of being unusable, presumably because it draws too much power)
  • Logitech G5 mouse
  • Wired ethernet cable
...and plugged in the power. It booted up fine once I had swapped to the less demanding basic MS keyboard. Following the excellent Quick Start Guide, I could log in at the command prompt, and spent a pleasant few minutes tinkering around with a Hello World C program provided (among other examples) in /opt/vc/src/hello_pi/hello_world. It's been a while since I compiled a C program using a makefile, and it took me a few minutes to figure out (by trial and error) that to build the program provided, you just type:
make
...as everthing else had been set up already. You then run the hello_world.bin binary this creates by typing
./hello_world.bin
For fun and as to begin building some familiarity, I modified /opt/vc/src/hello_pi/hello_world/world.c in vi (a text editor that I regret I know well, and therefore get a lot of value from, but would NOT particularly recommend to new users as their first Unix text editor!) to add a 10-iteration loop, and saved it as world10.c, so it becomes:
#include <stdio.h>

int main(void)
{
   int i;
   for (i=1;i<=10;i++)
   {
     printf("Hello world!\n");
   }
   return 0;
}
To get this to compile, I edited the Makefile in the same directory, to contain the following:
OBJS=world10.o
BIN=hello_world10.bin

include ../Makefile.include

...and the ran the make command again. That done, running ./hello_world10.bin output 10 Hello World! messages as expected:

This done, I ran started the LXDE desktop by running:
startx
 The GUI desktop is a bit slow and can be briefly unresponsive when you do something, as discussed fairly widely, and I'm looking forward to a more optimized version. But it's completely useable if you're willing to be a little patient occasionally. I'm not familiar with LXDE or Linux desktops in general, but found my way around easily enough. It's been designed to be a little familiar to Windows users.

My next task was to have a look at how much disk space I had to play with using:
df -ha
The answer was not much. Only a few hundred MB were free across the varions partitions on the SD card, since it was a 2Gb image, and most of that is needed for the OS and swap space. I clearly needed to do something to make the other ~2Gb on my SD card available within Debian on the Pi.

I searched a bit for how to do this, and quickly came across several mentions of gparted. Without reading them properly (I'm as guilty of this sometimes as anyone else) I tried
man gparted
 ...which brought up a man page. However, it wasn't very obvious from that what to do, and after playing around with running it at the command prompt for a while (unsuccessfully) I felt I was getting nowhere and went back to read the search results properly. Turns out this isn't how you use it at all!

Liam Fraser's excellent video tutorial at http://www.youtube.com/watch?v=DztSRaFyaVE shows you what to do in his laid-back but deceptively to-the-point style, and it's spot on. Thanks Liam! No point duplicating his guide here, but in outline, you need to use a Windows (or Linux) PC with a CD Writer and an SD card reader, a program for burning .iso images to CD (there's plenty available for free, and your Windows or Linux OS might even have one already), a blank CD, and you need to download a copy of GParted Live CD. You burn the image to the CD on your PC, and then reboot the PC using the CD as the boot disk. Next, with the Raspberry Pi's SD card in your PC's SD card reader, follow the simple steps in the GParted program (it has a nice simple Linux GUI) to move the swap partition on your Raspberry Pi's SD card to the end of the disk, and then expand the ext4 partition (mounted at /dev/sbd2) to include all the unallocated space on your SD card. When you're done, you can shut down the PC, which helpfully ejects the CD so that it doesn't boot back into GParted Live CD next time it powers up - nice touch. Put the SD card back into the Raspberry Pi, power it up, and log in, and this time
df -ha
shows a nice healthly root partition with plenty of space for you to play with:
One final thing I wanted to be able to do was connect to the Pi from my Windows PC using the tools I'm familiar with for this sort of thing: WinSCP, PuTTY and Xming (for running programs with a GUI remotely on the Pi, but displaying the program window on my Windows machine). I already had WinSCP, PuTTY and Xming installed on my desktop PC.
Tip: by default, Xming doesn't let you connect to it from a remote PC, but you can fix this either by putting the IP address of the remote machine in C:\Program Files (x86)\Xming\X0.hosts, under the existing entry for localhost, or alternatively you can edit the startup command to include the parameter -ac:

 To be able to connect to the Pi from your Windows PC over the network, you need to do two things:
  1. Enable SSH on the Pi
  2. Find out the IP address of your Pi on your network, so you know how to connect to it in WinSCP/PuTTY etc.
To enable SSH on the Pi, I followed the one-line instruction at  http://elinux.org/R-Pi_Troubleshooting#Cannot_ssh_in_to_Pi, which is to simply run this command:
sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc
...and then reboot the Pi.

To find out the IP address of your Pi, this time, on your network, run the command:
ifconfig
This should produce output something like the following, and the current IP address is pretty obvious within it, labeled inet addr:
Armed with SSH enabled and knowing the IP address, you can fire up PuTTY or WinSCP, or the terminal/remote filesystem tool you prefer, connect to your Pi over the network, and share files or run commands remotely. This interface is really familiar to me, so I expect I'll use it a lot in future posts!

Finally, setting up remote display is a nice way to complete the connection between your PC and your Pi. First, run Xming (or the X-server of your choice) on your PC. Remember to enable connections from your Pi to your X-Server in the X-Server config, so that connections aren't refused when you start a program on the Pi which wants to display something back on your PC. See the tip above if you're using Xming.

Next, you may need to enable X11 forwarding in your terminal client. In PuTTY, this is found burried away somewhat in the settings you are shown before you connect. Run PuTTY, and in the PuTTY Configuration window, in the Category tree on the left, expand Connection > SSH, and select the X11 config page. Then, in the config settings on the right, check the checkbox to enable X11 forwarding:

That done, scroll right back to the top of the Category tree on the left, and select Session again. Enter the IP address of your Pi in the Host Name (or IP address) textbox, leave the port set to 22, and type in the same IP address in the Saved Sessions textbox, before clicking Save. This saves both the IP address for future use, and the X11 forwarding setting you changed, so you don't need to enable it every time you connect to your Pi via PuTTY:
You could just call the session Raspberry Pi, but it will only work so long as your Pi has that same IP address on your network - it seems to keep the same IP address on mine, but that's only a function of my network gateway/router's DHCP setup and my particular usage pattern over the last couple of days. It probably isn't to be relied on.

Now click Open at the bottom (or double-click your saved session in the list), and you should be shown a login prompt - log in using the same username and password you would on the Raspberry Pi itself:

Next, find out what IP address your PC currently has. From Windows, start a command prompt (Start > Run > type "cmd" and press Enter), and then run the command ipconfig. On a Linux machine, it's ifconfig again. Look for the IP address under something like "Ethernet adapter Local Area Connection:" (on Windows), or "eth0" and "inet addr" on Linux.

Then, over on your PuTTY session connected to the Raspberry Pi, type the command
export DISPLAY=your-PCs-IP-address:0.0
So, for example, of your PC is on IP address 192.168.1.7, this would be:
export DISPLAY=192.168.1.7:0.0
Note that the DISPLAY environment variable must be in all-uppercase. I tend to run this command just before I need it in each PuTTY terminal session. You could probably put it in some sort of .profile or .rc script (I haven't explored this yet, so I'm not sure what the best place to put it would be), but it would rely on your Windows PC always having the same IP address, and you always wanting to display any X-based windows on your PC, which you probably don't want to do. I don't think this is helpful.

If you've followed all the steps above, you should be able to test running a simple program on your Pi (through the PuTTY terminal window) which sends its display to your PC's X-Server to appear on your Windows desktop, such as this:
xeyes

Done! Programs running on your Raspberry Pi can now display GUI windows on your PC over the network. Any suggestions for programs on the Pi where this would be useful?