Streaming lullabies with a Raspberry Pi Zero
Script Development
You can run MPlayer in a script with only a single line, but a scheduling solution will require a bit more. Listing 6 is an example of a streaming script that will play a specific stream, as well as calculate the time it should be stopped. The last line of this script updates the timer.txt
file with the date and time that the stream should be stopped.
Listing 6
Streaming Script stream1.sh
The solution has several moving parts (see Tables 1 and 2), but it is essentially held together by a few Cron entries.
Table 1
Streaming Scripts
Script | Task | Called by |
---|---|---|
|
Plays a stream |
|
|
Plays a stream |
|
|
Plays a stream |
|
|
Plays a stream |
|
|
Plays MP3 file |
|
|
Plays MP3 file |
|
|
Calls all stop scripts |
|
|
Shows which of our tasks are running |
|
|
Kills |
|
Table 2
Control Scripts
Script | Task |
---|---|
|
Creates shutdown trigger file |
|
System constants |
|
System constants |
|
Plays default white noise |
|
Calculates when stream should be stopped |
|
Takes top item from queue |
|
Adds task to queue |
|
Queue of which scripts are to be scheduled |
|
Date and time when music should stop |
The taskrunner
script must be run by the pi user every minute; thus, the web page is not very responsive when selecting a new stream. Considering the nature of the streaming solution, however, this frequency is acceptable.
The goquiet
script should also run every minute. This script will compare the time from the timer.txt
file against the system time. When this time is surpassed, it will stop any processes that are streaming or playing MP3 files.
Just like the playing.html
page, the timer.txt
and queue.txt
files and the logs
directory must be writable for world (i.e., chmod 777
).
The final crontab
entry is for the root user. The command looks for a trigger to shutdown the computer. The command uses the find
utility to search for the trigger and, upon finding it, removes the file and executes the shutdown:
find /tmp -name shutdown.now \( -exec /bin/rm {} \; -exec /sbin/shutdown \; \)
This is not the most secure solution for a production machine, but it should be OK for a tiny home streaming device.
To test and support a headless solution, I needed to know when processes were running during development. There may be an easier method for seeing what processes are running, but, not knowing it, I came up with a creative solution.
The playing
shell script queries the running tasks I am interested in and then dynamically generates the HTML to display those processes to the user using the browser.
Note: It is important that you create a file called playing.html
in the HTML input directory with permissions that will allow it to be overwritten; all other files can, and probably should, remain read only.
You can find the full source code on the Linux Magazine FTP site [7].
Video Problem Explained
Installing the operating system is really easy, but the Raspberry Pi Zero is slightly less friendly if you make a mistake. The full-size Raspberry Pi has a few LEDs to help you see what is going on. When I first booted my Raspberry Pi Zero, I didn't see anything on my screen because of a problem with my SD card. Raspberry Pis, unlike standard PCs, require a driver for anything to be displayed on the screen. I replaced my old SD card with a new one and everything worked out fine.
A Tale of Two Audio Players
Why am I installing the mpg123 program in addition to MPlayer? Why not just use MPlayer for MP3 files as well as streams? This solution was actually developed on an older Raspberry Pi before it was installed on the Raspberry Pi Zero W. At the time I was developed this technique, I was having problems playing MP3s using MPlayer, so I included mpg123 with the solution.
Infos
- Raspbian download: https://www.raspberrypi.org/downloads/raspbian/
- Installing Raspbian: https://www.raspberrypi.org/documentation/installation/installing-images/linux.md
- Installing Raspbian from Windows with Win32DiskImager: https://www.raspberrypi.org/documentation/installation/installing-images/windows.md
- Audio pHAT: https://shop.pimoroni.com/products/phat-dac
- Solderless headers: https://shop.pimoroni.com/products/gpio-hammer-header
- Forwarding audio output: https://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install
- Listings in this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/
« Previous 1 2 3
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.