Introduction: Pandora's Box - an Internet Radio Player Made With a Raspberry Pi!

Plug in this internet radio player and listen to your favorite stations on Pandora Radio. Six pushbuttons control the music, while an LCD gives feedback on the song, artist, and other useful information. All of this is housed in a custom acrylic case with access to the Raspberry Pi's ports.

Special thanks to LVL1 Hackerspace (www.lvl1.org) for use of their laser cutter!

Raspberry Pi is a trademark of the Raspberry Pi Foundation.

Step 1: Parts List

In order to build the system, you will need several things. The main piece is a Raspberry Pi Model B with a 5V, 1A power supply. This Instructable is based on a Revision 2 board, but will work on earlier boards with minimal changes. 

Next, you will need an SD card with a Raspbian Wheezy image. Instructions for this process found at: http://elinux.org/RPi_Easy_SD_Card_Setup

Electrical hardware needed:
1) Solderless breadboard (and wire)
2) Serial LCD module (3.3V module from Sparkfun was used: www.sparkfun.com/products/9067)
3) Ethernet Cable
4) USB Keyboard and mouse (necessary for setup, not the final product)
5) Pushbuttons (6) -  "Normally Open" type with threaded mounts
6) 3.3V Regulator (LM1117T-3.3 from Texas Instruments in the TO-220 package type used here)
7) Resistors: (7) ~10kOhm, (6) 1 kOhm
8) Capacitors: (2) 10 uF tantalum (as recommended by TI for 3.3V regulator
9) Pi T-Cobbler from Adafruit (not necessary, but makes GPIO access much easier)
10) Auxiliary audio cable

Other equipment needed:
1) Soldering Iron and solder (for the T-Cobbler breakout board)
2) 1-Minute Epoxy (for the acrylic case)
3) Acrylic (and a means of cutting it)
4) Nylon Standoffs and Screws
5) Velcro strips (with sticky sides)

The acrylic used in this project was 0.08" thick and can be found at Lowe's in 8"x10" sheets. This was great for cutting with a laser-cutter. Other thicknesses can probably be used - this will be left up to your discretion (and experience).

The standoffs used were a combination of (3/8"  - hex -  1.25" -  4-40 thread) and (3/8" - hex - 0.375" - 4-40 thread) nylon male/female standoffs. 4-40 fillister flat head nylon screws were used with these standoffs.

Although this Instructable includes directions for the custom case, this is probably the trickiest part of the design. Familiarity with a laser-cutter is necessary, since every laser cutter and design software package is different.

Step 2: Setting Up the Pi

After getting Raspbian Wheezy up and running on the Pi, the OS will need to be configured. Four changes need to be made.

1) In LXTerminal, run the following:

sudo raspi-config  

The SD partition should be expanded to fit the entire SD card, and the boot_behavior option should be set to boot to the desktop on startup. Also, enabling the SSH server will let us work on the Pi on a separate computer, without a keyboard and mouse plugged into the Pi.

2) Next, follow the instructions here to stop the OS from writing to the UART, which we will use to drive the LCD: 
http://learn.adafruit.com/adafruit-nfc-rfid-on-raspberry-pi/freeing-uart-on-the-pi

3) Adding Serial Communication to Python:
The OS comes with Python installed. We need to add the serial module in order to drive our LCD module. Run the following in LXTerminal:

sudo apt-get install python-serial

Documentation for the Pyserial module can be found at: http://pyserial.sourceforge.net/

4) Enhancing BASH's GPIO Capabilities:
Now, in order to access the GPIO pins in a more user-friendly manner, we will install the wiringPi library. Instructions are available here: https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/

However, to simplify these instructions, we only need to run:

sudo apt-get install git-core
git clone git://drogon.net/wiringPi
cd wiringPi
git pull origin
./build


Now simply wait for the build to finish, and the operating system is ready to install Pianobar, an open-source program running Pandora Radio.

Important warning: All of the work in this Instructable assumes you are working as the "pi" user. If not, the file locations used later on will need to be altered. Either work as the "pi" user, or be prepared to alter some scripts later.

Step 3: Installing Pianobar

Pianobar is an open-source, lightweight terminal program which runs Pandora Radio. Although the Pi is limited in resources, a lightweight application like Pianobar is right at home.  Setup is simple - run the following from a terminal:

sudo apt-get install pianobar

After entering "y" to all the prompts, Pianobar should be successfully installed. By entering pianobar in a terminal, the program begins. Log into your  Pandora account, and get a feel for the program. (Hint: typing "?" will give a list of keybindings for controlling Pianobar)

This installation has a directory for configuration files located in /home/pi/.config/pianobar. Our next step is setting up configurations for Pianobar, an example of which is given below. (Note that the login information needs to be replaced with your own Pandora account information).

Copy the following into a new file "config" in the /home/pi/.config/pianobar directory, changing the login information:


# This is an example configuration file for pianobar. You may remove the # from
# lines you need and copy/move this file to ~/.config/pianobar/config
# See manpage for a description of the config keys
#
# User
user = your@user.name
password = password
# or
#password_command = gpg --decrypt ~/password

# Proxy (for those who are not living in the USA)
#control_proxy = http://127.0.0.1:9090/

# Keybindings
act_help = ?
act_songlove = +
act_songban = -
act_stationaddmusic = a
act_stationcreate = c
act_stationdelete = d
act_songexplain = e
act_stationaddbygenre = g
act_songinfo = i
act_addshared = j
act_songmove = m
act_songnext = n
act_songpause = p
act_quit = q
act_stationrename = r
act_stationchange = s
act_songtired = t
act_upcoming = u
act_stationselectquickmix = x
act_voldown = (
act_volup = )

# Misc
#audio_quality = low
autostart_station = 123456
event_command = /home/pi/.config/pianobar/scripts/eventcmd.sh
fifo = /home/pi/.config/pianobar/ctl
#sort = quickmix_10_name_az
#love_icon = [+]
#ban_icon = [-]
#volume = 0

# Format strings
#format_nowplaying_song = %t by %a on %l%r%@%s
#format_nowplaying_station = Station %n
#format_list_song = %i) %a - %t%r

# high-quality audio (192k mp3, for Pandora One subscribers only!)
#audio_quality = high
#rpc_host = internal-tuner.pandora.com
#partner_user = pandora one
#partner_password = TVCKIBGS9AO9TSYLNNFUML0743LH82D
#device = D01
#encrypt_password = 2%3WCL*JU$MP]4
#decrypt_password = U#IO$RZPAB%VX2
tls_fingerprint = B0A1EB460B1B6F33A1B6CB500C6523CB2E6EC946




After setting the configuration file, restart pianobar from a terminal. It now automatically logs us into Pandora, and ask what station to play. After selecting a station, an 18 letter number will appear in parenthesis. By copying this number into the config file under autostart_station, Pianobar will automatically start playing that channel once it is opened.

If you are a Pandora One subscriber, additional settings in this config file will let you access high-quality streaming.

Step 4: Setting Up the Fifo

The fifo lets a script send commands to Pianobar. This is useful for sending button-based commands from a BASH script. In the last step, we configured Pianobar to look in the /home/pi/.config/pianobar directory for a fifo named “ctl”, which still needs to be created. In LXTerminal, the following command makes a fifo:

mkfifo /home/pi/.config/pianobar/ctl

To test that everything is working correctly, restart Pianobar. In another terminal window, enter:

echo 'n' >> /home/pi/.config/pianobar/ctl 

This should change the song being played by Pianobar. If so, we are now able to control it from another terminal (and by extension, a BASH script). All of the commands from Pianobar can work this way - and some interesting combinations can be made. Try “))))))” to increase the volume a lot at one time, or check out the difference between “s” and “s2”. Experimenting with this now will help our scripts make sense later.

Step 5: Setting Up the Hardware

There are four main components that need to be connected: the Raspberry Pi, the T-Cobbler breakout board, the serial LCD module, and the six pushbuttons. The Pi should not be connected to the power supply during these steps in case any wires get crossed.

Although it is not absolutely necessary, the T-cobbler makes working with the Raspberry Pi's GPIO pins much simpler. Some assembly is required for the breakout board (instructions for this are available at: http://www.adafruit.com/blog/2012/11/16/new-product-adafruit-pi-t-cobbler-breakout-kit-for-raspberry-pi/)

It is important to connect the ribbon cable the proper direction. On the Pi, the red line of the ribbon cable should be attached closest to the SD card (away from the yellow composite video connector).

Once the ribbon cable is connected to the Raspberry Pi, the T-Cobbler should be placed on a breadboard as shown in the second image.

Step 6: Connecting the LCD

The LCD module from Sparkfun accepts serial input, making it simple to communicate with on the Raspberry Pi's UART Transmit (TX) pin. 

This circuit uses three pins on the T-Cobbler breakout board - 5V, GND, and TX. For more information about the GPIO pins, refer to: http://elinux.org/RPi_Low-level_peripherals

Be careful about running wires next to the 3.3V regulator - it will be producing a modest amount of heat.




Bonus: Now that the LCD module is connected, some quick Python scripting will let us control it. Try the following Python "Hello World" script:

import sys, serial, time
global serialport
LCD = serial.Serial('/dev/ttyAMA0', 9600)
LCD.open()
LCD.write('\xFE\x01')      #CLEARS THE LCD
LCD.write('\xFE\x80')     #SETS THE CURSOR ON THE FIRST LINE, FIRST CHARACTER
LCD.write("HELLO")
LCD.write('\xFE\xC0')    #SETS THE CURSOR ON THE SECOND LINE, FIRST CHARACTER
LCD.write("WORLD")
LCD.close()

Step 7: Connecting the Pushbuttons

Each of the six pushbuttons needs a GPIO pin connection. The schematic shows a circuit that needs to be duplicated for each pushbutton. This circuit is useful since when the pushbutton is open, this arrangement does not drain any power. However, a logic "1" will be read on the GPIO pin, which is important to note. Also, the 1kOhm resistor on the GPIO pin is important in case the pin is set to output mode when the button is pressed. This would overdraw current on the pin, possibly burning it out. 

On the Revision 2 Pi, the six pushbuttons will connect to GPIO pins 2 (SDA), 3 (SCL), 4, 17, 27, and 22 (in order of vertical appearance on the Pi T-Cobbler). These pins are different for the Revision 1 boards; the pin changes can be found at the low-level peripherals link from the previous step.

EDIT: Since pins 2 and 3 on the Revision 2 board have pull-up resistors on them, the circuit I have shown is unnecessary (and may not function properly). You can simply connect the pushbutton between the GPIO pin and GND. 

The box has six buttons. In the picture, they they are attached from left to right in the pin order given above. The functions are:
1) Change Song
2) Change Station
3) Volume Down
4) Volume Up
5) Pause/Play
6) Menu/Shut Down

Button six has a dual function. If pressed momentarily, it brings up a Menu. If held for more than two seconds, it shuts down the Operating System. Once the Menu is open, new button functions are available. They are:
1) Like the current song
2) Dislike the current song (also skips to the next song)
3) Display the Pi's IP
4) Nothing (yet...)
5) Nothing (yet...)
6) Exit the Menu

Displaying the IP was added to make SSH access easier.

Step 8: Putting It Together With BASH and Python

Now that the hardware is connected, some custom scripts will give us control over Pianobar, the LCD, and the buttons. Programmers will probably cringe, but for simplicity's sake I gave each of these files full permissions for each user (in the terminal, enter "chmod 777 x.xx" where x.xx is the filename). This lets a BASH program execute a Python script, for example.

AUTOSTART FILE:

The first step to creating these scripts is to edit the autostart file in the LXDE directory. Edit this file with:

sudo nano /etc/xdg/lxsession/LXDE/autostart

Now add two lines to the end:

pianobar
bash /home/pi/.config/pianobar/scripts/PandorasBox.sh


Save and exit, and your OS is configured to start pianobar at startup. It will also begin continuously polling for pushbuttons. 

PYTHON AND BASH SCRIPTS:
The scripts used to control the LCD and buttons are hosted in a Github repository. They can be retrieved through the following:

cd /home/pi/.config/pianobar
git clone git://github.com/AyMac/Pandoras-Box.git
cd Pandoras-Box
git pull origin
cd ..
cp -r /home/pi/.config/pianobar/Pandoras-Box/scripts/ .

Now that the scripts have been retrieved, only one step remains to create a working system. 

Final Step: Since one of the options is to randomly select a new station, the main script needs to know how many stations are on a user's Pandora account. (To figure this out, start Pianobar and press "s". This will list all the available stations). Now open the file /home/pi/.config/pianobar/scripts/ButtonPiper.sh and change the "n=" value to the number found above.

Your system should be complete! Please comment on this Instructable with any issues you have, and I will try to address them. The next step talks about making a case, but the system should be fully functional before this step.

Step 9: Custom Box Design

A nice custom box is a great way to finish any project! Unfortunately, fabricating custom boxes is no cake walk. Plans for the box are attached, however should only be used as a general basis for your work. Verify the measurements (and puzzled edges) before starting the laser.

Note: The images attached are not to scale. They give dimensions for a box made from 0.25" acrylic. After a trip to Lowe's, I decided to use 0.08" acrylic instead, so the dimensions were adjusted accordingly when sent to the laser-cutter. Unfortunately, these plans are lost in the ether. You will have to use your judgement with the box design. Access to a drill (or drill press) and a tap set help tremendously. I do not recommend you attempt to cut the smaller holes (3/32") with the laser-cutter - they should be drilled instead.

In the six images provided, seven box pieces are given. One piece is for a horizontal divider inside the box, which is used to stack the Raspberry Pi above the breadboard. Once all seven pieces were cut, the holes should be drilled to the appropriate pilot size and then tapped as noted on the diagrams. Note that the LCD's holes are missing from the face plate, and will need to be added.

Before assembling the sides and bottom, connect the pushbuttons and LCD to the front plate. After this, the four sides and bottom can be assembled using 1-Minute epoxy. I did not use epoxy on the top piece, since I like being able to open the box (hinges or clear tape are both would be a nice choice to connect the top).

Once the sides are assembled, the breadboard should be attached to the bottom using velcro. 1.25" standoffs should be added, then the center piece screwed into these. After connecting the ribbon cable, the Pi should be mounted to the smaller 0.325" standoffs and screwed in place. Secure the top, and the system is complete! Photos of the final product are given in the last step.

Step 10: Final Product

Finished, assembled product for reference. I hope you enjoyed!