Home | Spelletjes | Uitvindingen | Gps | Scouting | Meteo | Computers

Ict / Internet Radio Using The Raspberry Pi Menu

Build your own very simple to use Internet Radio

Level: Experienced

My mother is 97 years old, she is not only almost blind, she also has increasing difficulties to control her radio set. What she needs is a very simple one-button radio, which she can push if she does not like the station that is on now. Pushing that button will switch to the next pre-programmed radio station.

What you need

  • A complete Raspberry Pi
    • I used the model B+
    • Power supply and cable
    • A HDMI cable and compatible monitor. You will need that during install only
    • 16 Gb micro-SD card (8 Gb will do too)
    • Extra: ADAFRUIT LCD+keyboard 1115
  • Internet connection (by cable or wifi-dongle)
  • A PC loudspeaker set: Volume and Power should also be simple to use
  • A box to fit it all in, I built it myself

Install the Raspberry Pi Operating system

You will need some basic Linux skills to be able to make the Raspberry Pi to operate. Write a Linux image on you SD-card, plug it into the Raspberry Pi, connect the monitor with the HDMI cable, and plug in the power-supply. Follow the instruction on the screen.

I will not go into detail on that, it is documented well on raspberrypi.org. You can do with a cli-only version, a desktop is not necessary for this application.

Get back here after you made Linux run on you Pi.

Raspberry Pi with LCD display

Hardware

On the picture you see the Raspberry Pi with the LCD display attached to it. Note that between the LCD display and the metal connectors a piece of isolating tape must be attached to prevent a short circuit. A power-cable, a loudspeaker cable and a network cable are connected.

Note the four buttons, wires will be soldered to connect with the external buttons on the front panel.

Turn the copper screw (on the blue potentiometer) until the characters become visible on the LCD screen.

Installing the tools

When you want to run an Internet Radio means you have to have some software that can receive and play the stream that is sent by one of the very many radio stations on the Internet.

Linux provides many solutions to that, I chose to use mpd, the Music Player Daemon.

Mpd is capable to open a music-file or music-stream from somewhere on the Internet, and play it. Mpd has no fancy interface, in fact it has no controls at all. It runs in the background, continuously and invisible to the user. That is why it is called a daemon, no-one can see it but it is there.

You will need a separate piece of software to be able to control this daemon. This tool is called a client. Mpc, the tool that we are going to use is such a client. There are many clients available, see the listing here. Some clients are very fancy, they have the looks of a modern juke-box, and some are sober.

To install type sudo apt install mpc mpd

Mpc can only be operated from the command line. Nevertheless it is a very powerful tool. For instance, mpc play will tell mpd to play the content of a selected play-list, and mpc stop will cause mpd to stop playing.

The script that I wrote will call mpc together with an argument, according to the specific button pressed, and will send the textual output to the LCD display.

Configuring

Your Raspberry Pi should now be up and running. From now on it is save to disconnect the HDMI, and do all work from my desktop computer through a ssh-connection. I use ssh pi@raspberrypi from my Linux desktop, or you can use putty if you work from a Windows PC.

Mpd

Normally the working files for mpd are stored in /var/lib/mpd. It is easier for maintenance to put them in the home directory. This is done by typing the following commands:

mkdir -p /home/pi/mpd
sudo cp -a /var/lib/mpd/music /home/pi/mpd/
sudo cp -a /var/lib/mpd/playlists /home/pi/mpd/
sudo chown -R pi:pi /home/pi/mpd

Then, edit mpd's configuration file by typing sudo pico /etc/mpd.conf and modify the following lines like this:

music_directory       "/home/pi/mpd/music"
playlist_directory    "/home/pi/mpd/playlists"

Restart mpd: sudo service mpd restart

Playlist

Search around the Internet for your radio-stations. Then create your playlist by typing pico /home/pi/mpd/playlists/stations.m3u and enter something like

http://streams.greenhost.nl:8080/klassiek
http://74.208.197.50:8020/;stream.mp3

for two stations, and so on. Add or change these for your favorite stations. Restart mpd: sudo service mpd restart and test:

mpc ls
mpc load stations
mpc playlist       # should list both stations
mpc play 1
mpc play 2

should play the first and then the second radio station in the playlist. Stop playing by typing mpc stop.

Software

Download Internetradio and copy to /usr/local/bin/internetradio:

sudo cp ~/Downloads/Internetradio /usr/local/bin/internetradio
chmod +x /usr/local/bin/internetradio

and run it: /usr/local/bin/internetradio. Look at the display for the software to report itself.

The functions of the buttons are:

  • button 1: Play next station
  • button 2: Play previous station
  • button 3: Toggle pause / play
  • button 4: Play first station

Hardware

Finally you have to build a case, and do the wiring. In the picture below you see the back with the LCD panel and the switches. The Raspberry Pi will be connected to the LCD panel shortly after taking this picture. Note the soldered connections on the back of the switches on the LCD panel.

The back of the Internetradio and the wiring

And here you can see the front of the radio. Note the big labels near the buttons to help the visually disabled radio-listeners.

The front of the Internetradio

Extra's

If you already have a server with loudspeakers attached to that, you can use that one to play your Internet radio-stations. Mpc and mpd can run on different machines (preferably within the same local network). Simply install mpd on that server (Linux and Windows binaries available), and edit the internetradio python-code on your Raspberry Pi. Change the word 'localhost' for the ip-address of your server (something like '192.168.1.100'). It should work right away!

You can open port 22 on your internet router to be able to remotely login on the internetradio in case of problems, or on demand to play one of my mothers favourite CD's that I put in the music-directory.
(Note that you have to do some ssh-server hardening; how to do that is out of the context of this article)

Starting and stopping

This is done in user pi's crontab. At 8 in the morning internetradio is started, and at 23 in the night stopped. Also, every hour a test for a running internetradio-process is done, if not it will be restarted.

# crontab
# Enter these lines after 'crontab -e'
# m    h dom mon dow    command
00   8-22  *   *   *     ps -ef | grep internetradio | grep -v grep || /usr/local/bin/internetradio 
00   23    *   *   *     ps -ef | grep internetradio | grep -v grep | kill $(awk '{ print $2 }') ; mpc stop

An example: If you want to hear the news on the third station in the playlist at 6pm every weekday, add this line in the crontab:

00   18    *   *   1-5     mpc play 3

Note that the display does not change accordingly!

Known problems

Crash
Very occasionally the system just stops. The script is still active, and the music continues to play but the buttons are unresponsive. I cannot reproduce it, and when it does I stop the script (kill <process_id>) and restart the daemon (sudo service mpd restart). Finally, start internetradio up again.
Volume
The volume of different radio stations vary. One can be soft, the next one can be very loud. I do not have a solution for that.
Display update
The display is only updates when a new station is selected. When a station changes its program, this information is not updated on the display. Solving this would require intensive re-coding.
Display does not switch off
When internetradio is stopped, the display is not erased, neither is the back-light switched off. The first problem can be solved by launching a program that erases the display; the second cannot be solved because the back-light cannot be controlled by software (afaik). Just take the power off...
Bad coding
Yes I am well aware of that. Actually this is my very first Python script. I put this program together from various bits of code that I found on the Internet. Usually I code in Perl, but I could not get the LCD-display to work in Perl.
Also, all the output is in Dutch; probably some translation must be done.
You are invited to clean up the code and send me a copy :)

Commercial info

I bought the Raspberry Pi and parts at Conrad.nl. Conrad sells the display completely assembled an ready to use. On their site you will find four pdf-files with programming examples.

Geef een reactie:

Mijn naam:
Bericht:
Ik vind dit:
Geef code in: Captcha
hans puts - 22-02-2018
leuk bedacht en uitgevoerd!
nazz - 03-10-2017
Hoi heb een beetje veel hulp nodig.

Ik ben geen nerd heb een piplate lc 16x2 met 5 druk toestsen wil mijn eigen internet radio stations beluisteren. Ik kom er niet uit... kun je een stappen plan maken. Het lukt mij niet op de LCD stations zichtbaar te maken en aan de hand van de toetsen de volgend station ..

Nazz

Antwoord: De Pyplate is anders dan de Adafruit LCD. Je moet zelf uitzoeken hoe je dit display aanstuurt en dan de code aanpassen

Departementen