Jul 18, 2012

Case for my Raspberri PI using Lego building blocks

Got my new Raspberry PI today and the first thing was to make a nice little box for it. I had seen some Lego cases in the web, but they were custom blocks. So I used my Son's regular Lego building blocks and built this. Came out surprisingly good and the fit is just perfect :-)



Jul 3, 2012

Setting up a MP3 player and media streamer on a cheap router...

I have been considering hosting all my music collection in a central place in the house to reduce redundancy with the many gadgets we use now a days. A dedicated NAS machine was not a good option simply because electricity is precious and expensive in India. And then the cost of dedicating a 24/7 hardware to host files is also not a great idea. Even if I used a small NAS box to save power, it still does not allow me to play the music anywhere I want. I need clients everywhere that can browse network shares and play the music.

Secondly, the charm of a radio station is the unpredictability of what plays next. The charm never works with a music collection where we tend to play the same songs we like over and over again. So I have also been fascinated with the idea of streaming my own English collection randomly, possibly because there is not a single FM radio station in Bangalore that is dedicated to English music.

The other requirement is that I should be able to control the music in various ways, primarily through the Phones and PC's ate home. After considering various high cost alternatives (in terms of hardware cost), I think I have found pretty much the perfect option. A low cost router that can do all the above :-)

Now, why did I document it when there are tons of people who have tried the same on various forums and have succeeded over the years ? Well, all of them have scattered bits and pieces of information dating back to three years ago when openwrt was small and could easily fit the 4mb flash on the Asus 520gu. The older versions of mpd (music player deamon) were also used, mpd has grown to gigantic proportions in the meantime (by router standards) and none of the forums have consolidated information on how to run audio on the router with the latest features of mpd.

So after one week of piecing together various options and trying out build after build, here is a precise walk through.

 

What do you need :

 

1. A router compatible with either OpenWRT or Tomato open source with a available USB port. I personally started with the ASUS WL-520gU, primarily because it is available for less than Rs 2000 everywhere. Later on, I figured out that I could have made my life a ot easier with a router that ships with more than 4mb memory. My old Buffalo WZR-HP-G300NH was the second experiment and it was much easier to configure.

Anyways, this blog will focus on the low cost ASUS WL-520gU.

2. A cheap USB hub, maybe a Belkin 4 port USB hub that is available for Rs 250. A powered hub is a netter choice is you intend to keep a lot of music in multiple usb drives. I personally had a Logitech 4 port hub.

3. A cheap 1gb or 2gb USB drive that will host the OS files.

4. A big kick ass usb drive which can hold all your music collection. I personally went with a 32gb drive !

5. A cheap USB sound adaptor with any C-Media chipset that is available for Rs 100 in ebay or everywhere in local shops. ( Check MightyOHM forum for more options). And yes, if you want a high end Optical output, there are a few options there also.

Now we start :-)

Want your own website ? Take a look at Terabyte Websites. They take care of building, hosting and maintaining your website.

Setting up the router with Tomato Firmware :


1. Download the Tomato firmware for the router from http://tomatousb.org/download

I personally recommend the Kernel 2.4 Lite version at Tomato 2.4 Lite

Also download the Asus Utility package for the router from their website. You need the Asus Firmware Restoration utility.

2. Connect the computer with a Ethernet cable to the port closest to the WAN port and setup the ip on your computer to 192.168.1.2 manually. No other connections should be active on the PC.

3. Press the reset button on the router and connect the power cord to it even as the button is pressed. You will see the Power LED blink slowly, now release the button.

4. Start the Asus firmware utility, select the firmware file you downloaded and start the process. Allow it to reboot and get ready for a full 5 minutes.

5. Change the IP address back to automatic on your PC and check that the Asus router now gives you a DHCP ip address.

6. Go to http://192.168.1.1 and login with the default user and password, both being "admin".

7. Now connect a WAN link to the router and power off and on the router. It should come up and start acting like a router !


Setting up the router with the necessary configuration and libraries:


1. Go to http://192.168.1.1 and choose the "USB and NAS" option in the Tomato menu and enable "Core USB Support", disable printing support, and enable USB Storage Support.

2. Using a existing linux machine, partition your USB pen drive with a  256mb SWAP named partition and the remaining as a ext2 partition named OPT (do not choose ext3 as it reduces the life of the pen drive with frequent writes).  The partition names of SWAP and OPT are important.

3. Now connect the USB hub to the router and connect the swap/ext2 formatted Pen drive to it. You should be able to see it mounted on the Tomato web interface immediately.

4. Enable ssh interface in the web interface and set the admin password. Now ssh into the router ip (192.168.1.1) using user "root" and password as you just defined.


We are in the SSH world now and ready to install the software we need :


1. Edit /etc/fstab and add these two lines to the file:

LABEL=SWAP none swap sw 0 0
LABEL=OPT /opt ext2 rw,noatime 1 1

2. Issues these commands to make the mount of /opt permanent.

nvram setfile2nvram /etc/fstab
nvram commit
reboot

Wait for the reboot and SSH again to the router. Now you should be able to see the ext2 partition mounted as /opt and the swap active in the web interface or via the mount command.

3. Issue the following commands to enable download of optware packages.

mkdir -p /opt/tmp
cd /opt/tmp
wget http://tomatousb.org/local--files/tut:optware-installation/optware-install.sh -O - | tr -d '\r' > /tmp/optware-install.sh
chmod +x /tmp/optware-install.sh
sh /tmp/optware-install.sh

This will take a little while. Make sure there are no errors reported. 

Now we install mpd and mpc plus the dependencies. The problem with the packaged mpd is that it does not seem to work well with httpd streaming, and hangs a lot. So we circumvented with additional packages from other sources and copying some libraries over existing ones to get the latest versions :

ipkg update

cd /opt/tmp
wget http://tomato.groov.pl/repo/mpd_0.16.2-2_mipsel.ipk
wget http://tomato.groov.pl/repo/gettext_0.14.5-2_mipsel.ipk

ipkg install mpd_0.16.2-2_mipsel.ipk

echo src optware http://tomato.groov.pl/repo > /opt/tmp/optware.conf
echo dest /opt/ / >> /opt/tmp/optware.conf
ipkg -f /opt/tmp/optware.conf update
ipkg -f /opt/tmp/optware.conf install gettext_0.14.5-2_mipsel.ipk
cp /opt/lib/libintl.so.0 /opt/lib/libintl.so.3

ipkg update
ipkg install sqlite libuclibc++
ipkg install mpc

The above install of mpd will install quite a few dependencies. Wait till all is done.

Note : The above files are also hosted in my private dropbox account in case the original link does not work.
https://dl.dropbox.com/u/6329778/gettext_0.14.5-2_mipsel.ipk
https://dl.dropbox.com/u/6329778/mpd_0.16.2-2_mipsel.ipk

Do drop a note of thanks to shibby20 at either of the links below as we used his hosted links on tomatousb. The mpd version in the tomato repository is older and not as efficient, this is his private compiled version of mpd 0.16.2 which works well. Here are the links that detail this:

http://www.wikidot.com/user:info/shibby20
http://tomatousb.org/forum/t-362104/usb-sound-on-asus-wl-520gu

4. Run mpd on the command line. It should now run and give functional errors, but not crib about any dependencies.


Setting up the audio interface


1. Insert the USB audio device to the usb hub. Make sure that the router recognized the insertion by typing the "dmesg" command, you should see a few lines about the audio device insertion

2. Issue the following commands :

cd /opt/tmp
wget http://tomatousb.org/local--files/doc/audio_control_files.tar.gz
cd /
tar zxvf /opt/tmp/audio_control_files.tar.gz opt
cd /opt/tmp
tar zxvf /opt/tmp/audio_control_files.tar.gz autoruns-to-use
cp /opt/tmp/autoruns-to-use/80-start-audio.autorun /opt
rm /opt/mpd/playlists/radio.m3u

Scripts obtained from this link. The link was my first starting point for this project, so a big thanks to the folks out there.Especially the startup scripts are a godsend and make sure that the music starts streaming the moment you switch on the router.

Note : The above files are also hosted in my private dropbox account in case the original link does not work.
https://dl.dropbox.com/u/6329778/audio_control_files.tar.gz 

3. Edit this file : /opt/etc/mpd.conf

Remove the existing state_file line and add this instead:

state_file              "/opt/etc/mpd.state"
pid_file                "/opt/mpd/mpd.pid"

Having the pid file isvery usefull while you are experimenting. Just use the "mpd --kill" command to kill the existing mpd proess instead of. Similarly, the state file is used to save the state information every time mpd exists. So you start the next round from the song you stopped the router at.

3. Edit this file : /opt/etc/mpd.conf

Remove the existing audio_output section and add these sections :

audio_output {
    type            "oss"
    name            "My USB Sound Device"   
    mixer_type      "software"
    device          "/dev/sound/dsp"
}

audio_output {
    type            "httpd"   
    name            "My HTTP Stream"
    encoder         "wave"
    port            "8000"
    bitrate         "128"
    encoding        "mp3"
    format          "44100:16:2"
}

Notes : The audio output section literally took me days to get it right with the right balance of quality and usability. The Asus 520gu does not have the horsepower in the CPU to do vorbis or lame encoding on the fly. The router just crashes. The wave encoder is the only one that works reliable on the 520gu, but if you have a slightly better cpu power in your router, fell free to change it to lame and see. vorbis always crashers in any router, do not know why.

Also, if you need only the http streaming or just the audio output, then remove the other section to save some cpu cycles for the router.

3. Now it is time to run the daemon to check. Insert some speakers or headphones into the audio adaptor and run the following commands:

/opt/80-start-audio.autorun
mpd
mpc update
mpc add /
mpc volume 50
mpc play

You should hear the one song that was added from the zip file you unpacked. Yippeee.

4. Now it is time to check if the same music is ALSO being streamed over http.

Start up vlc on your PC. Issue the mpc play command again (the song is short) and connect vlc to http://192.168.1.1:8000 and press play. You should hear the streamed music on your PC.


This is a good place to make the router enter your existing network and free it of Ethernet cables.

Tomato has a great option to make the Wireless interface in the router act as a bridge to your existing wireless network. Just choose the bride option in Wireless, provide your wireless network credentials and reboot, you will be on your home network wirelessly and the WAN cable would no longer be required.

Now to add your local music collection for random play.


1. Copy all your mp3's to another pen drive, preferably formatted to fat and insert the pen drive to the hub.

2. Note down the mount point in the tomato web interface and change the "music_directory" path to the same in /opt/etc/mpd.conf

3. Issue the commands :

mpd --kill
mpd
mpc add /
mpc update
mpc random on
mpc play

And Voila, you should hear random songs from your entire collection being simultaneously played on the audio adaptor and streamed to your local network. You can control the playlist and the collection by installing Gnome music player client (GMPC) on Windows. Or on Android or iPhone via the mpc ports, they both have lovely interfaces.

You can also add internet radio stations to the playlist. In this case the router serves as a nice Internet radio station streamer.

I actually plan to connect a hi-end FM transmitter to the audio output. That way, I get to play the random songs on any FM radio in the house !!! Finally my dream of a English FM radio station at home for cheap, with approximately 7 watts of power being used :-)

P.S : Also added as a guide at my favorite hangout, techenclave.com !!!
http://www.techenclave.com/guides-tutorials/setting-up-mp3-player-media-134452/#post1674834