First bake of your Raspberry Pi

22 Sep

Introduction

This is a short tutorial on how to bring up your Raspberry Pi for the first time and make it run something simple and fun.

In order to know that we accomplished what we want to, lets set the goal first. At the end of this short tutorial, we should be able to

  • Boot Raspberry Pi using an OS image (server grade, not desktop grade)
  • Run a webserver and a light blogging software on Raspberry Pi
  • Run a few automated scripts that collects a few simple statistics on the system and present this information as regular blog post

What we need

In order to accomplish our simple goal, we need the following bill of materials

  • Wiring and powering
    • Raspberry Pi device
    • A MicroUSB power supply (The minimum recommended power is 700mA at 5v. Most of the current day devices should support that. I personally use one of the power supplies that came with either of these devices – HTC Android phone, Asus Nexus 7 Android tablet or LG Nexus 4 Andriod phone. In a nutshell – you may not have to buy a power supply, you can use a redundant one that is eating dust around.)
    • A HDMI-HDMI cable (I run my Raspberry Pi without a monitor. I temporarily connect it to my TV during first builds, to do any inital configuration. Rest of the time, I connect to the device using SSH sessions from other computers.)
    • A USB keyboard. Please use a very basic keyboard that doesn’t draw too much power. Finding one should be fairly easy.
    • A class-4 or better SD Card of size 4GB or more.
    • A network cable
  • Display and connectivity
    • A monitor or TV that can take HDMI input. If you have a monitor that supports DVI only, you may have to use a HDMI-DVI cable instead.
    • A router or switch port on your home or office network to connect the Raspberry Pi to. I recommend a DHCP enabled network port for ease of use
    • A computer with network connection and SD card slot to download the OS and build your SD card.

Getting to know your Pi

At this time, I strongly recommend going thru this quick start guide, so that you are familiar with the device’s ports and connections.

Installing the OS on SD Card

I would normally do it the old fashioned way – by downloading the Raspbian Wheezy OS image and keeping it for multiple installations. I use my Mac or Linux system to prepare the SD Card and dumping the OS image using dd command. You need to be very careful and shouldn’t overwrite the primary hard disk on your system while taking this approach.

If you prefer to do interactive installations, download NOOB software from here. The NOOB software is interactive and offers you a few choices while guiding thru the installation process.

The rest of the tutorial is based on Raspbian Wheezy OS. If you prefer to install any other OS, the instructions might slightly change.

Booting the Pi

  • Connect the HDMI cable to monitor or TV
  • Insert the USB keybaord into one of the USB ports
  • Insert the network cable and connect the other end to the switch port or router.
  • As a last step, insert the micro USB power supply. Note that there is no additional power switch to Pi. Once you insert the power cable, it starts booting.

Once the system boots to a login prompt, login with the default username and password ( pi / raspberry ). Check if you are connected to Internet. If you see any issues, you might want to refer to the Raspberry Pi Forum.

Scenario

Here is a very simple scenario we are envisioning to accomplish on this Raspberry Pi.

There are three users setup on the system: boat, pi and tiger. The boat user is the one that collects the metrics and reports. The metrics collected are:

  • How long the system is up, how many users are currently on the system and the load on the system
  • How many pi users are on the system
  • How many tiger users are on the system

The metrics are collected by the boat users once in about 10 minutes and each collection is reported as a blog post.

Additional Software

For accomplishing our scenario, we need

  • A webserver running on the system: Let us use Apache as our webserver. If you prefer, there are other choices like Nginx or Lightttpd. However, this tutorial covers only apache2
  • A blogging software: Blosxom is one of the simplest blogging software around. It works on flat files (no DB installation required) and the blogging process is very simple – add a new post in the form of a new file that has the text either in text or HTML format.

Blosxom works by reading files in a given directory and displaying those files as a blog. Each file is treated as a separate blog entry. The first line of the file is treated as title of the blog post. The timestamp of the file is attributed to the blog post as publish time.

This approach of Blosxom fits our bill – periodically check a few metrics on the system and report these metrics. All we need to do is to collect all the metrics in one file and the file gets displayed as a blog post by Blosxom.

Updating/Installing the packages

It is a good practice to update the lists and upgrade the packages. Use the following commands to udpate the packge lists and then upgrade the packages.

sudo apt-get update
sudo apt-get upgrade

Once you are done, your Raspberry Pi should be running the latest version of the packages installed on the system. Now, I would recommend installing Apache webserver.

sudo apt-get install apache2

Once you install apache2, make sure that you have Apache up and running. Execute the following command and then visit the Raspberry Pi device using a browser (e.g. 192.168.0.109).

sudo service apache2 start

Now you should be greeted with the popular and familiar “It works!” greeting.

Configuring the rest of it

Now, let us create the users boat and tiger.

sudo useradd -m -U -d /home/boat boat
sudo useradd -m -U -d /home/tiger tiger
sudo passwd boat
sudo passwd tiger

Let us now configure the directory for our Blosxom blog and get it ready. For simplicity, I suggest we make this directory to be owned by the boat user.

sudo mkdir /var/www/LifeOfPi
sudo chown -R boat.boat /var/www/LifeOfPi

Once you reach this point, lets install and configure the blogging software – Blosxom. Blosxom is downloadable from here. You must download the file to your desktop and then copy it to the Raspberry Pi (using SCP is what I recommend.)

Assuming that the file (with .tar.gz extension is downloaded and copied over to the home directory of the boat user, you can extract it and copy it over to the apache directory.

Let us start with becoming the boat user. For the rest of the tutorial, the commands are executed with the id of the boat user.

sudo su - boat

Extract the software and copy it over to the web server’s directory.

tar xzf blosxom-2.1.2.tar.gz 
cp blosxom-2.1.2/blosxom.cgi /var/www/LifeOfPi/index.cgi

Now view the file /var/www/LifeOfPi/index.cgi and make the following two changes (if you prefer, you can change more parameters and the code offers decent explanation of each of the parameters):

# What's this blog's title?
$blog_title = "Life Of This Pi";
# Where are this blog's entries kept?
$datadir = "/home/boat/blosxom/data/";

Now, create the directories for  scripts and data.

mkdir /home/boat/blosxom/data
mkdir /home/boat/scripts/

Create the script that collects sample data and dumps to the file. The script is created with the name /home/boat/scripts/whatismylife.sh

#!/bin/sh

myts=`date +%y-%m-%d-%H-%M-%S`
myfile="/home/boat/blosxom/data/post-${myts}.txt"
mydate=`date`
mypi=`who | grep "^pi" `
mynpi=`who | grep "^pi" | wc -l `
mytiger=`who | grep "^tiger" `
myntiger=`who | grep "^tiger" | wc -l `
myuptime=`uptime`

echo "What the boat says at [$mydate]\n" >> $myfile
echo "At [$mydate], I have $mynpi Pi-s and $myntiger Tiger-s.\n<br>" >> $myfile
echo "<br>At that very minute, my sailing status looks like this: \n<pre>\n" >> $myfile
echo "$myuptime" >> $myfile
echo "\n</pre>\nPi-s on the system: \n<pre>\n" >> $myfile
echo "[\n$mypi\n]" >> $myfile
echo "\n</pre>\nTiger-s on the system: \n<pre>\n" >> $myfile
echo "[\n$mytiger\n]" >> $myfile
echo "\n</pre>\n" >> $myfile

Make the script executable

chmod +x /home/boat/scripts/whatismylife.sh

For the boat user, add a crontab entry that looks like the following line, so that the script gets executed every 10 minutes.

*/10 * * * * /home/boat/scripts/whatismylife.sh

To start with, execute the script once manually.

/home/boat/scripts/whatismylife.sh

Now, you can see the blog running on your Raspberry Pi device by visiting the URL of the blog (if your Raspberry Pi’s IP address is 192.168.0.109, then the url would be http://192.168.0.109/LifeOfPi/ ).
Here is a sample screenshot from my Raspberry Pi running Blosxom.

RPi Screenshot

Hope you liked this tutorial and your Raspberry Pi. Happy Baking!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.