måndag 16 juni 2014

Getting up-and-running with Raspberry Pi

Even though I'm careful always to shut down Raspberry Pi safely, I have had a lot of problems with file system corruption. Especially when updating the system, for some reason.
Going through all the steps of setting up Raspberry Pi after a file system corruption is quite bothersome so I've decided to write down all the steps here as a memo:

Creating a bootable image and starting up Raspberry Pi for the first time

(These instructions are for Linux. And much of this I got from Engadget, they also explain how to do it on Mac, see http://www.engadget.com/2012/09/04/raspberry-pi-getting-started-guide-how-to/)

0. Get hold of a SD card. For a list of card compatibility, see for example http://elinux.org/RPi_SD_cards

1. Insert the SD card in your computer or a USB SD card reader.

2. Check which device name the SD card is currently using. Type "sudo fdisk -l" and see which device name that corresponds to the SD card. For example "/dev/sdc".

3. Format the SD card. On Linux, install "gparted" if not already available: "sudo apt-get install gparted". Run "sudo gparted". Unmount any partitions, and set the size of one partition to the full size of the SD card, file system EXT4.

4. Download the latest Raspbian image from http://www.raspberrypi.org/downloads/

5. Unzip the archive to get the image, for example 2014-01-07-wheezy-raspbian.img

6. Write the image to the SD card: "sudo dd bs=1M if=2014-01-07-wheezy-raspbian.img of=/dev/sdc". Important note: Make sure to write to the proper device! "sdc" here is just an example.

7. Remove the SD card from your computer and insert it into Raspberry Pi. Make sure Raspberry Pi is turned off before inserting the card.

8. Power on Raspberry Pi. A configuration screen will show up, and we will change some configurations below... (If you need to reconfigure things in the future, you can call "raspi-config" from the command line any time to see the configuration screen.)

9. Select "expand_rootfs" to use the maximum available size of the SD card.

10. If needed, change "overscan" to make the screen take up the entire monitor real estate.

11. If needed, select "configure_keyboard" to configure the keyboard.

12. Select "change_pass" to set the user password for the user "pi".

13. If needed, change "locale". For example if you are in USA, select "en_US.UTF-8".

14. If needed, change "timezone".

15. Change additional settings if you'd like, then select "Finish".

16. You should see a login prompt. Login as "pi" and the password you selected during configuration.

Setting up WiFi:
Provided you have a usb WiFi dongle connected to your Raspberry Pi...
1. Do "startx" to start the windows session.
2. Once there, double-click "WiFi Config" and click "Scan" to scan for networks.
3. Double-click the wanted network and enter the PSK password.

Check your public IP address:
From a command prompt, type "curl icanhazip.com"

Install vim so that you can edit files more easily:
sudo apt-get install vim

Disable monitor blanking / powersave (so that you will always have a video signal out):
sudo vim /etc/rc.local
Above the "exit 0" line insert:
setterm -blank 0 -powerdown 0 -powersave off

Make sure Raspberry Pi always outputs video through the HDMI port:
If you don't do this, you might not get a video signal if you connect a monitor after Raspberry Pi is already booted.
Edit /boot/config.txt, for example: "sudo vim /boot/config.txt"
Activate these two lines, i.e. remove the hash character in front of these:
hdmi_force_hotplug=1
hdmi_drive=2

Installing NodeJS:
wget http://nodejs.org/dist/v0.10.18/node-v0.10.18-linux-arm-pi.tar.gz
tar -xvzf node-v0.10.18-linux-arm-pi.tar.gz
echo "NODE_JS_HOME=/home/pi/node-v0.10.18-linux-arm-pi" >> ~/.bashrc
echo "export PATH=$PATH:$NODE_JS_HOME/bin" >> ~/.bashrc
NODE_JS_HOME=/home/pi/node-v0.10.18-linux-arm-pi
export PATH=$PATH:$NODE_JS_HOME/bin

If you need to check which USB devices are available:
"lsusb"

To force mount a USB hard drive, so that it automatically becomes available at bootup:
1. Start Raspberry Pi without having the USB drive connected at first.
2. Once booted, connect the USB drive.
3. In a command prompt, do "df" to see which device name the USB drive now has. For example the device name might be "/dev/sda1" and the mount point might be "/media/HDPC-U"
4. Edit /etc/fstab, for example "sudo vim /etc/fstab"
5. Add a line for the USB drive, for example "/dev/sda1/ /media/HDPC-U vfat defaults 0 0"

Starting the GUI environment:
Type "startx"

Restarting Raspberry Pi from the command prompt:
sudo reboot

Safely shutting down Raspberry Pi from the command prompt:
sudo shutdown -h now


Audio
-----

Installing audio stuff:
"sudo apt-get install alsa-utils"

If you want to see the currently used audio device:
"amixer"

If you have installed an USB audio device, and want to use it as the default audio device,
do "sudo vim /etc/modprobe.d/alsa-base.conf", find the line that says "options snd-usb-audio index=-2" and comment it out
by adding "#" in front of the line. Save the file, and restart Raspberry Pi (sudo reboot), then call "amixer" to see that the USB
audio device is correctly selected.

To make sure audio can be played, connect headphones to the USB audio device, and try audio playback like this:
"aplay /usr/share/scratch/Media/Sounds/Vocals/Singer1.wav"

Install mpg321:
"sudo apt-get install mpg321"

Try playback with mpg321:
"mpg321 path-to-audio-file"

To set the volume of PCM audio output to 80%:
"amixer set PCM 80%"

To set the volume of Master audio output to 80%:
"amixer set Master 80%"


Auto-connect to WiFi if WiFi connection is lost (and how to create
automatic upstart jobs in general)
---
Sometimes my Raspberry Pi loses connection to my WiFi access point.
I wrote a small script that regularly checks if the connection is up or not.
If the connection is not up, the script will try to reconnect to the
access point.
I named the script "network-monitor.sh" and it looks like this:
#!/bin/bash

sleep 240

while true ; do
if ifconfig wlan0 | grep -q "inet addr:" ; then
sleep 120
else
echo "Network connection down! Attempting reconnection."
ifup --force wlan0
sleep 120
fi
done

The script is stored as "/home/pi/network-monitor.sh"
I also did "chmod 777 /home/pi/network-monitor.sh" so that the script
usage is not restricted to anyone.

To make sure that the script is automatically run after boot, I
created an upstart job.
I named the upstart job "network-monitor.conf" and it looks like this:
description "Network monitor startup script, Mattias"
author "Mattias Erlo"

start on startup
stop on shutdown

script
echo "Network monitor startup script" >> /tmp/network_monitor_log.txt
date >> /tmp/network_monitor_log.txt
exec sudo /home/pi/network-monitor.sh >> /tmp/network_monitor_log.txt 2>&1
end script

The upstart job is saved as /etc/init/network-monitor.conf

You can download these files from my Github account:
git clone https://github.com/mattiaserlo/network-monitor.git
sudo mv network-monitor/network-monitor.conf /etc/init/
mv network-monitor/network-monitor.sh .
chmod 777 network-monitor.sh
rm -rf network-monitor


A safe way to power-off Raspberry Pi (and how to read gpio pins on
Raspberry Pi in general)
---
It's possible to power-off Raspberry Pi safely from the command
prompt, using "sudo shutdown -h now",
although sometimes you may not have a keyboard and monitor connected
and you want to avoid just disconnecting
the power abruptly since it may damage the filesystem.
To manage this, I bought a simple button and connected it to Raspberry
Pi using the gpio interface.
A script monitors the button, and whenever it is pushed, the script
will safely shutdown the system.

I put the button on a small breadboard and connected one pin to gpio ground
and the other pin to GPIO2 (SDA). (Note: different revisions of
Raspberry Pi have different gpio pin configurations
See for example
https://www.google.co.jp/search?q=gpio+raspberry+pi+rev+2&safe=off&tbm=isch&imgil=AEoSB-jVMSPKuM%253A%253Bhttps%253A%252F%252Fencrypted-tbn2.gstatic.com%252Fimages%253Fq%253Dtbn%253AANd9GcQA2Q_v0ABDbffieN5DRL-vUjNeA6vTP4YGcqYjp-ySWnw_R35JSg%253B1000%253B472%253BlZZxIYgYnJnxNM%253Bhttp%25253A%25252F%25252Fwww.raspberrypi-spy.co.uk%25252F2012%25252F09%25252Fraspberry-pi-p5-header%25252Fraspberry-pi-gpio-layout-revision-2%25252F&source=iu&usg=__tnu8escFp66G1bDcuAiDcH7hcfM%3D&sa=X&ei=WVGVU-LWCIjhkgXy7oH4Cw&sqi=2&ved=0CCEQ9QEwAA&biw=1122&bih=581#facrc=_&imgdii=_&imgrc=OwSwobhBlcKnJM%253A%3BPehAY2N2I2o8lM%3Bhttp%253A%252F%252Felinux.org%252Fimages%252F2%252F2a%252FGPIOs.png%3Bhttp%253A%252F%252Felinux.org%252FRPi_Low-level_peripherals%3B254%3B581).

Now to enable gpio pin 2, you can type:
echo 2 > /sys/class/gpio/export
After having enabled it, you can check the value of gpio pin 2 like this:
/sys/class/gpio/gpio2/value

To continuously monitor the state of the button, I wrote a simple
script like this:
#!/bin/bash

echo "Running shutdown-monitor.sh script to monitor the poweroff-button..."

echo 2 > /sys/class/gpio/export

sleep 8

monitoring=1

while [ $monitoring = 1 ] ; do
value=$(> /tmp/shutdown_monitor_log.txt
date >> /tmp/shutdown_monitor_log.txt
exec sudo /home/pi/shutdown-monitor.sh >> /tmp/shutdown_monitor_log.txt 2>&1
end script

The upstart job is saved as /etc/init/shutdown-monitor.conf

You can download these files from my Github account:
git clone https://github.com/mattiaserlo/shutdown-monitor.git
sudo mv shutdown-monitor/shutdown-monitor.conf /etc/init/
mv shutdown-monitor/shutdown-monitor.sh .
chmod 777 shutdown-monitor.sh
rm -rf shutdown-monitor

Inga kommentarer: