Although the CP2102 serial adapters are both affordable and easily available, there is only one problem: their serial number is fixed at “0001” (at least the ones I bought myself). If you want to use more than one of these devices simultaneously on a Linux PC (eg Raspberry) you will have difficulties to create the udev roles to uniquely distinguish each adapter. Fortunately, the remedy is to re-assign each device a unique serial number. This is feasible using a program provided by the same manufacturer of the chip CP2102 (SiLabs). The package can be downloaded from the addresses at the end of this post. Inside the zipped downloaded file you will find the Windows\CP21xxCustomizationUtility folder which must be decompressed on your hard-disk (the package also includes versions for Linux and Mac users). Run the file CP21xxCustomizationUtility.exe, enter the new serial number into the “Serial” field, then click over “Program Device” to program the device (wait until the process is done and the data are verified). For my own purposes I also modified the Product Description (in this case it was shortened to “CP2102″). Simple and useful tool!
Making some modifications to some nice Python examples found on the Internet (Fuzzy Logic Robots, python-mpd documentation, raspi-hd44780 by Irvick) I was able to make a very basic, but fully working, standalone RPI Web Radio receiver.
Here is the complete wiring sketch:
In the above sketch, pin 1 of the LCD is the leftmost one. The rightmost 2 pins (15 and 16) are for the backlight connections. The LCD works at 5V. A 2 lines x 40 characters display was employed, because the titles and the station-ID are generally long. If a smaller display is being used (e.g. 16/20 characters), some modifications to the LCD program are necessary. Adjust the small 10 kOhm variable resistor to get the best LCD characters contrast.
S1 and S2 are used to move up and down through the channels, S3 and S4 to adjust the volume level. The pull-up resistors are all connected to the 3.3V power source. Idea: the four pushbuttons may be connected to a simple remote control circuit.
Take care not to short out each-other the 5V and the 3.3V outputs coming from the RPI and double-check the wiring before powering-up the RPI.
For simplicity, two independent programs have been made: one starts mpc and handles the four pushbuttons, while the other one handles the LCD (updated every 3 seconds). Both programs run in background.
The name of the playlist used by mpc is embedded into the program. The playlist is named “mymy” (see below).
MPD/MPC must be already be installed as described in Part 1.
1. download the python programs and change the file permissions:
mkdir /home/pi/rpi_web_radio cd /home/pi/rpi_web_radio wget http://www.gmpa.it/resources/rpiwebradio/radio.btn.py wget http://www.gmpa.it/resources/rpiwebradio/radio.lcd.py sudo chmod +x radio*
2. download the sample playlist “mymy.m3u” (stations may be obsolete at the time you’re reading this article, just update the contents as needed) then check/change the file permissions and the owner:
sudo cd /var/lib/mpd/playlists/ sudo wget http://www.gmpa.it/resources/rpiwebradio/mymy.m3u sudo chown mpd:audio /var/lib/mpd/playlists/mymy.m3u sudo chmod 644 /var/lib/mpd/playlists/mymy.m3u
3. download the following programs into /etc/init.d/ then change the files permissions:
sudo wget -O /etc/init.d/radio_btn http://www.gmpa.it/resources/rpiwebradio/radio_btn sudo wget -O /etc/init.d/radio_lcd http://www.gmpa.it/resources/rpiwebradio/radio_lcd sudo chmod +x /etc/init.d/radio_btn sudo chmod +x /etc/init.d/radio_lcd
4. make the programs run automatically at boot:
sudo update-rc.d radio_btn defaults sudo update-rc.d radio_lcd defaults
If needed, the two control programs can be manually stopped/started:
sudo /etc/init.d/radio_btn stop sudo /etc/init.d/radio_btn start
sudo /etc/init.d/radio_lcd stop sudo /etc/init.d/radio_lcd start
To stop the automatic program execution at boot:
sudo update-rc.d -f radio_btn remove sudo update-rc.d -f radio_lcd remove
The Raspberry, wired to the network (ethernet or wifi), should automatically start playing and the LCD should show the song title and the station name at every reboot.
Along with their conventional RF transmitters, nearly all major radio stations now broadcast their programs through the Internet (Streaming Media). Also, many web-radios are exclusively Internet-based.
Being cheap and small, transforming the Raspberry PI into an Internet radio player was really tempting. Adding a wireless adapter, plus a handful of cheap components, the RPI may easily be transformed into a standalone “receiver”.
This first article deals with the installation of some command-line programs: MPD, Music Player Daemon, MPC (Music Daemon control-program) and python-mpd. These programs can easily interact with any user-written programs.
The following setup was tested under a fresh installation of the latest Raspbian distro (do sudo apt-get update and sudo apt-get upgrade before starting).
Download and install the required packages from the repository:
sudo apt-get install mpd mpc python-mpd alsa-utils
Make sure that the sound module is enabled:
sudo cat /etc/modules
snd-bcm2835 must be present in the list. Otherwise add it.
The setup is done, so let’s test the “receiver” and make it play. Execute MPC:
it should reply:
volume: 80% repeat: off random: off single: off consume: off
Good, it should work.
MPC holds the list of “stations” on a playlist. It’s time to add the first “radio” to the playlist, so enter:
sudo mpc add http://18.104.22.168:80/CapitalMP3
Inside the playlist, each station is identified by a number, starting from 1 and increasing as we enter the radios one after another. Having only one station, make it play entering:
sudo mpc play 1
MPC should reply with:
[playing] #1/1 0:00/0:00 (0%)
volume:100% repeat: off random: off single: off consume: off
and… if an headphone or the speakers are plugged into the RPI’s audio socket, we should now hear the radio playing.
Deleting an entry from the playlist, the list is shifted backwards (so, if the 4th was deleted, the 5th will become the 4th, the 6th will become the 5th, an so on).
The volume can be adjusted with the following command:
sudo mpc volume (followed by any number from 0 to 100)
and again MPC will reply with some useful informations:
Let’s now add another nice station, then query the status of MPC:
sudo mpc add http://uwstream3.somafm.com:6200 sudo mpc
The reply should be something similar to:
[playing] #2/2 0:00/0:00 (0%)
volume:100% repeat: off random: off single: off consume: off
Now we can save our playlist entering:
sudo mpc save mylist
The list, named “mylist” in the above example, is saved under /var/lib/mpd/playlists/ and the full-name is mylist.m3u. More than one list can be created and loaded when needed.
If the playlist exists, it must be deleted before saving it, using:
sudo mpc rm mylist
Among the many MPC commands, the most useful ones are:
sudo mpc load mylist sudo mpc lsplaylists (show the available .m3u playlists) sudo mpc playlist (show the playlist content) sudo mpc clear (to empty the playlist)
To stop the player enter:
sudo mpc stop
Here is a very short and simple Python program that gets the Station Name and the Song Title from MPD and prints these informations onto the command-line:
#!/usr/bin/python import mpd client = mpd.MPDClient() client.connect("localhost", 6600) cs = client.currentsong() if 'title' in cs: SongTitle = cs['title'] elif 'file' in cs: SongTitle = cs['file'] print ('Song Title : ' + SongTitle) print ('Station Name: ' + cs['name'])
Download and run the above program:
wget http://www.gmpa.it/resources/py_mpd.py chmod +x py_mpd.py ./py_mpd.py
The program works only when MPD is tuned-in and playing (through mpc play).
Looks quite simple and promising, isn’t it ?