Friday, September 21, 2018

Raspberry Pi Internet Radio Part 2

For a complete index of all my stories click this text

This is the second story in the series of 3 where I am going to make an internet radio with the humble Raspberry Pi Zero.

In the previous story I showed you what materials you need, how to install the raspberry OS and make it up to date, how to find radio stations on the internet and how to play an internet radio station from a Python program. You can re-read that story here: http://lucstechblog.blogspot.com/2018/09/raspberry-internet-radio-part-1.html
 

In this story I am going to expand the program so you will have the choice of multiple radio stations.

The radio stations

I love news shows and radio documentaries and radio drama. Therefore I choose the radio stations that broadcast these programs. It is a mix of 3 Dutch and 2 English stations for which the links are:

NPO1                       http://icecast.omroep.nl/radio1-bb-mp3
NPO2                       http://icecast.omroep.nl/radio2-bb-mp3
NPO5                       http://icecast.omroep.nl/radio5-bb-mp3
BBC Radio 4            http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p
BBC World news     http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws

Feel free to replace these with your ow preferred radio stations.

The Python program

What I wanted in the end is a Raspberry Pi with just a few buttons that allow me to choose some preset radio stations without the need for a screen, mouse or keyboard. And that is what we are working towards. But maybe you want something different. That is why I am first going to make an internet radio with a nice menu that allows me to choose between the stations.



When your Raspberry has booted open a terminal window by clicking on its icon at the left side on your screen where the arrow in the picture points at.

Next make sure that you are going to work in the moe/pi directory by giving the following command:

cd ~

Then start the Nano editor by giving the next command in the terminal:

sudo nano radio2.py

This makes sure that the Nano editor is started and a new file is made with the name radio2.py

Then copy the next program and paste it in the terminal window



import os
import sys

SOUND =  'mpg123 -q http://icecast.omroep.nl/radio1-bb-mp3 &'
SOUND2 = 'mpg123 -q http://icecast.omroep.nl/radio2-bb-mp3 &'
SOUND3 = 'mpg123 -q http://icecast.omroep.nl/radio5-bb-mp3 &'
SOUND4 = 'mpg123 -q http://bbcmedia.ic.llnwd.net/stream/bbcmedia_radio4fm_mf_p &'
SOUND5 = 'mpg123 -q http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws &'

while True:
 os.system('clear')
 print ("")
 print ("====================================================") 
 print ("             Internet radio stations")
 print ("====================================================")
 print ("")
 print ("1  Radio 1")
 print ("")
 print ("2  Radio 2")
 print ("")
 print ("5  Radio 5")
 print ("")
 print ("b  BBC 4")
 print ("")
 print ("w  BBC world news")
 print ("")
 print ("s  Shutdown system")
 print ("")
 nummer = input('What is your choice: ')

 if nummer == "1":
  os.system('sudo killall mpg123')
  os.system(SOUND)
 if nummer == "2":
  os.system('sudo killall mpg123')
  os.system(SOUND2)
 if nummer == "5":
  os.system('sudo killall mpg123')
  os.system(SOUND3) 
 if nummer == "b":
  os.system('sudo killall mpg123')
  os.system(SOUND4)
 if nummer == "w":
  os.system('sudo killall mpg123')
  os.system(SOUND5)     
 if nummer == "s":
  os.system('sudo killall mpg123')
  os.system('sudo shutdown now')


When the file is copied close the nano editor with CTRL-X and answer yes at the question wether the file has to be saved and check if the right filename is used: radio2.py

A closer look to the program.

The program starts with importing the necessary libraries for accessing Operating System functions from within Python.

Next the radio stations are defined. Let's look closer at one of the definitions.

SOUND3 = 'mpg123 -q http://icecast.omroep.nl/radio5-bb-mp3 &'

MPG123 calls the program that plays our MP3 broadcasts.
The -q makes sure that mpg123 does not display any information.
The & at the end of the line makes sure that mpg123 works in the background so that the python program radio2.py is always the program that has the focus,

while True:

makes sure that the program keeps running

os.system('clear')

Clears the terminal window so that we have a nice clean screen.

Then the screen is build up by the print commands and

nummer = input('What is your choice: ')

waits for us to input our choice followed by ENTER.

Nexts the IF statements test what choice we have made. Again let us look at an example:

        if nummer == "5":
                os.system('sudo killall mpg123')
                os.system(SOUND3)

If we choose "5" first any previous choice of a radio station is shutdown by sudo killall mpg123and then the new choice is activated by os.system(SOUND3).

And take a closer look at these lines:

        if nummer == "s":
                os.system('sudo killall mpg123')
                os.system('sudo shutdown now')

These lines make it possible to safely shutdown the Raspberry Pi by choosing s
If you have read my story on making an on-off button for your Pi you now have 2 ways to shut the Pi down.

That's all folks !!!

How to start the program.

After the Raspberry Pi has booted open a terminal window and type:

python3 radio2.py



Your screen will look like the picture above.

Autostart the program.

Maybe you do not want to start the program manually all the time. I surely don't. So the option is to start the internet radio program automatically when booting the Raspberry Pi.

This program has to be started after the Raspberry has completely booted and started its Graphical environment. To do this we need to alter the autostart file.

Searching the internet you can find numerous ways to autostart a Python program when booting the Raspberry. All these solutions have the same problem. The program starts but no terminal window is automatically opened. So there is no way you can choose the radio station or whatever.
So we need to have a specific way to autostart the program and open it in a terminal window. This is a bit more complicated.

Just follow the next steps and you´ll be good.

We´ll start with writing a simple script that will start the python program. Open again the terminal window and make sure you are working in the /home/pi directory by typing the following command:

cd ~

Next step is to write the script. Open the Nano editor with:

sudo nano startpy.sh

This will open the ditor and creates a new file called startpy.sh
Type in the following commands or copy and paste the next lines in:

#!/bin/bash
echo "starting internet radio"
python3 /home/pi/radio2.py

Close the editor with CTRL-X and answer yes to the question wether to save the file. Also check if it is saved with the right name: startpy.sh

Now this bash file can not be started without telling the OS that it should be an executable script. You can do that by typing the following command:

chmod +x startpy.sh

So if everything went well we now have an executable script that will start our radio2.py program. Let's try this by typing the following command in the terminal window:

./startpy.sh

The screen will briefly display starting internet radio and then the screen will clear and the radio2.py screen will be displayed. Don't forget to type the point and slash at the beginning of the command.
If this does not work carefully check the previous steps.

You can end the radio2.py program (like any Python program) by pressing CTRL-C.

The onlything left to do now is to make sure that the program starts when the Raspberry Pi boots. So in the terminal window type the following command:

sudo nano ~/.config/lxsession/LXDE-pi/autostart

The Nano editor opens and displays the autostart file which looks (in my case) like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi

Now add the following line to the bottom:

@lxterminal -e /home/pi/startpy.sh

This line makes sure a terminal window is opened and startpy.sh is executed which in turn starts radio2.py in the terminal window !!!

The complete file will then look like:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@lxterminal -e /home/pi/startpy.sh

When done press CTRL-X to stop the editor and make sure you answer yes to the question wether the changes must be saved. Also like usual check if the right filename is used at saving: ~/.config/lxsession/LXDE-pi/autostart

Finished !!!!

You can now reboot your Raspberry Pi with the command:

sudo reboot now

Or reboot from the menu.

The Pi will shutdown / restart and when the GUI is started a terminal window will open in which the Internet Radio program will run.

Stand alone version

We now have a full working internet radio that allows you to choose from several broadcasters. However you still need a screen and keyboard to control it. This can be done through your computer with VNC. This means that your computer should be on.

What I want is an Internet Radio that works without a screen, mouse or keyboard and where you can choose the stations by preset buttons just like a normal radio. And that is what we are going to do in the next part.

Till then: have fun

Luc Volders