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)