Monday, June 3, 2013

PiTunes

Previously, I posted my experience automating iTunes for use in the nursery.  That has been working very well and I wouldn't normally see any reason to change it.  However, since I recently acquired a Raspberry Pi, I've had several ideas about what to do with it.  The idea that won was to replace the nursery room computer (which was already a Dell Studio Hybrid small form factor PC).  The advantages would be that the Pi is fanless and quite a bit smaller.  I could also then reuse the Dell Hybrid for other things.

The same objectives apply:
  1. I want the music to come on and turn off by itself
  2. I don't want the music to start off at full blast in case we've put the boys down early.    
  3. I want the music to play whenever we put the boys to sleep, which could be as early as 7pm but as late as 9pm.
  4. I want to be able to rotate playlists so it's not the same thing every night.
  5. I want to be able to kick off the music any time.
  6. I want to be able to manually override either the current song or the current volume level.
So, of course the first thing I did was download the Rasbian image and get it booted up, connected via SSH, and downloading updates.  I'll assume if you're also trying to do this that you'll use the instructions at RaspberryPi.org to get you started with that.  To get the most recent updates, type sudo apt-get update && sudo apt-get upgrade.  This may take a while, depending on your internet speed.  The Raspberry Pi doesn't have that much CPU either, so you might just kick this one off and go to bed.

I also purchased a Netgear G54/N150 Wireless USB Micro Adapter and plugged it in.  I'm a Windows guy, so most of what Linux does appears magical to me.  I had a suspicion that the Raspberry Pi would get on my wifi all by itself.  Of course it didn't because at the very least it didn't have my SSID and WPA pre-shared key (the password).  I was lucky enough that the Raspbian distribution recognized it and fired it up without requiring me to load any drivers (thank the stars!).  I verified this by issuing an iwconfig command.  This showed me that it was all ready to go.  Since my network uses WPA2 encryption, I had to configure a built in utility called supplicant.  In the end, all I had to do was edit /etc/wpa_supplicant/wpa_supplicant.conf and add a couple lines:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Hobbiton"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="yeahright"
}

Much documentation online says all you have to do to get it working is bring the interface down and back up.  I wasn't able to get it to grab a DHCP address without rebooting the Pi. To get the interface to come online at boot time, I added ifup wlan0 to /etc/rc.local.

After I got the Pi up and running, it was time to install a few things that aren't included in the base image.  Mainly, I needed mplayer (that's the media player that will actually play the music) and ffmpeg (a bunch of codecs that will allow mplayer to play all kinds of files including mp3 and m4a).  I also wanted to be able to access the log file from wherever, so I decided to install a lightweight web server.  I'll log to index.html in the www folder.  Type sudo apt-get install lighttpd mplayer ffmpeg and sit back for a while.    

The next thing is to get the actual music onto the Raspberry Pi.  At first, I started out by putting all the music (and my scripts) on a USB stick.  In order to give the Pi access to a USB drive, just follow the instructions here.  I eventually went instead with a network attached hard drive, so I followed the instructions here to create a persistent mount to my NAS.  After a while, I noticed some jitter in the music, so I moved the data back to the SD card (where the OS is also installed).  I'm trying to listen now to see if that makes a difference to the jitter.

Now, on to the script.  The script runs as a cron job.  The comments should explain exactly what's happening.

By the way, the contents of /mnt/nas/PiTunes are: christmas/  nothing/  music/  play.sh  waves/.  Version 2 of the script will play the Christmas directory instead of the music directory during the month of December.

The next part involves running the script from CRON, which is equivalent to Windows' Scheduled Tasks.  To add jobs, just run crontab -e.  The format is one job per line, parameters separated by spaces:


Mine looks like this:


So, let's look at the objectives again:
  1. I want the music to come on and turn off by itself - check!
  2. I don't want the music to start off at full blast in case we've put the boys down early - check!
  3. I want the music to play whenever we put the boys to sleep, which could be as early as 7pm but as late as 9pm - check!
  4. I want to be able to rotate playlists so it's not the same thing every night - check!
  5. I want to be able to kick off the music any time - this can be done, although it's not as easy.  I have to ssh to the Raspberry Pi and kick off the script manually.  However, if it's a silent day, nothing will happen.  I'll probably look at a way to adjust the script so that when it's called from CRON it does its normal thing but when called from command line it always plays music.
  6. I want to be able to manually override either the current song or the current volume level - This can also be done, but it's not as easy.  While in ramp up or ramp down mode, the volume doesn't increase/decrease by 1, it gets set to a particular level.  Overriding the volume during ramp up/down won't make any difference because as soon as the script loops it will reset the volume to right back where it was.  I may be able to change this in the future.  While playing, I can change the volume by logging in via SSH and issuing amixer -q set PCM X% where X is the volume I want to set to.  However, during ramp down, the volume starts back at 100% before dropping down.  It may not take much, but the next version may have an increment/decrement instead of fixed volume levels.  If I get this working, this objective will be met.
Some of the plans I have for the future are an automatic switch over to Christmas music instead of regular music during the month of December, web service based controls, & incremental/decremental volume controls.  We'll see when I have time to work on it.