Thursday, December 6, 2012

[Tutorial] Apple AirPlay on TomatoUSB Router

Hello and welcome to my first blog post.

In this blog posting, I'm going to show you how to setup Apple AirPlay on a router running TomatoUSB firmware. This will allow you to stream music from your iOS device or iTunes to speakers plugged into your router over your wireless network.

If you're not familiar with Apple AirPlay or TomatoUSB, here's a good introduction on AirPlay and a link to the TomatoUSB website. I would go as far as saying, don't buy a router unless it's supported by TomatoUSB.

First, lets talk about the router I'm using. It's a Belkin Share Max N300 Router (F7D3301/F7D7301). Here are some key things that makes this a great router in general and ideal for this project:

1) TomatoUSB Supported
2) 8MB Flash - allows you to run larger TomatoUSB builds (more features)
3) 2 USB 2.0 Ports
4) Gigabit Switch
5) The price I paid for it - $22 shipped

This router is very similar to the Asus RT-N16 (same chipset) minus the extra RAM, larger Flash and external antennas. The instructions below (not including the flashing instructions) will also work for the Asus RT-N16, Linksys E3000 and most modern router with USB Ports that is supported by TomatoUSB ('Yes' in either the K26 or K26RT-N columns and has USB ports).

Belkin Share Max N300 Router (F7D3301/F7D7301) Hardware Specs:

Broadcom BCM4716 Rev1
453 MHz CPU
8 MB Flash
64 MB RAM
2x USB 2.0
5 Ports Gigabit switch
WPS Button




I believe this particular model may have been discontinued. However, the following replacement models will also work, which I would highly recommend (same TomatoUSB flashing instructions) if you don't already have a router for this project:

Belkin Share N300 (F7D7302) - 1 USB, No Gigabit 
Belkin Play N600 (F7D8302) -  Dual Band, 1 USB, No Gigabit 
Belkin Play N600 HD (F7D8301) - Dual Band, 2 USB, Gigabit

Go by the model number and not the name. On the models with only 1 USB port, you'll need a USB hub because we need two USB ports - one for USB sound adapter or USB speakers and one for USB Flash drive or USB hard drive.

Other things you will need:

1) USB Audio adapter or USB Speakers - I'm using is the cheapy "3D Sound" ones you find on eBay or Amazon for $2-4 dollars. If you're in a hurry, order it from someone local for a couple of dollars more. Otherwise be prepared to wait 2 or 3 weeks if its coming from China.

2) USB Flash Drive (>1GB) or USB Hard Drive. This tutorial will assume you're using a USB flash drive.

3) Speakers (if you're using the USB Audio Adapter) - computer speakers or your home stereo system.

Here is a picture of the "3D Sound" USB Audio Adapter (cheapest option):



Other options I've tested:


Creative Sound Blaster X-Fi USB Sound Card











Logitech Z305 USB Laptop Speakers




Pioneer SMM301 USB Powered Speakers








Overall Outline:

1) Install TomatoUSB

2) Enable USB Support in Tomato

3) Install Entware

4) Install AirPlay (aka ShairPort) and other required packages

5) Configure the router so that everything works after a reboot

6) Enjoy some music


1) Install TomatoUSB

WARNING: you can brick your router if you flash the wrong firmware.

The flashing instructions below are specific to the Belkin routers I mentioned above. If you're using a different router, please use the appropriate tutorial/instructions for your particular model.

To install TomatoUSB, we first flash the router with DD-WRT and then from DD-WRT update to TomatoUSB.

  a) Download DD-WRT Firmware
  b) Download TomataUSB Firmware
  c) Enable Recovery Mode on Router
  d) Access the 'CFE miniWeb Server' Page and Update Router with DD-WRT
  e) From DD-WRT Flash to TomatoUSB

1a) Download DD-WRT Firmware for your particular model (.bin file):


Belkin Share N300 (F7D7302)
ftp://ftp.dd-wrt.com/others/eko/V24-K26/svn18946/dd-wrt.v24-18946_NEWD-2_K2.6_mini_f7d7302.bin

Belkin Share Max N300 (F7D3301/F7D7301)
http://dl.dropbox.com/u/42238/BelkinN300/dd-wrt.v24-15396_NEWD-2_K2.6_mini_f7d3301.bin

Belkin Play N600 HD (F7D4301/F7D8301)
ftp://ftp.dd-wrt.com/others/eko/V24-K26/svn18946/dd-wrt.v24-18946_NEWD-2_K2.6_mini.bin

1b) Download TomatoUSB Firmware (.trx file)

For these Belkin Routers, I would recommend the latest Big-VPN version, or BT-VPN if you want BitTorrent Client built into the firmware. I'm going to be using build 108 of the Big-VPN in this tutorial.

Download it here - http://tomato.groov.pl/download/K26/build5x-108-EN/tomato-K26USB-1.28.RT-MIPSR2-108-Big-VPN.trx

1c) Enable Recovery Mode on Router

Shortcut:
1) Unplug the router. Hold down the reset button while plugging in the router. Let go of the reset button.

If the shortcut doesn't work, try the long way.

Long way (aka 30-30-30 reset):
1) While the router is powered on press the reset button for 30 secs (do not let up on the reset button)
2) Keeping the reset button pressed, unplug the power (do not let up on the reset button)
3) Plug the power back in and wait for another 30 seconds (do not let up on the reset button)
4) You can now let up on the reset button

1d) Access the 'CFE miniWeb Server' Page and Update Router with DD-WRT firmware

5) Plug your desktop/laptop into port 1, 2, 3 or 4 and set your IP Address to 192.168.2.8 with a gateway of 192.168.2.1
6) Point your IE browser/Chrome (Do not use Firefox) to http://192.168.2.1 and CFE menu will be available. If you're having issues connecting to the CFE menu, try setting your computer to 192.168.1.8 and point your browser at http://192.168.1.1
7) Browse and Upload the dd-wrt .bin firmware file

This is how the CFE Menu looks like:



1e) From DD-WRT flash to TomatoUSB

8) Set network settings back to DHCP. It will take about 5 minutes to load DD-WRT. After about 5 minutes open a new tab on your browser and connect to http://192.168.1.1
9) When logging into DD-WRT for the first time, it will prompt you to set a password. Set the password to something very simple like 'password'. (Only used temporary).
10) Navigate to the firmware upload screen, Administration->Upgrade Firmware, and then load the TomatoUSB .trx firmware file (tomato-K26USB-1.28.RT-MIPSR2-108-Big-VPN.trx). Make sure to select the "Reset to Default Settings" after flashing.
11) Connect to TomatoUSB @ http://192.168.1.1. Use admin/admin as username/password.

At this point you should have TomatoUSB running on your router.

2) Enable USB Support in Tomato

Connect to your TomatoUSB router (http://192.168.1.1). Log in with root/admin as the username/password and go to USB and NAS-> USB Support (http://192.168.1.1/nas-usb.asp).

Make sure the following are checked, then click [SAVE]:.
Core USB Support
USB 2.0 Support
USB 1.1 Support OHCI
USB Storage Support
Automount (we'll use this later)




3) Install Entware (a package manager that allows you to install additional software on your router)


Plug in your USB flash drive and USB Audio Adapter or USB Speakers. Note that your USB flash drive will be completely wiped.

SSH (Putty) into your router. Use the default username and password of root/admin.

From here on, most of the work will be done via the console. Don't be afraid, you can cut/paste most of the commands. Note that the router needs to be connected to the internet because we will be downloading additional software.

#Partition your usb flash drive
umount /dev/sda1 
fdisk /dev/sda

# Type in the following commands:
# p # list current partitions
# o # to delete all partitions
# n # new partition
# p # primary partition
# 1 (one) # first partition
# <enter> # default start block
# <enter> # default end block #use the whole flash drive
# w # write new partition to disk



#umount /dev/sda1 incase it was automounted
umount /dev/sda1

#format newly created partition
#label disk as 'optware' case sensitive
mke2fs -j -L optware /dev/sda1

#mount the new disk partition
mount /dev/sda1 /opt

#Make sure /opt is properly mounted on a reboot.
echo "LABEL=optware /opt ext2 defaults 1 1" >> /etc/fstab
nvram setfile2nvram /etc/fstab 

nvram commit

#Install entware
cd /opt
wget http://wl500g-repo.googlecode.com/svn/ipkg/entware_install.sh
sh ./entware_install.sh

4) Install ShairPort (aka AirPlay) and other required packages


#download and install shairport
cd /tmp
wget http://dl.dropbox.com/u/42238/entware/shairport_0.05-20121002-1_entware.ipk
opkg install shairport_0.05-20121002-1_entware.ipk

#install dbus, avahi (aka bonjour), 
opkg install dbus avahi-daemon avahi-utils

#download extra kernel modules
cd /tmp
wget http://tomato.groov.pl/download/K26/build5x-108-EN/extras-mips2.tar.gz
mkdir /opt/extras
cd /opt/extras
tar xvzf /tmp/extras-mips2.tar.gz

#load sound modules
insmod /opt/extras/soundcore
insmod /opt/extras/snd
insmod /opt/extras/snd-hwdep
insmod /opt/extras/snd-page-alloc
insmod /opt/extras/snd-timer
insmod /opt/extras/snd-pcm
insmod /opt/extras/snd-seq-device
insmod /opt/extras/snd-seq
insmod /opt/extras/snd-rawmidi
insmod /opt/extras/snd-seq-midi-event
insmod /opt/extras/snd-seq-midi
insmod /opt/extras/snd-mixer-oss
insmod /opt/extras/snd-pcm-oss
insmod /opt/extras/snd-usb-lib
insmod /opt/extras/snd-usb-audio
insmod /opt/extras/input-core
insmod /opt/extras/hid
insmod /opt/extras/usbhid

#Lets do a quick test to verify that sound is working
#WARNING, may be loud
opkg install madplay
wget -O - http://ca.ah.fm:9000 | madplay -

#Got sound? Good. Lets continue.
#Control+C to break and stop music

#required to start avahi-daemon
echo "nogroup:x:2:" >> /tmp/etc/group
echo "nogroup:x:2:2:avahi daemon:/opt/sbin/avahi-daemon:/bin/false" >> /tmp/etc/passwd

#delete orphan pid files in case they exist
#rm /opt/var/run/dbus/pid
rm /opt/var/run/dbus.pid
rm /opt/var/run/avahi-daemon/pid

#start dbus first before avahi
/opt/etc/init.d/S20dbus start

#start avahi-daemon
avahi-daemon -D

#starts shairport 
shairport -a "Living Room" -b 282 -d

#Test playing some music from your 
#iOS device or iTunes via AirPlay

#to stop shairport, avahi and dbus
killall shairport
avahi-daemon -k
/opt/etc/init.d/S20dbus stop

5) Configure the router so that everything works after a reboot:


Go to USB and NAS -> USB Support (http://192.168.1.1/nas-usb.asp).



In the "Run after mounting" section add the following:

#load sound modules
insmod /opt/extras/soundcore
insmod /opt/extras/snd
insmod /opt/extras/snd-hwdep
insmod /opt/extras/snd-page-alloc
insmod /opt/extras/snd-timer
insmod /opt/extras/snd-pcm
insmod /opt/extras/snd-seq-device
insmod /opt/extras/snd-seq
insmod /opt/extras/snd-rawmidi
insmod /opt/extras/snd-seq-midi-event
insmod /opt/extras/snd-seq-midi
insmod /opt/extras/snd-mixer-oss
insmod /opt/extras/snd-pcm-oss
insmod /opt/extras/snd-usb-lib
insmod /opt/extras/snd-usb-audio
insmod /opt/extras/input-core
insmod /opt/extras/hid
insmod /opt/extras/usbhid

#required to start avahi-daemon
echo "nogroup:x:2:" >> /tmp/etc/group
echo "nogroup:x:2:2:avahi daemon:/opt/sbin/avahi-daemon:/bin/false" >> /tmp/etc/passwd

#delete orphan pid files in case they exist
#rm /opt/var/run/dbus/pid
rm /opt/var/run/dbus.pid
rm /opt/var/run/avahi-daemon/pid
#start dbus first before avahi
/opt/etc/init.d/S20dbus start

#start avahi-daemon
avahi-daemon -D

#start shairport 
shairport -a "Living Room" -b 282 -d


In the "Run before unmounting" section add the following:

#run before umount
killall shairport
avahi-daemon -k
/opt/etc/init.d/S20dbus stop


6) Enjoy some music


On your iOS Device or iTunes, play some music and look for the AirPlay icon.



You should now see "Living Room" as one of your AirPlay options





You're done!


Addendum - Adjusting the Volume


#install alsa-utils
opkg install alsa-utils

#link the correct sound device
mkdir /dev/snd
ln -s /dev/controlC0 /dev/snd/controlC0

#required for amixer
echo "audio:x:3:" >> /tmp/etc/group

#Check the mixer name. "PCM" in my case
#Could be "Speaker"
amixer

#Simple mixer control 'PCM',0
# Capabilities: pvolume pswitch pswitch-joined penum
# Playback channels: Front Left - Front Right
# Limits: Playback 0 - 128
# Mono:
# Front Left: Playback 128 [100%] [0.00dB] [on]
# Front Right: Playback 128 [100%] [0.00dB] [on]

#adjust volume to 70% or whatever is appropriate
#as low as 7% for earbuds/headphones
amixer set PCM 70%

#Simple mixer control 'PCM',0
# Capabilities: pvolume pswitch pswitch-joined penum
# Playback channels: Front Left - Front Right
# Limits: Playback 0 - 128
# Mono:
# Front Left: Playback 90 [70%] [-38.00dB] [on]
# Front Right: Playback 90 [70%] [-38.00dB] [on]

#To make the volume stick after a reboot

#Add the two commands to the run after mounting section
mkdir /dev/snd
ln -s /dev/controlC0 /dev/snd/controlC0
echo "audio:x:3:" >> /tmp/etc/group
amixer set PCM 70%


-------------------------

My personal awesome sounding DIY AirPlay setup for around $100 consist of the following:

- LP-2020A+ Lepai Tripath Class Amplifier ($20 - Highly Recommended)
- Pioneer SP-BS21-LR Bookshelf (discontinued - Here are some alternatives)

Special thanks to the Entware developers/maintainers. Entware is a replacement of optware. Please check them out - http://code.google.com/p/wl500g-repo/wiki/Goal.

Please leave some feedback in the comments and let me know if you run into any issues. Questions welcomed.

[Update 3/6/2013] Just got AirPrint working on TomatoUSB also. Check out the tutorial below. Sorry, at this time, AirPlay and AirPrint are not compatible with each other. The AirPrint solution uses, Optware which isn't compatible with Entware. Hopefully we'll resolve it soon.

[Tutorial] Apple AirPrint on TomatoUSB Router

[Update 4/9/2013] Please check out my PogoPlug Tutorial below. A PogoPlug running Arch Linux ARM is the perfect companion to your TomatoUSB Router. I highly recommend that you run AirPlay/AirPrint on the PogoPlug versus your TomatoUSB router.

[Tutorial] - PogoPlug E02 with Arch Linux ARM - NAS (Samba4), AirPlay, AirPrint, Google CloudPrint

38 comments:

  1. After a while I get like 4-5 "living room"s to choose from my AirPlay options.

    Also there is like a 5sec delay before pressing play in itunes and actually hearing music.

    Linksys 610N v2 (E3000) Tomato by Shibby 105.

    ReplyDelete
    Replies
    1. Venice, the only reason I can think of why you are seeing more than one "living room" AirPlay speaker option is because shairport some how got restarted. Try rebooting your router to see if it clears up the problem.

      The delay is normal due to buffering.

      Delete
  2. Hi - I posted in the openwrt forum but maybe here's better? Just trying to work out how to set the system volume as it's too high at the moment! Thanks

    ReplyDelete
    Replies
    1. Hi Andrew,

      Thanks for letting me know about the volume. I'm looking into a solution. Typically you would run 'amixer' or 'alsamixer' to control the volume which is part of the 'alsa-utils' package. However, entware doesn't have that package yet. I've opened an issue on the entware project page requesting the 'alsa-utils' package. I'm also attempting to build it myself. I'll keep you updated on any progress.

      If any reader can help with the build or can offer another solution/suggestions it would be appreciated. Thanks

      Delete
  3. Thank you, Qui Hong. Before your how-to the Shairport was a "black box" for me.

    Recently I've added a afpfs-ng package that helps to mount Time Capsule or other AFP shares right on router.

    ReplyDelete
    Replies
    1. Hi Alexander,

      This tutorial wouldn't be possible without you. Thank you so much for working on entware and building the shairport package.

      I find it amazing that here I am in SF, CA and you're in Smolensk, Russia, across the globe, and we're working together!

      Delete
  4. Thanks for the guide! Got the usb soundcard to play the techno stream on my headphones, but the shairport function doesnt work. My iphone sees "living room" tries to play there, fails and then pauses the music. Dmesg didnt have any related output, any ideas?

    ReplyDelete
    Replies
    1. @hhhhh,

      One quick thing to try:

      opkg remove shairport
      opkg install shairport

      For a short period of time a few days back, there was a newer version of shairport available on in the entware repo that required additional arguments to start up properly. That package has seen been reverted.

      Another thing to try is to manually start shairport and enable verbose logging to see if there's any obvious errors.

      "shairport -a "Living Room" -b 282 -v"

      Delete
  5. i failed to mention i am using the toastman build:
    tomato-K26USB-1.28.7500.4MIPSR2Toastman-RT-VPN.trx
    with matching extras sourced from shibby:
    http://tomato.groov.pl/download/K26/build5x-075V-ML/extras-mips2.tar.gz

    any chance either of these factors would cause shairport not to function? I have shairport4win on two of my comps and it works great!

    ReplyDelete
  6. Awesome tutorial. Easy to follow. Finally got my diy airplay setup running! Props to you dude! Would it be possible to have a custom tomato build with shairport integrated though so we dont have to connect a flahsdrive?

    ReplyDelete
    Replies
    1. @clone987,

      Thanks for the positive feedback. There is zero chances of a custom firmware with shairport integrated. :) I highly doubt it would fit on a 8MB flash.

      Care to share details on your diy airplay setup?

      Qui

      Delete
  7. Hello!

    I'm trying to install Tomato with AirPlay on my Belkin PlayMax N600HD router. Everything going fine till "opkg install shairport". Every install attempt finishes with "* wfopen: /usr/sbin/shairport: Read-only file system" error. I understand that there is a problem with chmod(?), but I don't know how to fit it. Any ideas what to do?
    (ofc when I try to start shairport, there is no such file - "-sh: shairport: not found")

    ReplyDelete
    Replies
    1. There might be a problem with the shairport package in the official entware repo. I've updated the tutorial to point to a known working version of shairport. Can you please give it a try and report back? Thanks

      Delete
    2. Everything working now.
      Thanks a lot!

      Delete
    3. One more question: is delay 8-9 sec normal? Shouldn't be like 2-3 sec?

      Delete
  8. I also had the same problem as KaM and the update to the tutorial fixed that. But there is an md5 error on the libao now. So I used the original @http://wl500g-repo.googlecode.com/svn/ipkg/openwrt/

    So I can get it working via ssh, but when I add the lines to the mount/unmount and reboot the router it never starts again? Any ideas there?

    ReplyDelete
    Replies
    1. Alright.. I figured that out. For the nvram line I had to change it to just
      "nvram setfile2nvram /etc/fstab"

      dropped the "nvram commit" off the end

      I'm running toastman...not sure if that's the issue with that?

      I also used the "tomato-F7D3302-1.28.7501.3MIPSR2Toastman-RT-Mini.bin" for the initial flash because the DD-WRT file you posted above wasn't working on my router.

      Delete
    2. Hey Doug,

      Thanks for pointing that out the run on command. Sometimes when I edit the tutorial, the editor sometimes doesn't behave properly.

      It should be two separate command:
      nvram setfile2nvram /etc/fstab
      nvram commit

      Which router model are you using again? I'll update the tutorial appropriately. Thanks

      Let's chat on testing the sound and newer version of shairport on the tomatousb.org forum.

      Delete
    3. As said.. kind of new to this, so wasn't aware it should be two lines. It seems to be working fine with out the nvram commit?

      I'm using the Belkin Share N300 F7D7302

      Delete
  9. Please post a link to the thread on tomatousb.org, where the Airplay is discussed.

    ReplyDelete
    Replies
    1. I've updated the tutorial with instructions for adjusting the volume and a link to a newer version of ShairPort if you're interested in testing it.

      Delete
  10. will you please make airplay available via ipkg?

    ReplyDelete
    Replies
    1. I'm working on it. Compiled already but not working properly and/or not tested. Once it's working I'll post a updated tutorial.

      Delete
  11. everything went fine until #load sound modules part. i got
    "insmod: can't insert '/opt/extras/soundcore.ko': unknown symbol in module, or unknown parameter"
    any idea?

    ReplyDelete
    Replies
    1. Did you use the same firmware as the tutorial? If not you need to find the extra modules compiled for your firmware to make it work.

      I had the same issue and I was able to get it to work after finding the correct extras.MIPS2 modules

      Delete
    2. no, i use dualwan firmware(1.28.0542 MIPSR2 K26 USB 8M). do you know how to find the correct extras.MIPS2 modules?

      Delete
  12. hi Qui Hong

    I was able to get it to work when I followed your tutorial (although i am using an Asus RT-N16, different firmware and extras module)
    but after reboot I had some issues:

    1. the USB drive did not mount to /opt on reboot. fixed this after i added to the "run after mount":

    #mount the new disk partition
    mount /dev/sda1 /opt

    2. The Shairport process is running, but there is not airplay device

    I went back to trace the steps and I am getting a failed message when starting the "S20dbus" dbus-daemon.


    Any idea on how to fix this?

    ReplyDelete
    Replies
    1. Hi Andy,

      1) This section should have taken care of mount /opt on reboot via fstab. Since you did this on an existing setup using a different firmware, I'm assuming your flash drive was not label as "optware" which is what is expected.

      #Make sure /opt is properly mounted on a reboot.
      echo "LABEL=optware /opt ext2 defaults 1 1" >> /etc/fstab
      nvram setfile2nvram /etc/fstab
      nvram commit

      2) Do you have this section in the run after mounting section?

      #delete orphan pid files in case they exist
      rm /opt/var/run/dbus/pid
      rm /opt/var/run/avahi-daemon/pid

      Delete
    2. Yes, I have them, but dbus still fails to start after rebooting. Until than everything works as described in this how-to. After umount /opt - reformat the "optware" partition - reinstalling opkg, dbus and avahi everything goes ok again.

      Nice job though...

      my setup:
      -wl500g+2Gb usb pen drive+usb sound card
      -/opt mounts correctly
      -my extra modules are in post-mount script
      -/tmp/etc/group and passwd files modified on flash

      Delete
    3. I had the same problems with /opt and dbus.
      I had to add
      rm /opt/var/run/dbus.pid

      Delete
    4. True that!!!!!


      "rm /opt/var/run/dbus.pid" instead of "rm /opt/var/run/dbus/pid". Now I'm up and running! Thanks a lot!

      Ed

      Delete
    5. On the other hand for avahi-daemon the above mentioned "rm /opt/var/run/avahi-daemon/pid" is the right choice. So the right way is the following:

      rm /opt/var/run/dbus.pid
      m /opt/var/run/avahi-daemon/pid

      Delete
    6. Thanks for letting me know. Might be dependent on the dbus package used. I'll update the tutorial to delete both files (in case either one exists).

      Delete
    7. I went ahead and updated the location of the dbus pid file in the tutorial. Looks like the dbus package in entware was updated and the pid file location changed.

      Thanks for letting me know!

      Delete
  13. Qui:
    Thanks for putting this tutorial together.
    Everything works until I tried to start Avahi.

    avahi-daemon -D
    Timeout reached while wating for return value
    Could not receive return value from daemon process.

    Any ideas?

    ReplyDelete
    Replies
    1. run it without the -D to see what the issue is.

      Delete