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
-
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.