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

./players/stream1.sh

Plays a stream

./cgi-bin/stream1.cgi

./players/stream2.sh

Plays a stream

./cgi-bin/stream2.cgi

./players/stream3.sh

Plays a stream

./cgi-bin/stream3.cgi

./players/stream4.sh

Plays a stream

./cgi-bin/stream4.cgi

./whitenoise/stream5.sh

Plays MP3 file

./cgi-bin/stream5.cgi

./whitenoise/stream6.sh

Plays MP3 file

./cgi-bin/stream6.cgi

./utils/stopall.sh

Calls all stop scripts./utils/stopmplayer.sh./utils/stopmpg.sh./utils/stopwhite.sh"

./cgi-bin/stopall.cgi

./utils/playing.sh

Shows which of our tasks are running

./cgi-bin/playing.cgi

./utils/nonoise.sh

Kills noise.sh task which plays white noise after 30 minutes

./cgi-bin/nonoise.cgi

Table 2

Control Scripts

Script

Task

./cgi-bin/poweroff.cgi

Creates shutdown trigger file

./cgi-bin/config.cgi

System constants

./variables.sh

System constants

./utils/noise.sh

Plays default white noise

./utils/setshutdown.sh

Calculates when stream should be stopped

./utils/taskrunner.sh

Takes top item from queue

./utils/taskscheduler.sh

Adds task to queue

./queue.txt

Queue of which scripts are to be scheduled

./timer.txt

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.

The Author

Christopher Dock is a senior consultant at T-Systems on site services. When he is not working on integration projects, he likes to experiment with Raspberry Pi solutions and other electronics projects. You can read more of his work at http://blog.paranoidprofessor.com

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Streaming with Icecast

    For live Internet radio, you need a streaming server. We’ll show you how to get started with Icecast, an open source streaming alternative for Linux.

  • Multimedia

    Enjoy music and video on your Fedora machine.

  • Firefly Audio Streaming

    The Firefly Media Server makes streaming music and Internet radio onto your home network for iTunes or Banshee clients as easy as pie.

  • Deluxe Web Radio

    An LCD in an unusual format, a self-designed housing, and matching software make a simple Raspberry Pi web radio the perfect centerpiece for any living room.

  • Stream Processing 101

    Batch processing strategies won't help if you need to process large volumes of incoming data in real time. Stream processing is a promising alternative to conventional batch techniques.

comments powered by Disqus
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.

Learn More

News