Wednesday, January 23, 2013

Improvements to my RPi radio

Some weeks ago I posted about how I set up my older (256 MB RAM) Raspberry Pi as a wifi radio.
It continues to work extremely well but now I've enhanced it with two very useful upgrades.

I now have 29 preset channels, one of which is LastFm.
Of course, I can put as many channels as I want on my RPi and there's essentially no restriction as long as the channel I want is actually available on the internet.

I almost always control the radio with my iPad (which is actually a generation one iPad).
One thing I didn't like about my initial setup was that I couldn't see what songs were being played (when I had selected a music channel).
Well, that's actually very easy to rectify as just running the command mpc outputs quite a lot of useful information on what channel is playing and information regarding the music being played (for most, but certainly not all, channels).

To output the channel and song information on my iPad (or whatever other device I'm using to access my headless RPi wifi radio), I added these lines to the script to start the channel (this example is for the station Soft Classic Rock):


while true; do   ## sets up a never-ending while loop
echo                      ## prints a blank line
mpc | grep Soft | cut -c1-17  ## selects ONLY line with word 'Soft', prints first 17 chars.
mpc |  grep volume   ## prints ONLY the line containing the word 'volume'
echo   ## another blank line
for i in {1..5}    ## sets up a for loop which iterates 5 times
do                     ## part of the for loop asking for action to be taken
mpc | grep Soft |  cut -c20-  ## Select the 'Soft' line, print everything but first 20 chars.
sleep 30       ## wait for 30 seconds before iterating again
done             ## end of for loop
done              ## end of while loop
The mpc command on its own provides this information while the channel is active:

Soft Classic Rock: Moody Blues - Nights In White Satin
[playing] #1/1   1:23/0:00 (0%)
volume: 50%   repeat: off   random: off   single: off   consume: off
but after mangling it through the addition to the script shown above it comes out like this:
 volume: 50%   repeat: off   random: off   single: off   consume: off
Moody Blues - Nights In White Satin 
Moody Blues - Nights In White Satin 
Moody Blues - Nights In White Satin 
Moody Blues - Nights In White Satin 
Moody Blues - Nights In White Satin
Soft Classic Rock 
volume: 50%   repeat: off   random: off   single: off   consume: off
and this goes on forever, changing when the played song changes, so it's always easy to see what channel and what song is playing as well as some technical stuff such as the volume setting.
Incidentally, this latter is easy changed with a command such as
$ mpc volume 70
which will increase the volume from 50% to 70%.


As I mentioned in my previous post, a central part of setting up my radio menu (on whatever device I use to connect to the headless wifi RPi -- usually my iPad) was the great script posted here.

Strangely, however, I found that the order of the menu items produced was quite variable, which is a little disconcerting if you have a lot of menu items as you'll need to spend sometime finding the channel you want.
I still don't understand why they don't come out in alphabetical order.

Nevertheless, to organize my radio menu, I went about it like this:

1. Select some categories that cover all of your channels such as Blues, Pop, Sport, News.

2. Create directories in the ~/radio folder corresponding to each of the selected categories.

3. Modify the menu script to reflect the new arrangement.
The only required change is in the line starting with 'fileList' where the maxdepth is changed from 1 to 2 to reflect the fact that the channels are now listed two levels down from the ~/radio/ directory.

Here's what my menu now looks like:


#!/bin/bash
# Displays a list of files in current directory and prompt for which 
# file to edit 
cd /home/pi/radio/ 
# Set the prompt for the select command 
#echo -e '\E[1;34m '  
PS3="Type a number or 'q' to quit: " 
#echo -e '\E[0m ' 
# Create a list of files to display 
#echo -e '\E[1;33m ' 
fileList=$(find . -maxdepth 2 -type f | sort -k1) 
#echo -e '\E[0m ' 
# Show a menu and ask for input. If the user entered a valid choice, 
# then invoke the editor on that file 
select fileName in $fileList; do    
if [ -n "$fileName" ]; then        
${fileName}    
fi    
break 
done
When I run the menu command (which I've shortened to mn), here's what I see:

 1) ./Blues/bu4blues                         16) ./News_Talk/BBC_Radio4  
2) ./Blues/Lemonos_Blues               17) ./News_Talk/BBC_Radio4_Extra  
3) ./Blues/Super_Blues                   18) ./News_Talk/BBC_Radio4_LW  
4) ./Classical/BBC_Radio3            19) ./News_Talk/BBC_World_Service  
5) ./Classical/BBC_Radio6            20) ./News_Talk/RTE_Radio1  
6) ./Classical/lyricfm                     21) ./Pop/Absolute_Classic_Rock  
7) ./Commands                            22) ./Pop/Absolute_Radio  
8) ./Irish/Raidio_na_Gaeltachta    23) ./Pop/BBC_Radio1  
9) ./MPB/MPB10                        24) ./Pop/last_fm 
10) ./MPB/Radio_Mix_Brasil        25) ./Pop/Radio_Caroline 
11) ./MPB/Radio_MPB_Brasil      26) ./Pop/RTE_Radio2 
12) ./MPB/Radio_Qualidade_Brasil 27) ./Pop/Soft_Classic_Rock 
13) ./NewAge/Eternity                  28) ./Pop/Virgin_Radio 
14) ./NewAge/New_Age               29) ./Sports/BBC_Radio5_Live 
15) ./News_Talk/BBC_Radio2      30) ./Sports/BBC_Radio5_Live_Extra 
Type a number or 'q' to quit: 
Now, that's what I call organized. Impossible to get lost here.

OK, this post is already too long.
I'll another on how I got LastFm to play on my RPi wifi radio.









No comments:

Post a Comment