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
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
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.