Raspberry Pi Wireless Radio – Part 2
To control the Raspberry Pi radio, you’re going to need to connect a bunch of switches to the GPIO header block. How many switches and the functions they perform is up to you. For example you could use them as presets, or channel up/down. And don’t forget you’ll need something to control volume.
I purchased some ribbon cable, veroboard, tactile switches, an IDC connector and a bunch of resistors to make my control board (see pic below) but you could go a breadboard route at least initially to get something working. This post is an excellent introduction to the wiring required: http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/
To interface with my external switch board (via the GPIO header block), I’m going to use Python.
Installing Python
Although installing python in itself was straightforward, finding information on how to install the gpio library was more challenging, most users having used it with raspbian or debian. In the case of archlinux it was clearly going to have to be compiled from scratch which given I haven’t compiled anything from scratch, was a bit daunting.
However, whilst standing on my own shoelaces and generally face planting, I stumbled across this post on the subject http://archlinuxarm.org/forum/viewtopic.php?f=31&t=4654 which outlines everything needed to install both python and the gpio library as well as a host of other development tools required along the way such as gcc and so forth.
First install the development tools you’re going to need (including things like gcc)
$ sudo pacman -Sy file base-devel abs git
Next download the gpio source code and unpack it
$ wget https://aur.archlinux.org/packages/ra/raspberry-gpio-python/raspberry-gpio-python.tar.gz $ tar xf raspberry-gpio-python.tar.gz
Now run the make process that will create a package that can be imported by pacman, and import it
$ cd raspberry-gpio-python $ makepkg -Acs --asroot $ sudo pacman -U raspberry-gpio-python
By this point you now have Python 2 and 3 installed, plus a variety of other development tools and the gpio library.
NB: If at any point you see error message that look a bit like this:
error: failed retrieving file ‘libpulse-2.0-2-arm.pkg.tar.xz’ from mirror.archlinuxarm.org : The requested URL returned error: 404
the chances are that the package database in pacman is out of sync with what’s out on the internet. The following command will resync the database:
$ sudo pacman -Syy
If that doesn’t work try changing the download mirror
$ cd /etc/pacman.d $ nano mirrorlist
Comment out the original mirror and remove a comment from one of the others, then save and update http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=12840
A Simple Python Radio Control Program
This simple program has three radio stations and two mp3s assigned to various input buttons, plus a volume up and a volume down.
#!/usr/bin/python import RPi.GPIO as GPIO import time import os def PlayRadio(station_url): os.system("mpc stop") os.system("mpc clear") os.system("mpc add " + station_url) os.system("mpc play") def PlayMp3(mp3_file): os.system("mpc stop") os.system("mpc clear") os.system("mpc add " + mp3_file) os.system("mpc play") def VolumeUp(): os.system("mpc volume +2") def VolumeDown(): os.system("mpc volume -2") def mainProg(): # to use Raspberry Pi board pin numbers GPIO.setmode(GPIO.BCM) # set up unused GPIO as output channels and 0V # set up GPIO input with pull-down control # (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF) GPIO.setup(14, GPIO.OUT) GPIO.setup(15, GPIO.OUT) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(8, GPIO.OUT) GPIO.setup(7, GPIO.OUT) GPIO.setup(2, GPIO.OUT) GPIO.setup(3, GPIO.OUT) GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(10, GPIO.OUT) GPIO.setup(9, GPIO.OUT) GPIO.setup(11, GPIO.OUT) # set RPi GPIO output pins low GPIO.output(14, GPIO.LOW) GPIO.output(15, GPIO.LOW) GPIO.output(8, GPIO.LOW) GPIO.output(7, GPIO.LOW) GPIO.output(2, GPIO.LOW) GPIO.output(3, GPIO.LOW) GPIO.output(17, GPIO.LOW) GPIO.output(10, GPIO.LOW) GPIO.output(9, GPIO.LOW) GPIO.output(11, GPIO.LOW) # look for inputs on RPi board pins prev_input = 0 while True: #take readings this_cycle = 0 input = GPIO.input(25) if ((prev_input == 0) and input): print ("Button 1 pressed") this_cycle = 25 # ISA FM PlayRadio("http://80.13.146.243:8000/") input = GPIO.input(24) if ((prev_input == 0) and input): print ("Button 2 pressed") this_cycle = 24 # LBC PlayRadio("http://ice-the.musicradio.com:80/LBC1152MP3Low") input = GPIO.input(23) if ((prev_input == 0) and input): print ("Button 3 pressed") this_cycle = 23 # Fun Kids PlayRadio("http://icy-e-04.sharp-stream.com/funkids.mp3") input = GPIO.input(22) if ((prev_input == 0) and input): print ("Button 4 pressed") this_cycle = 22 # Rock Lobster mp3 Test PlayMp3("LobsterTest.mp3") input = GPIO.input(27) if ((prev_input == 0) and input): print ("Button 5 pressed") # Beethoven mp3 Test PlayMp3("ClassicalTest.mp3") this_cycle = 27 input = GPIO.input(18) if ((prev_input == 0) and input): print ("Button 6 pressed") VolumeUp() this_cycle = 18 input = GPIO.input(4) if ((prev_input == 0) and input): print ("Buttpon 7 pressed") VolumeDown() this_cycle = 4 prev_input = 0 if (this_cycle > 0): prev_input = 1 # software switch debounce time.sleep(0.05) def main(): mainProg() if __name__ == "__main__": main()
From here you can really go to town. I followed this up with a web interface coded in Python and Django to allow button configuration and all sorts. Far to long to blog about here. But plenty of fun to be had!
References:
http://www.instructables.com/id/Pandoras-Box-An-Internet-Radio-player-made-with/step2/Setting-up-the-Pi/
http://www.instructables.com/id/Pandoras-Box-An-Internet-Radio-player-made-with/step7/Connecting-the-Pushbuttons/
http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/ describes wiring
http://lwk.mjhosting.co.uk/?p=343 describes GPIO
http://elinux.org/RPi_Low-level_peripherals describes GPIO
http://pypi.python.org/pypi/RPi.GPIO/0.3.1a describes python programming with GPIO
http://docs.python.org/2/tutorial/index.html python for newbies
http://www.xatworld.com/radio-search/ for finding IP URLs for radio stations
http://www.codedefied.co.uk/2011/12/24/playing-bbc-radio-streams-with-mpd/ how to unpack BBC tokenised streams