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
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.