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
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.