Pages

Friday, September 14, 2018

Raspberry Internet Radio part 1

My girlfriend and I love radio. Radio gives me up to date information and in depth intervieuws, documentaries and background stories. I prefer it far over television. The reason for this is that I can do things while listening to the radio. When watching TV it is (for me anyhow) not possible to do something simultaneous.

However the radio world is changing. Many radio stations here in the Netherlands are no longer available on the FM band, they are only available over cable or digitally. For listening to digital broadcasted radio stations one needs a special DAB+ radio. In my work-room I don't have a cable connection and I certainly do not want to buy a special DAB+ radio.

So listening to my favorite station on my computer would be an onvious solution. However the problem is structual. It not only occurs in my hobbyroom but in more rooms in our house. And I do not have a computer in every room. The solution would be a stand-alone internet radio. So let's build one !!

The Raspberry Pi Zero




After more as a year of waiting (due to popular demand) last year suddenly the Raspberry Pi Zero became available in the Netherlands. For those of you living under a rock: a bit of information. The Raspberry Pi Zero is a small (3cm x 7cm) full fledged computer. You add some components and you'll have a full Linux computer to your availability. It has a 1 Mhz broadcomm processor, 512Mb memory, a Full-HD HDMI connector, 26 available I/O ports, and 2 usb connectors. These are impressive specifications for such a small footprint. However the best part is that it only will set you back 5USD (6 Euro) !!




There is no network (UTP) connector. This can be solved in two ways.
There is a USB to network adapter widely available for just 5 euro. Or you can buy the Raspberry Pi Zero W which not only has Wifi but also Bluetooth and will cost just 10 USD (11 Euro).



You will still need some extra components to get it working:
- A SD-card for the operating system (8Gb is sufficient)
- An usb power supply (a 2amps phone power supply will do just fine)
- An USB hub for attaching mouse, keyboard, network adapter and audio Dac (I will get to that later)

And you will off course need a keyboard, mouse and screen. To start things up you can use your telly as the screen and borrow a mouse and keyboard from your computer. These will only bee needed when programming the Raspberry. When finished it will run so called headless, meaning that no mouse, keyboard or screen are needed anymore.

Even better: the Raspberry has a VNC license. So just download the VNC viewer on your PC and you will be able to use your PC as the screen, mouse and keyboard for your Raspberry. Your PC will function as a graphical computer terminal just like in the old days of computing. Activating VNC is a common practice with my setups as most of my Raspberry systems are running headless.




Next to that the Raspberry Pi Zero has no real audio connection (except for digital audio coming out of the HDMI connector). And while it is possible to create one with some complicated programs (by redirecting IO pins) and additional hardware it is easier (and cheaper) to buy a small USB-Audio-Dac. I bought mine for less then 2USD from Ali-Express.

The picture above shows the USB - DAC (Digital Analog Converter) with  on the left side a 3.5mm microphone connection and on the right side a 3.5mm headphone or active speakers connector. Active speakers are speakers that have a build in amplifier just like your computer speakers or speakers for your smart-phone.


That is about the complete picture.

The complete price-list for an internet radio.

As I am building an internet radio it is good to know how my DIY project will compare against a commercial DAB+ radio.

So here is the overall components list:

- Raspberry Pi Zero 6 Euro
- 8GB SD card 7 Euro
- USB power supply 5 Euro
- USB Hub 5 euro
- USB-network adapter 5 euro
- USB audio Dac 5 euro
- Speakers 11 Euro
Grand total 44 Euro

If you would use a Pi-Zero W you could save some money as you would not need the USB-Hub:
- Raspberry Pi Zero W 11 Euro
- 8Gb SD card 7 Euro
- USB power supply 5 Euro
- USB Audio Dac 5 Euro
- Speakers 11 Euro
Grand total 39 Euro.

 
I included speakers in the total price. However if you are going to attach the Internet Radio to your existing stereo or surround system no speakers are needed !!!
 

By good sourcing and scavenging your stockpile of rubble the total price can be lower.

Compare these prices to a commercial available DAB+ Radio or internet radio !!!

Installing the PI Zero Operating system.

There are ample descriptions on this all over the Internet. The best source is off-course the Raspberry home page which can be found here https://www.raspberrypi.org/documentation/  So I am not going into that.

And let's make sure the Linux version we are using is fully up to date. So open a terminal window by clicking on the terminal icon on the left-top side of the screen, where the arrow is pointing at.
As the picture below shows, a terminal window will open.

Now start by issuing the following commands:

sudo apt-get update
sudo apt-get upgrade


After each command press enter and wait till it is finished. Answer Y to any questions that might occur. After these have finished your Rapsberry Linux version is up to date.

Setting USB-Audio

As said before the audio will be send over HDMI by default. As we will not be using a monitor we need to make sure that the audio is re-directed to our USB audio DAC.

First make sure you are in the home/pi directory .




In the terminal window type:

CD ~

This makes sure you are in the home/pi directory.
In this directory we need to make a file that redirects the audio. to the DAC so carefully copy the following command:

sudo nano .asoundrc

And do not forget the . before the name asoundrc. The . makes the file hidden for normal vieuws.
The sudo nano command opens an editor window in which a file will be created called .asoundrc

Now type in the following commands or just copy and paste:



pcm.!default 

{
  type hw
  capture.pcm "mic"
  playback.pcm "speaker"
}
pcm.mic {
  type plug
  slave {    pcm "hw:1,0"
  }
}
pcm.speaker {
  type plug
  slave {
    pcm "hw:1,0"
  }
}

pcm.!default {
 type hw
 card 1
}

ctl.!default {
 type hw
 card 1
}


When done press Ctrl-x for stopping the editor and answer Yes to the question wether to save the file.

If everything went well you should now have a file called .asoundrc
You can check that by looking at the directory with the ls -a command

Now plug the speakers into the USB-Dac and attach the Dac to the Raspberry-Pi and reboot the Raspberry by choosing Reboot in the Shutdown menu (last item in the menu).




When the Raspberry has booted again open a terminal window and type in the next command:

speaker-test -t wav

If everything went as it should you should get a test signal on your speakers.

Playing MP3 files.

The standard Raspberry linux distribution does not support MP3 audio. And as most radio streams are broadcast in MP3 format we do need an MP3 player. There are several options for this. You could choose for Mplayer or for MPG123. Mplayer offers many features for audio and video playback. If you want most options you should choose this player. As I needed only MP3 playback I choose MPG123.

To install MPG123 just type in the terminal window:

sudo apt-get install mpg123

Finding radio stations

Most radio stations broadcast over the internet. But how to find them. Well Google is your friend. Just search for: list of internet radio stations
The most interesting directories I found were:

- http://www.listenlive.eu/
- http://dir.xiph.org/
- http://www.hendrikjansen.nl/henk/streaming.html
- http://vtuner.com/setupapp/guide/asp/BrowseStations/startpage.asp
- http://www.radiosure.com/stations/

The one I use most for searching stations is vtuner

Playing radio stations.

So you have found your favorite station and want the Pi-Zero to play it live for you.
For that we need to write a small Python program.

First open the terminal window again and make sure you are in the home directory. To get there just type:

cd ~

This makes sure you are directed to the /home/pi directory.

Now let's write the python program.

Type:

sudo nano radio.py

The Nano editor opens and you can write your program.
It's just a few lines:



#!/usr/bin env python

import os
os.system

('mpg123 http://icecast.omroep.nl/radio1-bb-mp3')


Just make sure you replace mpg123 http://icecast.omroep.nl/radio1-bb-mp3 with the link for the radio station you want to listen to.

After typing these lines press CTRL-X and make sure you answer yes to the question wether the program must be saved and check if the right name is used (radio.py).

That's it.
Let's test it.

In the terminal window just type:

python3 radio.py

And you should hear your favorite radio station over the speakers !!!

And that's all for now. The internet radio is working.

Next steps

In the second story in this series of 3 I am going to show you how to expand the python program to choose from more radio stations. And in the last story I am going to show you how to make a stand-alone headless (no screen, mouse and keyboard needed) internet radio with multiple preset stations.

Till then, happy listening

Luc Volders