This is the third installment in a series of 3 stories on how to build a cheap internet radio with a Raspberry Pi Zero.
The first story described what accessories you need and how to write a small Python program that actually playes one radio station. You can find that story here: https://lucstechblog.blogspot.com/2018/09/raspberry-internet-radio-part-1.html
In the second story I expanded the Python program to give it a nice looking menu that gives the possibillity to choose from a number of radio stations. You can re-read that story here: https://lucstechblog.blogspot.com/2018/09/raspberry-pi-internet-radio-part-2.html
An intermezzo was story 2B that showed how to find MP3 Broadcasts on the internet that you can incorporate in the program made in story 2. Read it here: https://lucstechblog.blogspot.com/2018/09/raspberry-pi-internet-radio-part-2-b.html
In this last part we are going to make the Internet Radio headless.
This means that it is a stand-alone system. The Pi Zero will start when you press a button and shuts-down again when you press a button. Next to that there will be buttons for choosing the radio stations. No monitor, keyboard or mouse will be needed to operate the internet radio.
To achieve this there are several steps to be made:
1) the hardware
2) the Python Internet Radio program
3) assure that the Python Internet Radio program is started automatically at booting the Pi
The hardware
I am giving you the breadboard layout for an Internet Radio control with an on/off button and 2 buttons for Radio stations. Just have a good look at the setup and you can expand this easily.
First let us have a look at the Raspberry I/O pins.
GPIO3 (pin 5) has a special ability. When you momentarily connect it to ground the Pi Zero will boot. Now that is a nice feature we happily will use to set the Internet Radio ON.
Several I/O pins have multiple functions. So best is (in this case) not to use them for our Internet Radio. That is no problem as there are my pins left that can be used. I will use GPIO20 (pin 38) and GPIO21 (pin 40) as the pins that choose the radio stations.
I used one extra GPIO being GPIO18 (pin 12) to attach through a 1K delimiting resistor to a led.
This led will go ON as the Internet Radio program is running and OFF when the Internet Radio program stops.
So the above breadboard layout shows you how to wire the buttons. As there are many I/O pins freely available you can expand this with as many buttons as you like.
Above you can see my setup in real life. I just altered one thing.
The button in the picture is not an ordinary push button. It is a pushbutton with a build-in led. So what I did is connect the button to GPIO3 and connect the LED part to GPIO18. You can use one ground lead that can be attached to the two connectors on the switch. Don't forget to attach a 1k delimiting resistor to the LED. If you don't you will damage your Raspberry PI
This way I have a switch with a build in LED that goes on when I start my internet radio and the LED goes off when I shut down the Raspberry Pi. Neat !!!
The Software
To install the software I always enable VNC on the Raspberry Pi and download the VNC viewer on my PC (https://www.realvnc.com/en/download/vnc/windows/) so I can install everything remotely and I do not need to attach an extra keyboard, mouse and monitor to the Raspberry.
Start with installing and updating the Raspberry OS as described here:
https://www.raspberrypi.org/documentation/installation/installing-images/README.md
Make sure Real-VNC is active and connect to your PC. Not only can you operate the Raspberry from your PC but as a bonus it is easy to copy files between the PC and Raspberry by the normal copy and paste commands.
When the Raspberry has booted start with opening a terminal window by clicking on its icon on the top-left of the raspberry window. That's where the arrow is pointing.
Now first make sure you are working in the home/pi directory by typing the following command:
CD ~
Next open the nano editor and make sure it starts a new file with the name radio3.py by giving the following command:
sudo nano radio3.py
When the nano editor has started copy the following program from this weblog and paste it into the editor.
#!/usr/bin/python3
import os
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.OUT)
SOUND = 'mpg123 http://icecast.omroep.nl/radio1-bb-mp3 >/dev/null 2>&1 &'
SOUND5 = 'mpg123 http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws >/dev/null 2>&1 &'
os.system('clear')
os.system(SOUND)
def station1(channel):
os.system('sudo killall mpg123')
os.system(SOUND)
def station2(channel):
os.system('sudo killall mpg123')
os.system(SOUND5)
def stationq(channel):
GPIO.output(18, GPIO.LOW)
os.system('sudo killall mpg123')
os.system('sudo shutdown now')
GPIO.add_event_detect(20, GPIO.FALLING, callback=station1, bouncetime=300)
GPIO.add_event_detect(21, GPIO.FALLING, callback=station2, bouncetime=300)
GPIO.add_event_detect(3, GPIO.FALLING, callback=stationq, bouncetime=300)
while True:
GPIO.output(18, GPIO.HIGH)
When everything is pasted in the editor close the Nano Editor with CTRL-x and make sure the file is saved by answering yes when asked and look if the right filename is used.
A closer look at the Internet Radio program
The import commands make sure the necessary libraries are present.
GPIO.setmode(GPIO.BCM)
This command makes sure that we can use the GPIO names instead of the pin numbers.
Next the GPIO's are defined as input with a pull-up resistor except GPIO18 on which the led is attached.
SOUND = 'mpg123 http://icecast.omroep.nl/radio1-bb-mp3 >/dev/null 2>&1 &'
The SOUND variables are filled with the links of the MP3 streams and the & at the end of the command makes sure that the MPG123 playes is opened in the background so that the radio3.py program keeps the focus and does not open a terminal window.
Next the screen is cleared and the radio station that is defined in the variable SOUND is activated. This way the station strats playing direct when the program is started.
The def station routines first make sure anything that MPG123 is playing is shutdown and then open the new choosen radio station defined in the accompanying SOUND variable.
The last def routine called stationq is activated when the button attached to GPIO3 is pressed. This will shut down the Raspberry Pi Zero. This is the ON/OFF button that has been described in this story:
GPIO.add_event_detect(20, GPIO.FALLING, callback=station1, bouncetime=300)
These commands test wether a button has been pushed, and incorporate a waiting time to stabilise button bouncing.
The last commands:
while True:
GPIO.output(18, GPIO.HIGH)
Are an endless loop and make sure the program keeps running.
Expanding.
Just as easy as expanding the hardware with more buttons is it to expand the software.
Just add some SOUND variables. Add some GPIO.add_event_detect commands with the right GPIO numbers and attach them to some extra def station1(channel): routines. This should not give you any trouble.
Autostarting the program.
This is the last step. We have to make sure the Internet Radio program will start immediately after the Raspberry Pi has booted. If that would not be the case we would need a screen, mouse and keyboard to start the program and that is just the thing we want to avoid.
Before we make things permanent test if the program works as expected. Type the next command in the terminal window:
python3 radio3.py
The default radio station should be playing immediately. Pressing one of the pre-set buttons will change the radio station and pressing the ON/OFF button will shut the Raspberry Pi Zero down.
Re-booting the Pi is now easy as you just have to press the ON/OFF button. Remember what I said previously: that connecting GPIO3 shortly to ground will boot the Raspberry.
The Pi will re-boot but the radio3.py program will not start automatically. That is the last step we need to implement.
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 ~/.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 alter the autostart file so that it looks like the next lines:
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
python3 /home/pi/radio3.py
@xscreensaver -no-splash
@point-rpi
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 the radio3.py program is automatically started in the background. No terminal window will be opened and just the plain desktop is shown. If everything goes well the default radio station is playing. Sometimes that does not happen, or it just plays for a few moments and then stops. No problem just push one of the pre-set radio buttons and that station begins to play.
Pressing the ON/OFF button will shut down the Pi and pressing it again will re-boot the Pi and the Internet Radio program will instantly be working again.
Shutting down manually.
If you need to make alterartions to the program like adding radio stations or altering stations you can shut down the program manually even if it is running in the background.
Open the terminal window and type the following command
sudo killall python3 mpg123
This kills any running Python3 program and also kills any MPG123 stream.
Expanding further
Like said above it is easy to expand the hardware with more butoons and it is equally easy to expand the software to add more radio stations.
You can expand further.
- Add some MP3 music files to your SD card and dedicate a button to randomly playing songs.
- Dedicate 2 buttons and call them next and previous to step through your music files.
- Let the internet Radio play songs from your NAS
- Alter the software so that it can not only play headless but that it can also be controlled in a terminal window
- Add an LCD screen or Oled screen that displays what's playing.
- Exchange the pushbuttons for a rotary switch
This way you could build a complete media system.
Casing
Off course you can build any casing to your liking even from cardboard. You could also check the flea markets and find some vintage radio in which you can build-in the Pi-Radio.
Luckily for me I have a 3D printer so I designed a simple box and put my radio in it.
Till next time. Have fun
Luc Volders