Raspberry Pi Notes

Introduction

In case you really don't know, and that is perfectly possible because not everyone is fascinated with computer technology in the same way as I am, the Raspberry Pi is a little DIY computer on a small circuit board. The Raspberry Pi foundation started as a way of creating educational IT projects, based around the ARM 600 processor, which powers most of the mobile phones in use today. These clever boffins stuck the ARM 600 chip on a little circuit board and attached a couple of USB ports, an HDMI video port and a standard power socket, and sold the raw thing for about twenty quid.

Wikipedia Raspberry Pi

However, a processor and a USB port really is NOT a functioning computer. It needs a bit more "stuff." It needs some storage. Your computer has a hard drive. The "Pi" has a slot for a standard SD RAM card like you put in your digital camera. That is the Pi's hard drive. Amazingly you can get SD cards that can hold up to 64 GIGA bytes of storage. I remember replacing the hard drive in an old PC with a 20 Megabyte hard drive that was marketed as "all the hard drive space you will ever need." These days that would be just about enough for one photograph.

So armed (ha ha) with a memory chip and a Raspberry Pi, a broken USB keyboard liberated from a skip in the office, and a USB mouse borrowed from another computer, I was ready to set up the Raspberry Pi. That is what this page is all about.

 

One of the interesting possibilities, since the "hard drive" is just the SD RAM card, is to set up various different Raspberry Pis on different cards. Then, you switch off, change the card, and switch on, and hey presto! You have an entirely different type of computer with an entirely different suite of software on it. I was very tickled with the idea of loading RISC OS on one card, and bringing my beloved old Acorn A3000 back from the dead. I wonder if any of my old programs would actually run. I have a suspicion that they very probably would. However, I haven't actually done that yet. There would be an interesting poetic symmetry in this, since Acorn invented the ARM processor (Acorn Risc Machine. Risc = Reduced Instruction Set Chip) in the first place.

So let me describe how I have set up my Pi. The outline is like this:

Order a Raspberry Pi online from Farnell Element 14
Buy an SD RAM card from Currys PC World
Download the Operating System of my choice to another computer
Unzip it and install it on an SD RAM card
Plug the SD RAM card into the Pi
Plug in a monitor, mouse, keyboard, and network cable, with access to the internet
Switch it on

At this point I had a functioning computer, with almost no useful programs at all. It was the most inelegant collection of wires and circuits you could possibly imagine, but it worked first time! How amazing is that? The trigger for getting this thing was to create "Bee-cam" for people to watch the company honey bees. That is still the plan but there is a bit of work to do yet. It would be so much easier to buy something ready-made but so much less fun. Anyhow, these are the steps I have followed, in a lot more detail, to get a web cam connected to a battery-powered twenty quid computer.


Get the Operating System onto the SD RAM Card

I have access to a few other computers. I decided to download the Operating System image 'Raspbian' to my Macbook. Raspbian is the recommended beginners route. It is FREE, and I thought that if it doesn't work I can do it again with something else. Raspbian is a version of Linux. Linux is getting used more and more widely. I could have done it via a PC probably more easily, but it worked very easily enough.

1. Download and run some software to format the SD RAM card
This link will get you the SD RAM formatter. SD Formatter

You can follow the instructions to format the SD card, but be a bit careful. Apparently it is theoretically possible to format your hard drive by accident. Get past this without destroying your own computer and the rest is less stressful.

2. Download and unzip the Operating System image
Downloading the Raspbian image was easy enough. There are plenty of websites that will point you to the right place for the Raspbian Operating System. I chose to download the .img file. I would advise moving the downloaded file from 'Downloads' to somewhere easy to find on the Macbook, like 'Documents' and renaming it something like 'raspbian.img' because you are going to have to type in a command line. It actually comes down with a great long name with a time and date stamp on it. In Mac world, to execute a command, you have to get to the Terminal, which is in Finder-Applications-Utilities, unless you have moved it by accident. As I said, it is probably easier on a PC.

Working backwards, on the Macbook, I had to execute the command 'dd' with some extra parameters. For this to work, the destination disc has to 'unmounted' which is slightly alien to PC users. Unmounting a disc involves executing a 'diskutil' command. You need to know which disc to unmount with 'diskutil', which involves executing a different 'diskutil' command to identify the right disk.

So this was my sequence of commands in Terminal on the MacBook, after sticking the SD Ram card in the slot in the Macbook. The exact disk to unmount and execute the dd command on may not be disk4 in your system.

The dd command decompresses the operating system onto the SD RAM card, and takes a little while to run. Prefixing it with 'sudo' means it is run with administrator rights. Get used to 'sudo'. It comes in handy later. 'if' means input file and 'of' means output file. Hell alone knows what 'bs' means.

Plug it all in

At this point I plugged the following into the Raspberry Pi:

SD RAM Card
Network cable connected to the WiFi router
USB Keyboard
HDMI cable connected to the telly

Finally (drum roll) the 5V micro USB power supply; actually my phone charger. How is it possible that this is so exciting? A few flashing lights and some rainbow colours on the screen for a second, and YAY!!!! Houston, we have liftoff. User name = pi. Password = raspberry.


Setting Up

It is not very inspiring, after getting used to the fancypants tablets and smartphones and nifty flaptops, to see a flashing cursor on a black background, waiting for keyboard input, unless you are the kind of person who is happier sliding down the local snowy hill on a home-made tea-tray based toboggan than a commercially sold purpose built plastic moulded sledge. I made my own sledge. 'Nuff said?

Start by maximising the memory available to the Raspberry Pi. You can do that from the first menu that pops up. Then change the password.

The 'stuff' that installs itself on the Pi from the internet is sometimes updated. It is always a good idea to keep it up to date, so one of the first tasks is to update it. These are the commands to do this:

Now what on earth are we going to do? Well, there is a desktop environment, but that is not very exciting after Windows 7/8 and Mac OS X. The main use of the Pi is going to be with no mouse, no keyboard, and no screen attached, so I am not too fussed about what the desktop looks like. What I really want, to start with, is

To be able to do stuff on the Raspberry Pi remotely, without trailing wires all over the living room floor.
To have it do things with the webcam.

Remote Access

The first of these is done from the Macbook with a command called 'ssh'. From a PC you can use a bit of free terminal software called PuTTY that you can download and run easily. You just need the IP address of the Pi on your network. Ditch the screen and keyboard, and log in from SSH or PuTTY on another computer. However, to avoid having it connected to the router box with wires it is a great idea to have a WiFi USB dongle in the Pi.

SSH needs to be running on the Pi, and by default, it is. It is a service, and there is a command to display the running services, and a command to turn them off, and a command to turn them on, but in the case of SSH it starts out in the state we need.

After I got WiFi running on the Raspberry Pi I could connect to it remotely, but let me explain the WiFi setup on the Raspberry Pi first.

WiFi

I set up the WiFi with a command line. Turns out to be really quite easy. You just have to understand a configuration file enough, and edit it a little bit. It is easier to do it through the Raspbian desktop where there is an icon for launching the WiFi configurations, EXCEPT you need a keyboard in one USB port, a mouse in the other, and then the WiFi dongle in .... er.... Ooops - only two USB sockets. Need to go and buy a USB Hub? Do NOT bother. Just configure the WiFi via the command line. With your keyboard and screen plugged into the Raspberry you can edit the network configuration file with the following command.

Unfortunately I discovered how easy this is after I had bought the powered USB hub, plugged lots of things in, including the WiFi USB dongle, USB keyboard, and USB mouse. Here is my configuration file for the WiFi. Note - Network name and Password changed, but you get the gist. The gateway IP is usually the IP of the wireless router. Yours may be different, but mine is quite standard.

auto lo

# ethernet cable settings
iface lo inet loopback
# fixed IP address on local network
iface eth0 inet static
address 192.168.2.200
netmask 255.255.255.0
gateway 192.168.0.1

# wireless LAN settings
allow-hotplug wlan0
# fixed IP address on local network
iface wlan0 inet static
address 192.168.0.98
netmask 255.255.255.0
gateway 192.168.0.1
wpa-passphrase WiFiPassword
wpa-ssid WiFiName

Restart the Raspberry Pi to get the network configuration file to take effect.

So now we only need a power supply to the Pi, and the USB WiFi dongle, and we can control it from another workstation. The IP address of the Pi would normally change every time you turn it on, except the configuration file sets a static, fixed, IP address so I always know where to get to my Pi on my own network.


Connect Remotely via WiFi

On the Macbook, launch the Terminal application in Applications-Utilities. On a PC you can use PuTTY.

The Macbook command to connect to the Raspberry Pi with Terminal is:

I know that is the right IP address because I set it to be that in the interfaces configuration file. The Raspberry Pi responds with:

pi@192.168.0.98's password:

The default password was raspberry but I changed it when doing the setup earlier.


Where am I going with this?

Well, I am planning to get the WiFi-enabled Raspberry Pi talking to some kind of internet hub, so that the web cam can stream video to the internet. I do not actually need the WiFi dongle for configuring the Pi, but it is very convenient. The whole lot is going to be portable and battery powered. Somehow.

Just for now, I want to set up the Raspberry Pi with a webcam, and stream the output to the internet from my home network. To do this I will need a webcam and some software to control it. The network wireless router will need some reconfiguring too. Let us look at these two steps separately.


Webcam Setup

I went and bought an inexpensive webcam online. What I really wanted was one which used very little power, because I want to be able to run the webcam from the Raspberry Pi using just one power source. It was a Logitech C300 and it works without any messing about, and seems to need no exta power.

Compatible Webcams

The Power Monkey iPod charger I have gives enough power to fire up the Raspberry Pi, and the Logitech webcam AND the Wi Fi dongle, and will keep all three running for about an hour. If the camera had needed more power I would have had to plug in a powered USB hub with another power supply for that, and a cable connecting the hub to the Pi and the webcam plugged into the USB hub, which would have been very messy.

Motion

Motion is free software for Linux machines that will control most webcams. It is easy to get on the Raspberry Pi, especially if the Pi is wirelessly connected with the Wi Fi dongle to the internet via the home wireless network. Log on to the Pi and execute this command Like most software in the Raspbian Linux operating system, it is controlled with a configuration file. This file can be viewed and edited with the command One of the things recommended in other fora is to edit the configuration file so that motion starts with a daemon. Do NOT do this. A daemon is a program that runs by itself and starts when the computer starts. The Pi does not have a battery-backed clock, and as a result the video and picture files that motion is going to create will be given a name with a default time stamp if the Pi is not connected to the internet. This is going to cause a problem. Files will be over-written when the Pi is started up a second time, when I get it home to look at what I captured. Just imagine collecting a nice movie file out and about somewhere, and the next time you switch on the Pi, the daemon kicks in and starts recording a movie with exactly the same as your movie. I have done this. It is frustrating.

As if that were not enough of a challenge, there is another one. By default, motion stores its files in a folder in the /tmp directory. What might not be obvious to a beginner, is that the /tmp directory is a temporary folder that is emptied when the Pi starts up. Anyway - there is a solution to both of these, but we will come back to that.

Motion allows you to set the width and height of the output video, and whether to save 'normal' JPG snapshot photos instead, or at the same time. The timelapse settings permit photos every N seconds, and the movie settings permit setting the frame rate for movies. What is not obvious is setting timelapse photos to be captured more often than one per second. N must be an integer.

However, one thing at a time. I decided that I would have a script that moves files from the default folder to another before motionstarts up. This would permit me to take the Raspberry Pi out, turn it on, grab some pictures or video, turn it off, take it home, and tuen it on again without risking the pictures I just recorded being overwritten.

But it wants to be a bit smarter than that. I want a whole cascade of folders, so files get moved from save8 to save 9, then save 7 to save8, then save6 to save7, etc, all the way to save1 to save2, before motion starts up recording to the (now empty) save1. So we have the following things to do:

Change the destination for motion files
Create a startup script to move files to another folder before motion starts
Find a way to view the output from motion on another computer

Changing the output destination
Edit the motion.conf file with

and edit this line to something like:

output_destination="/files/save1"

The thing is, this is not going to work by itself, because (a) that folder does not exist, and (b) even if you create it motion does not have rights to write to it. You need to create the folder and set the permissions

Good, now motion can record to that folder, which is /files/save1 in the above example. It is the work of mere moments to create all the folders we need.

The chmod command with the -R switch means it is applied recursively to all subfolders.

So now we want some kind of script to move all the previously records files about and then launch motion.

This is going to be in two bits. There is a file called /etc/rc.local which you can edit. By now you might be able to guess the command.

On the tail end of it you can put a line to call the a script file which we will describe in a moment, and then launch motion. My rc.local looks like this (just showing the non-comment bits):

#!/bin/sh -e
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

/home/pi/bin/move_motionfiles
/etc/motion start

exit 0

We need to create that file 'move_motionfiles' and to make it executable. I chose that name, by the way. You could call it anything you want when you create it.

My move_motionfiles looks like this now:

#!/bin/bash
# Script to move files

sudo find /files/save9 -type f -name "*" -delete &
sudo find /files/save8 -type f -name "*" -exec mv '{}' /files/save9 \;
sudo find /files/save7 -type f -name "*" -exec mv '{}' /files/save8 \;
sudo find /files/save6 -type f -name "*" -exec mv '{}' /files/save7 \;
sudo find /files/save5 -type f -name "*" -exec mv '{}' /files/save6 \;
sudo find /files/save4 -type f -name "*" -exec mv '{}' /files/save5 \;
sudo find /files/save3 -type f -name "*" -exec mv '{}' /files/save4 \;
sudo find /files/save2 -type f -name "*" -exec mv '{}' /files/save3 \;
sudo find /files/save1 -type f -name "*" -exec mv '{}' /files/save2 \;

You can see what is going on here without knowing the details of the find command. In theory, there is a simpler command mv which could be used. However, with mv, in Linux the wildcard is expanded out to be replaced with all the file names it finds. Motion can create a LOT of files. If there are zillions, then the command simply fails with some sort of error message about the command being too long. mv /files/save8/*.jpg /files/save9 is really not too long, unless that asterisk is replaced with forty thousand file names. So we have to use find instead.

The move_motionfiles file needs to be made executable. In DOS anything called .exe or .bat or .cmd is executable. In Linux you have to set that as a specific property of the file itself.


Summary of What We Have So Far

In theory, if all this stuff is set up properly, we have a WiFi dongle and a webcam plugged into the Raspberry Pi. When the thing is switched on all the files that motion has saved previously are moved in a series of steps to archive folders, and then motion starts up and the little green light on the front of the camera comes on. That is controlled by the rc.local script which calls the move_motionfiles script.

Exactly what motion is going to do at the point depends on what is in the motion.conf configuration file.

In theory, the Raspberry Pi has a static IP address on our local network, and in theory we can log on to it over the wireless network with some terminal software like Terminal or PuTTY.

So assuming all this is working, we can switch off the webcam and examine what it has been collecting. To switch it off we log on the the Raspberry Pi and execute this command:

The little green light goes off, and we can look at whatever has been captured and saved, with: With any luck there are lots of files there, that motion has captured. What type of files and how many rather depends on what we configure motion to do.

Splicing JPG Files Together to Make a Movie - Mencoder

Motion can record video directly to the designated save folder (/files/save1) in the above example. We will come back to this later. Right now, let us configure motion to record one JPG picture every 5 seconds, and then join all these together into a timelapse video. Yes, yes, I know motion can record timelapse video directly, but only if the interval between frames is one second or more. It has to be an integer. We are just playing about.

So configure motion by editing its configuration file and restarting it.

These are the lines we want in the motion.conf file:

snapshot_interval = 5
output_normal = off

Assuming we have collected some .jpg files to play with, we need another bit of software to stitch them all together into a movie. I got something called mencoder. To be honest I can't remember doing it, but I guess I typed:

The syntax to use mencoder is sufficiently long that I save this command in another file and simply execute it when needed.

The snapshot pictures are one every 5 seconds, and the output video is 25 frames per second, so the final video should go at 125 times real life. In theory, we now have a file called timelapse2.avi in the current folder on the Raspberry Pi. So how are we going to look at it?

Apple OS X comes with a command called scp, standing for Secure CoPy. It will work on the Raspberry Pi, too. I don't quite know how to copy a file from the Raspberry Pi to a Windows machine but it can't be rocket science. However, I do know that scp will not copy to a Windows computer without some sort of share being mounted, or some other utility being installed (Samba?) but I suspect you could use a part of PuTTY. Some homework for you. Anyhow, logout of the Raspberry Pi terminal session and execute something like this in Terminal on the Macbook. It will request the password for the pi user.


This is for the source file (timelapse2.avi) in the bin folder in the pi user's home folder, copied to the Documents folder as timelapse.avi.

Is there More?!

Just when you thought we were finished, we come across another exciting hurdle. The Macbook has no idea what to do with an .avi file. If you don't already have Flip4Mac you can download and install it for free.

Alternatively you can convert it to something the Macbook can use, with Wontube Video Converter. This is incredibly easy to use and absolutely brilliant.

Even more alternatively you could be using a PC which will have no problem showing you a .avi file, because pretty certainly it will download the necessary codec in the background if it doesn't already have it.


To be continued