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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.