Streaming lullabies with a Raspberry Pi Zero

pHAT DAC

The Raspberry Pi is an amazing device that supports Bluetooth and WiFi and is small enough that you might misplace it if you have a messy desk. The only problem I had was that my speakers don't support Bluetooth or WiFi, and the Raspberry Pi Zero is missing the audio jack. I corrected the situation by purchasing a pHAT – a Raspberry Pi add-on board (HAT) for the Pi Zero – to add on a stereo jack kit [4].

The kit requires soldering on the female headers. It only takes a few minutes, but if you are not comfortable with a soldering iron, you can purchase headers that can be attached without soldering [5]. The pHAT setup requires some simple modifications [6] of a few text files to forward the audio output to the pHAT.

Software

The three software packages that need to be installed on the computer are apache2, mpg123, and mplayer. These packages are installed in the standard way using apt-get from the command prompt.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mplayer2 mpg123 apache2

Verifying that Apache works is as simple as passing in the IP address of the Rasp Pi or entering http://127.0.0.1 in the address bar of your web browser if it is running on the same machine.

The software works as expected, but Apache does need a small configuration change. My streaming solution requires that my HTML page can also run scripts, and for that, I need to enable the Common Gateway Interface (CGI) module. I can enable CGI on the Rasp Pi with a single command:

sudo a2enmod cgi

Once you run this command, you will need to restart Apache to enable the change

service apache2 restart

or use the alternative (and recommended):

systemctl daemon-reload

To test whether CGI is properly enabled, I copied the contents of Listing 1 to the hello.cgi file in the /usr/lib/cgi-bin directory. Once the file is in the directory, simply point your web browser to it, as shown in Figure 3. If your scripts are displayed instead of run, you have a problem with enabling CGI.

Listing 1

Testing CGI

 

Figure 3: Testing the Apache CGI configuration.

GUI Development

All you need is a small HTML file to produce the front end for the shell scripts. It is really neat to see what you can do with just a few lines of HTML. The index.html file in Listing 2 displays the heading Music selection, along with 10 buttons representing different musical choices.

Listing 2

Music Selections

 

The cascading style sheets (CSS) file in Listing 3 was created to eliminate browser-specific markup, making it possible to create web pages that display both a high level of sophistication and consistency across all browsers. To change the look and feel completely, you can just change the style sheet, but the main benefit of using CSS is that it makes the HTML files smaller and easier to understand.

Listing 3

CSS File

 

The next step is to assign a CGI script to each of the buttons:

<div class="button-wrapper">
<a class="button control-button" href="cgi-bin/poweroff.cgi">power off</a>
</div>

You can use your favorite scripting language, whether it be Perl, Python, or PHP. Simply populate the /usr/lib/cgi-bin directory with your scripts.

My solution was coming together nicely, but I encountered permission problems when trying to run MPlayer and mpg123. I encountered difficulties running MPlayer when it was called from Apache (as the www-data user). Rather than granting all permissions all over the place or adding users to groups that don't make any sense, I decided to have a real user account play the music.

To get around the permission issues, I changed my method of running these programs. Instead of running them directly, I created my own primitive scheduler to run the selected stream or MP3. Listing 4 is one of the CGI scripts created to call my task scheduler with a shell script that will later be run by the pi user.

Listing 4

CGI Script

 

The taskrunner script (Listing 5) uses the head utility to pull off the top item from the queue and run it in the background; a few seconds later, it schedules the white noise script. The noise script schedules the white noise to play after 30 minutes of the previous item, but only if the button pressed is not the white noise button.

Listing 5

taskrunner.sh

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Streaming with Icecast

    For live Internet radio, you need a streaming server. We’ll show you how to get started with Icecast, an open source streaming alternative for Linux.

  • Multimedia

    Enjoy music and video on your Fedora machine.

  • Firefly Audio Streaming

    The Firefly Media Server makes streaming music and Internet radio onto your home network for iTunes or Banshee clients as easy as pie.

  • Deluxe Web Radio

    An LCD in an unusual format, a self-designed housing, and matching software make a simple Raspberry Pi web radio the perfect centerpiece for any living room.

  • Stream Processing 101

    Batch processing strategies won't help if you need to process large volumes of incoming data in real time. Stream processing is a promising alternative to conventional batch techniques.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News