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 Max N300 (F7D3301/F7D7301) - 2 USB, Gigabit
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
*Belkin Wireless Play Max Router (F7D4301) - (Same as F7D8301) Dual Band, 2 USB, Gigabit
Belkin Wireless Play Router (F7D4302) - (Same as F7D8302) Dual Band, 1 USB, No Gigabit

*Recommended (Best model), typically $30-40.

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.

Requirements:
  1. TomatoUSB Supported Router (with at least one USB Port)
  2. USB Audio Adapter (USB DAC) - see below for some compatible ones
  3.  USB Flash Drive (>1GB) or USB Hard Drive. This tutorial will assume you're using a USB flash drive.
  4. Speakers - computer speakers, your home stereo system, iPod/iPhone dock with aux in.
Compatible/Recommended USB Sound Cards (Plug and Play)
Some additional USB DACs that I haven't tested personally, but should also work:
Outline of Steps:
  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)
http://www.dd-wrt.com/dd-wrtv2/downloads/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 & Belkin Wireless Play Max (F7D4301/F7D8301)
http://www.dd-wrt.com/dd-wrtv2/downloads/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

On the Belkin Play N600 HD (F7D4301/F7D8301) model, try holding down the WPS button for 10 seconds while powering on the router.

Also try the WPS instead of the reset button for the 30/30/30 reset, if you need to clear the NVRAM or having issues trying to get into the CFW Menu.

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 ext3 defaults 1 1" >> /etc/fstab

nvram setfile2nvram /etc/fstab 

nvram commit

#Install entware
cd /opt
#all one line
wget -O - http://entware.wl500g.info/binaries/entware/installer/entware_install.sh | 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