Tutorial Part 1 - Everything but the LCD
I've split the tutorial up into two parts so that people who only have a Pi and a USB Wifi dongle can get most of the benefits of this project without having to do any actual hardware stuff.
Skill level:
The ability to read and follow instructions
Time:
~20 minutes if simply downloading my image onto an SD card and tweaking some config settings
1-2 hours if following this tutorial step by step
If you have the time it's worth doing it step by step so you learn since that's what the Pi is all about!
Requirements:
-
Raspberry Pi
- Model B Rev 1 & 2 tested
- Model A untested but should work, GPIO layout should be same as rev 2
-
Power supply for Raspberry Pi
- Current capacity depends on your design, if you are powering speakers and an amp like in mine you'll need to determine your current draw needs first. If you're only connecting a wifi dongle, usb flash drive, and are using an LCD you should be fine with the standard 5V 700mA supply still.
-SD Card 2GB minimum, 4GB or higher recommended
-Software for writing a .img file to the SD card,
use this guide's recommendations for sofware.
-
USB Wifi Dongle
- Preferrably one using the Railink RT5370 chipset however this tutorial will also cover using one with the rtl8192 chipset. Note Airplay will not work properly in ad-hoc mode with the rtl8192 chipset.
- NOTE: I've recently found some ebay listings claiming to sell RT5370 based dongles that are actually RT3070 dongles, I've posted about my experience here.
-Wireless internet connection (ie you need internet access and a wifi router)
- Earphones - You could opt to connect this to a stereo but I recommend earphones for initial setup.
Tools needed (for setup only):
TV or some sort of display with either composite video or hdmi inputs
Necessary cords to connect to the display
USB Keyboard
READ THIS FIRST!!!!!!!!
It is important that you follow these instructions closely.
Do not reboot unless instructed to do so in the procedure otherwise your
Pi could be improperly configured and might not boot after. If this
should happen don't worry you can always wipe your SD card and start
again.
When editing any config files on your Pi you should always
make a backup first, I try to include this in the instructions but I
may miss one or two.
If you get any errors after entering a command check if you missed typing "sudo" in front of the command
From here out there will be code boxes, simply copy the code from the box and paste it into your terminal session.
Before beginning it is important to note that you should ensure you have a good power supply for the Raspberry Pi, a lot of people have ran into issues that were caused by a bad power supply. Often times cheap no-name power supplies (typically from China) will say that they are 700mA or 1A etc but will not actually provide that current level. They may allow the Pi to boot when nothing is plugged into the USB ports but as soon as you start "loading" down the supply their voltage drops and can cause your Pi to reboot. Since this project is using a Wifi Dongle which will draw more current than other peripherals like a USB mouse or keyboard you will need to have a proper supply. A safe bet is to use a charger from a name brand phone (Samsung, Motorola etc) that is 700mA or higher.
The Image and SD Card
Download Rasbian Wheezy from www.raspberrypi.org (I was using 2013-02-09-wheezy-raspbian but hopefully as newer images are release they will work too)
Unzip the file
Use Win32DiskImager to write the image file to the sd card
(Consult
this guide if necessary)
Install the SD card into your Pi
Connect a usb keyboard
Insert your wifi dongle
Connect earphones to the Pi
Apply power to the Pi
First run setup
After a bit of a boot process you will be presented with the initial setup window
Expand Root Partition YES
Configure Keyboard (it takes a few seconds to start, don't worry)
- Choose Generic 102 key international keyboard
- On the next screen choose Other
- On the next screen select English US again (should be at the very top of the list)
- For the Alt Gr key just choose keyboard default
- Select the default options for the next questions
Optional: Change password for 'pi' user
Change Locale
- For Canada select en_CA ISO-8859-1 and en_CA.UTF-8 UTF-8
Hint, use spacebar to make selection, hit enter to continue afterwards
- For US select all of the en_US options
- I'm not familiar with the requirements for other Countries so you're on
your own, check for tutorials at rasberrypi.org or adafruit
- Next you will be asked which to choose as the system default, choose one of the UTF options for your country of choice, for Canada this is en_CA.UTF-8
Change Timezone:
- For Canada or US select US
- Select the desired timezone
SSH Enable
- Make sure this is enabled
Boot Behavior Start desktop on boot?
Disable, this is a headless system so we don't want it booting to the desktop each time
Do
not select update
Don't bother messing with any overclocking, it really shouldn't be necessary.
Finish
It should ask if you wish to reboot now, say yes.
If you didn't get the option to reboot or if you accidentally clicked no then you will now be at a command prompt, enter
Log in
username is "pi"
password is "raspberry" unless you changed it in the initial configuration
Configure Wifi
The easiest way to do this is via the desktop app called wpa_gui, make sure you know your wifi network name, encryption type (ie WPA, WPA2 etc) and your password before continuing.
Start the desktop by typing:
It will take a little while to start but you should eventually get a desktop. Now this will be a little tricky because we don't have a free usb port to connect a usb mouse so we'll do this purely from the keyboard.
Press Alt F2, this will bring up a Run window, type wpa_gui and hit enter.
The wpa_gui window should open, press the right arrow to select the "Manage Networks" tab.
Press tab until "Scan" is highlighted then press the spacebar.
A new window opens, press enter to scan.
Note the SSID of the network you want to connect to.
Press tab until close is highlighted, then press spacebar.
Press tab until "Add" is highlighted and press spacebar.
Type in the SSID of your network, use tab to continue to the next fields as necessary.
When finished press enter, if everything was entered correctly you should connect and obtain an IP address, write down the IP address that your Pi was given. If for some reason you were unable to connect using wpa_gui repeat the steps and verify the information is correct. Note that your SSID is case sensitive.
Once connected press Alt-F and select exit.
We'll launch a terminal window next
Press Alt-F2 again and this time type lxterminal
Now we'll reboot, type the following into the terminal window
Once rebooted login again
Installing software MPD & MPC
*NOTE
You can install multiple items at once by listing them one after
another like apt-get install mpd mpc however if there is any errors
during install it makes it really difficult to figure out where in
the process it had issues so I suggest doing them one at a time even
though it takes longer.
It will tell you how much disk space is required and ask if you want to continue, hit enter.
This will download mpd and install everything, it will take a few minutes
MPD will automatically try to start at the end of the installation, it will give an error saying "Failed to load database", this is fine. After a little while you should return to a command prompt, if not press enter and wait a little longer.
We now need to stop mpd and edit it's config file
Find the section titled Audio Output
*Tip use ctrl+w to search when using nano as a text editor
Change "My ALSA Device" to "PiFi Analog Output"
And add the following line after name and before mixer_type:
mixer_type "software"
Afterwards your Audio Output section should look like this:
audio_output {
type "alsa"
name "PiFi Mini Analog Output"
mixer_type "software"
device "hw:0,0" # optional
format "44100:16:2" # optional
mixer_device "default" # optional
mixer_control "PCM" # optional
mixer_index "0" # optional
}
Find the line
music_directory "/var/lib/mpd/music"
And change it to
music_directory "/home/pi/music"
Find the line
bind_to_address "localhost"
And change it to
bind_to_address "any"
Save and exit the file by pressing ctrl x, it will ask you if you want to save, press Y
It will ask the filename to write to, just press enter.
Now we need to make a music directory for the user pi
Start mpd again
Now we'll set the system volume to 100, this way whatever service is outputting sound will control the volume itself
This will open the alsamixer, press the up arrow until the volume is at 100, press esc when finished
Install mpc
It will tell you how much disk space is required and ask if you want to continue, hit enter.
This will download mpc and install everything, it will take about a minute.
Set mpc's volume to something low just to be safe
If for some reason you get any errors then mpd may need to be restarted after mpc is installed
You can do this by typing sudo service mpd restart
Now we'll copy a sample .wav file to test things out with
Now we'll tell MPC to update it's database
It should return the following
Updating DB (#1) ...
volume: 30% repeat: off random: off single: off consume: off
Now if we type
We should see our Front_Centre.wav displayed
Now we can add it to a playlist
Let's see if it plays
If you've followed everything correctly you should hear the words "Front Center" coming out of both headphones (*assuming you're listening through the headphone jack)
If you didn't get any sound at all try typing the following commands below. This should set the audio output to the headphones instead of hdmi, if you want to switch to hdmi change the last 1 to a 2
Try mpc play again
You may at this point wish to adjust the volume a little higher which you can do by using the mpc volume command like we used early and entering a percentage between 0 and 100. At this point I still would recommend keeping it low, probably no more than 50.
Installing Shairport an Airplay emulator
Again this will take a little while to download an install.
Now there are a bunch of other dependencies to download, its up to you if you want to try to install them all at once or one at a time.
Installing Net::sdp
Installing Shairport
Making shairport run at boot
Test shairport
Now fire up an iOS device, make sure you're on the same wifi network as your Pi and you should see a new icon beside your pause/play/ff/rw controls in your music app.
When you click this icon there should be an option "ShairPort 6736 on raspberrypi"
The number listed on yours will be different.
Select it and you should be able to stream music to your Pi now from your iOS device.
Setup hosts and hostname
Change this line
127.0.1.1 raspberrypi
To
127.0.1.1 pifi-mini
Press ctrl+x to save, type y, hit enter
Replace raspberrypi with pifi-mini
Note that after editing the above two lines you may get an error saying "could not resolve host pifi-mini" every time you use sudo in the next sections, don't worry this will clear after a reboot.
Make shairport broadcast PiFi-Mini as the airplay device name
We want to add one line and change one line
Under NAME add a new line and write
PUBLIC="PiFi-Mini"
Find the following line
DAEMON_ARGS="-w $PIDFILE"
And change it to
DAEMON_ARGS="-w $PIDFILE -a $PUBLIC"
It should look something like this
...
NAME=shairport
PUBLIC="PiFi-Mini"
DAEMON="/usr/local/bin/shairport.pl"
PIDFILE=/var/run/$NAME.pid
DAEMON_ARGS="-w $PIDFILE -a $PUBLIC"
...
Save the file by pressing ctrl+x, press y, press enter
Restart shairport
At this point if you are playing a song or stream using MPD and then send music to the Pi using Airplay both will come out of the speakers at the same time. Don't worry, we'll now write a little bash script that will fix this.
Copy and paste the following text:
PASTEBIN IS BEING WEIRD AND ADDING 4 SPACES TO THE START OF EVERY LINE, INSTEAD RIGHT CLICK ON "DOWNLOAD RAW" AND OPEN THE LINK IN A NEW TAB THEN COPY/PASTE. SORRY I'LL WORK ON GETTING THIS FIXED ASAP. NOTE THIS APPLIES TO ALL OF THE PASTEBIN SCRIPTS BELOW TOO.
Save the file by pressing ctrl+x, press y, press enter.
Make it executeable
Let's test it, but first lets delete the Front_Center.wav test file and download a better test file. This is a public domain mp3 so it's legal to download it. When you delete Front_Center it will ask you to confirm because it is a write-protected file, just type y and hit enter
We'll clear the current mpc playlist, update the database, then enable repeat.
At this point I recommend ensuring your volume is at a reasonable level, something 50 or less should be good.
Now we'll start the airplay monitor script and run it in the foreground to make sure there are no errors. It should just run and no text should appear.
Note that since we're running it in the foreground we'll have to press ctrl and c to stop it afterwards.
!!!!!!!!!!STOP and READ!!!!!!!!!!!!
The following could result in bad static that could damage your ears or stereo if you have the volume cranked so if you ignored my previous warning go back and set the volume to something below 50. At the time I write this (April 2013) there is still an audio glitch with the Raspberry Pi, at this point if you send a song via Airplay and it sounds fine and the MPD song pauses you're good. If not then skip to the next section called "Audio Fix".
On your iphone or ipod play a song to the Pifi Mini
Audio Fix
As of right now this is the only way I know how to do this, these files are taken from https://github.com/Hexxeh/rpi-firmware/commit/779f0fb6139452a0f1c4be32dab58eb87359517e
It is possible that later releases may not need this fix. First we'll backup the orginal files just to be safe.
You'll need to reboot once the files have downloaded
If your pi reboots without issue login and skip the next paragraph.
At this point if for some reason your Pi doesn't boot you'll have to power off the Pi and plug the card into a computer. Browse to the boot folder, delete the start.elf and fixup.dat files and rename the fixup.datBAK and start.elfBAK to fixup.dat and start.elf
If this is the case then I'm not sure what the issue is and you'll have to live with the audio glitch or google it to look for other solutions, one fix is using pulseaudio but it's not a great fix and I'm won't cover it in this tutorial anyway.
Assuming your reboot was sucessful try it all again, start mpc playing if it isn't already, start the monitor script, and try using airplay.
There should be a smooth transition (there will be a small pop when Airplay starts though), the song from your iOS device will start playing and the song MPD was playing should stop. When you end the Airplay connection the MPD song should resume without issue.
You can stop the airplay_monitor.sh script now by pressing the control key and c simultaneously.
Installing USB Mount
This is a nice little program that will automount a usb drive when it's plugged in so we can access it from MPD.
We'll make a symbolic link (ie shortcut) in our music folder to point to where our usb will automatically mount.
Plug in a usb drive with some mp3's on it (no portable hdd's unless you use a powered hub, stick with thumbdrives if powering off of the Pi itself).
Refresh the database.
Now we'll add all of your mp3's from the thumbdrive to a playlist - Later you can do all of this much easier using an app or the web interface.
Display all of the songs we just added to the playlist
In a little bit we'll install a web interface which will include a way to safely eject your usb drive, however if you wish to do this before then you'll have to do it manually.
We'll stop mpc so it's not accessing the usb otherwise we won't be able to eject it.
At this point you can safely remove your usb drive. You'll need to update MPC's database again to reflect the change otherwise it will think those files are still present.
Access Point failover mode, configure interfaces and rc.local
First we need to install a dchp server
The previous command will install the dhcp server and then automatically try to start, it will fail to start and give an error but don't worry about it.
Next we have to prevent the dhcp server from automatically starting.
Now we need to modify some config settings, as usual we'll backup the file first.
Find the line below with INTERFACES and add wlan0 between the quotes.
INTERFACES="wlan0"
Save and exit the file.
More configuring
Copy and paste the following into the new file.
Save and exit the file.
Configure Interfaces
Copy and paste the following into the new file
Save and exit the file.
!!!!!!!!!IMPORTANT!!!!!!!!!!
Before proceeding to the next step we should double check which chipset your wifi card is using.
Even if the packaging says one thing it could still be another
If you see Bus 001 Device 004: ID 0bda:8176
Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter then follow the Hostapd Realtek 8192 instructions below.
If you see Bus 001 Device 005: ID 148f:5370
Ralink Technology, Corp. RT5370 Wireless Adapter
Then follow the Hostapd Railink 5370 instructions below.
Note that your Bus and Device numbers may be different than shown
Realtek 8192 instructions:
Install Hostapd -
Realtek rtl8192 chipset (requires custom binary).
Prevent hostapd from starting at bootup.
Make a backup of the hostapd binary.
Change directory to /usr/sbin.
Get the new binary file for hostapd.
Make it executable.
Create and edit the hostapd config file.
Copy and paste the text below.
Point hostapd's init.d file to the right config file.
Find the line
DAEMON_CONF=
And add /etc/hostapd/hostapd.conf after the = like so:
DAEMON_CONF=/etc/hostapd/hostapd.conf
Save the file and exit.
Ralink RT5310 Instructions:
Install Hostapd -
Ralink RT5370 chipset
Prevent hostapd from starting at bootup
Create and edit the hostapd config file
Copy and paste the text below
Save and exit the file.
Point hostapd's init.d file to the right config file
Find the line
DAEMON_CONF=
And add /etc/hostapd/hostapd.conf after the = like so:
DAEMON_CONF=/etc/hostapd/hostapd.conf
Save and exit the file.
Test hostapd
This will stop the wlan0 interface, kill any wpa_supplicant programs running, start the hostapd daemon, start the wlan0 interface again at ip 10.10.10.1, and finally start running the dchp server daemon.
You should now see a new wifi network available called PiFi_Mini_AP, try connecting to it with a smartphone or laptop, remember the password is pifimini00
Your device should connect and should get an IP of 10.10.10.2, if you connected using an iDevice you should see the PiFi Mini available as an airplay device.
Reboot to get back to normal mode
rc.local
We're going to edit the rc.local file so everything gets done properly at startup. We'll backup the original, delete the original, then create a new rc.local file.
Copy and paste the following into your blank rc.local file, the code is commented for those who are curious or for advanced people who want to tweak or customize it.
UPDATED: I've improved the rc.local file, now it checks to see if a wifi card is present first, if not it then checks for a wired connection.
Save and exit the file.
We'll reboot again at this point, this time during the boot process you should see more info like which wifi network your connected to. Log back in once it's booted.
Testing AP Failover mode
First we'll backup our working wpa_supplicant.conf file, then we'll remove it so that when the system reboots it will fail to connect thus sending into failover mode.
It takes awhile to failover to AP mode so be patient, after awhile you should see a message on the display saying "Starting AP Mode". Once this happens try to connect to it with your phone or laptop, at this point we don't have the web interface setup yet so you wont actually be able to do anything once connected but it's good to make sure your phone or laptop connects and is obtaining an IP from your Pi. Your device should be given an IP of 10.10.10.2
Web Interface - Installing lighttpd and PHP
Note install these in the order listed below, if you install php5 before php5-cgi it will install apache as well and we don't want that. I suggest copy and pasting these lines one at a time letting each install before continuing to the next line.
Set permissions for the /var/www folder so php will run properly there
Add "pi" user to www-data group
You'll need to log out and log back in to obtain the permissions we just set, you can do this by typing exit
Test your webpage by entering your Pi's IP address into a browser, on some systems you can now try simply typing pifi-mini.local instead of the numerical IP (Android OS does not seem to like this). If it works then it's a good idea to bookmark the page so you don't have to type it each time.
You should see a PHP Placeholder page
Making a more detailed index page for diagnostics
Paste the following text into the text editor
Save and exit the file.
Refresh your browser page, you should now see a long page with a bunch of info about your php installation
Now let's rename that index file to test.php in case we need it later
Installing PiFi Mini webui and an MPD web interface
Note: For the web interface I chose phpMP which is I found to be the most full featured and supports php5, you could try phpMPreloaded which contains a bunch of different MPD web clients, however at the time of writing it's not fully php5 compatible
Edit your sudoers so www-data has some extra permissions
Add the following below pi All=(ALL) NOPASSWD: ALL
Save and exit the file.
In order for the webUI to be able to edit the wifi config file we need to allow non-root users to edit it
Note that if you edit the wpa_supplicant.conf file manually at any time you'll have to redo this step or else the webUI's Easy Wifi Config won't work
Before testing it out, we'll backup our working wpa_supplicant.conf file first in case it doesn't work
Now we'll replace the current wpa_supplicant.conf file with a blank one so that we know our webui actually did what it was supposed to.
We'll reboot now and you should see during the boot process that the Pi will be unable to connect to a network and will create it's own, note it takes about a minute before it fails over and creates the network. Look for the PiFi_Mini_AP network with your laptop or smartphone, once it appears connect to it. Remember the password is pifimini00.
Once connected open your browser and go to the PiFi Mini's web interface, at this point you should be able to simply point your browser to pifi-mini.local (unless you're using an Android device which doesn't resolve this address properly) else simply enter in the IP address 10.10.10.1
Click on Easy Wifi Setup
Enter in your wifi SSID, Encryption Type, and Password
Press "Submit and Reboot"
If all goes well your PiFi Mini will reboot and connect to your network without issue, if it doesn't then just look for the PiFi Access Point to show up in your wifi networks list and try again taking care to enter all of the info correctly.
It should work but if not you can restore your old wpa_supplicant.conf file
At this point you should have a working PiFi-Mini smart radio minus an LCD screen. You can keep this as is or if you're feeling adventurous you can continue on to part II and add an LCD.
Removing Unnecessary Packages & Files (Optional)
If you used a 2GB card you'll only have ~115MB of space left so you may want to do a little house keeping and remove what you don't need, after you should have ~400MB of free space. If you used a 4GB card or higher I suggest just leaving it as is.
Note this will remove desktop functionality, if you wish to preserve it do not proceed.
Continue to Part 2 to add an LCD screen if you want to.