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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.