Usual disclaimers: I'm not a doctor, legal professional or financial advisor. This article is for information/education only and reflects my own opinions. It should not be taken as financial, legal or medical advice. Do your own research and never invest anything you cannot afford to lose (including your time).

4 July 2014

Raspberry Pi for Secret Agents

I've had one of those rare luxuries this week; a couple of spare hours to go through something I've been meaning to try out for a while. Yes I've got hold of a copy of the Raspberry Pi for Secret Agents book and the task of creating a custom SD card for our Pi lab to accompany the book.

The Pi lab is really starting to take shape and we have more equipment coming later on this year. I'm currently working on a Minecraft install with python API, finishing off the old recycled recycling truck time-lapse-dolley using a Pibrella and a few additional bits & pieces, but today belongs to the secret agent book.

To start with, I was getting on well with the book and setting bits up while waiting for some components to arrive. To do the audio exercises in chapter two, you really need a USB microphone, so I installed the packages, ordered one and put this project on the back burner again while I waited for snail-mail to arrive. Luckily it arrived yesterday which coincided nicely with a very rare quiet spell so I went back to chapter two

I noticed there were some flaws in the chapter. Some alias commands did not seem to work, they would not survive a reboot and I got errors when I tried to play back a recorded mp3 file using SoX (even though the file appeared to be created ok). There were also some things missing, like how to stop a recording (CTRL+C seems to work. If you're getting lock-ups from trying to fix the aliases, you can also try CTRL+X or CTRL+Z).

Lets start with fixing the main problem then. If you manage to get SoX to record and pipe it to Lame, you end up with a file that you can't play back and you get the error message "no handler for file extension mp3". I eventually found a fix for this which is: sudo apt-get install libsox-fmt-mp3 It seems the author must already have this (or something similar) installed. That should get those mp3 files playing.

The next issue for me was the Alias commands refusing to survive a reboot. The author suggests adding them into ~/.bash_aliases. I tried this and the commands were there in the file after reboot but didn't seem to work after I logged out and logged back in again. I came across this forum article about a custom message of the day (motd) which I thought would be good for us to use to provide some help to anyone using the card. It not only provided the inspiration for our own custom message, but also got me thinking 'well if that gets displayed to anyone logging in then any commands I put in there will get run as well'. It uses a file called ~/.bash_profile and if you put aliases at the end of that file they will indeed be there and usable by the person logging in. I then decided to customise this a bit further. I can see no point having weather information on a terminal which is right next to a window but I figured the one thing which is missing from that script is the infinitely-more-useful SD-card free-space remaining. I have added this bit in quite a crude way (using grep with character offsets discovered by trial and error)

Anyway here is my current .bash_profile in all it's glory. No doubt I will come back to this at some point. I also used Sudo mkdir ~/agent to create a working directory but you can skip that bit if you remove the last line of this script (cd /home/agent).

let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm%02ds" 
"$days" "$hours" "$mins" "$secs"`

# clear the screen
clear

echo "$(tput setaf 2)
   .~~.   .~~.    `date +"%A, %e %B %Y, %r"`
  '. \ ' ' / .'   `uname -srmo`$(tput setaf 1)
  .~ .~~~..~.   
  : .~.'~'.~. :   Uptime.............: ${UPTIME}
 ~ (   ) (   ) ~  Memory.............: `cat /proc/meminfo | 
grep Memfree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | 
grep MemTotal | awk {'print $2'}`kB (Total)
( : '~'.~.'~' : ) IP Address.........: `/sbin/ifconfig eth0 | 
/bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | 
/usr/bin/cut -d " " -f 1`
 ~ .~ (   ) ~. ~  SD Space left......: `df -h | 
/bin/grep "rootfs" | /usr/bin/cut -c 28-34`
  (  : '~' :  )   
   '~ .~~~. ~'    

Agent Pi: Our SD card to accompany the 'Raspberry Pi for Secret Agents' 
book.
Read the section on tmux (p34) before you start.

Extra packages installed: tmux, sox, espeak, lame, vorbis-tools

This message is:/home/pi/.bash_profile 
Use Alias to list shortcuts   
"

#back to white
echo "$(tput setaf 7)"
#(0)Black (1)red (2)green (3)yellow (4)blue (5)Magenta (6)cyan (7)white

#Alias commands here as .bash_aliases does not seem to work for some reason
alias recordmp3="sox -t alsa plughw:1 -t wav - | lame - "
alias record="sox -t alsa plughw:1 -t wav - | lame - "
alias recordwav="sox -t alsa plughw:1 -t wav - "
alias recordogg="sox -t alsa plughw:1 -t ogg - "
alias recordflac="sox -t alsa plughw:1 -t flac - "
alias play="echo 'use sox filename.ext -d for playback'"
alias encmp3="lame - "
alias encogg="oggenc - "
alias micvol90="amixer -c1 sset Mic 90"
alias micmute="amixer -c1 sset Mic 0"   
alias headphones="amixer cset numid=3 1:"
alias hdmi="amixer cset numid=3 2:"
#go to working directory
cd /home/agent


If you're wondering how I managed to get this hidden profile off my pi, just copy it as a text file first (cp ~/.bash_profile bash_profile.txt). Then you can use WinSCP to grab it from a Windows machine. If you're trying to go the non-M$ route, you can start up Midori on the Pi and upload it to Google docs (or more likely pastebin :) ).