Baby Monitor Music

So I seem to have become obsessed with the baby monitor,  and I figured that the code in baby monitor revisited is just goddamn awful. I have decided the following improvement are required:

(a) All music should be installed on a usb drive (for easy updating)

(b) There should be the ability to rescan the usb drive for new music and create a web page with links to that music.

So assuming you have read the baby monitor revisited blog, I did the following:

1 Prepare The USB Key

Create a top level folder on a usb key called Music and put your nursery rhymes on there.

Then plug the usb key into the raspberry pi.

Then logon to the raspberry pi and type

ls -l /dev/disk/by-uuid

You should see something like this

 ls -l /dev/disk/by-uuid
total 0
lrwxrwxrwx 1 root root 15 Jun 30 20:17 0EF2-CA4B -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 10 Jun 30 20:17 6791-5359 -> ../../sda1
lrwxrwxrwx 1 root root 15 Jun 30 20:17 e093a5bb-b180-4f87-9d60-467b3e79811d -> ../../mmcblk0p2

then type sudo nano /etc/rc.local and add the following line just before the exit 0 line at the end

mount /dev/sda1 /media/usb -o uid=pi,gid=pi

and press control-X to save

You can reboot the pi by typing sudo reboot. Once you log back in and run a df command you should not see an entry for /media/usb

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 7508760 5781400 1367872 81% /
devtmpfs 185788 0 185788 0% /dev
tmpfs 190076 0 190076 0% /dev/shm
tmpfs 190076 8656 181420 5% /run
tmpfs 5120 4 5116 1% /run/lock
tmpfs 190076 0 190076 0% /sys/fs/cgroup
/dev/mmcblk0p1 64456 20992 43464 33% /boot
/dev/sda1 61038624 50977824 10060800 84% /media/usb
tmpfs 38016 0 38016 0% /run/user/1000

 

Now we want to create a symblic link from /var/www/html/Music to where our music is actually stored. This will make life easier when creating a script to list the music

sudo ln -s /media/usb/Music /var/www/html/Music

now if you execute

ls -l /var/www/html/Music

you should see the music on you usb drive

Its important that the music files dont contain any spaces in the names, so here is a handy script that will strip out the names

#!/bin/bash
# Convert filenames to lowercase
# and replace characters recursively
#####################################

if [ -z $1 ];then echo Give target directory; exit 0;fi

find "$1" -depth -name '*' | while read file ; do
 directory=$(dirname "$file")
 oldfilename=$(basename "$file")
 newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
 if [ "$oldfilename" != "$newfilename" ]; then
 mv -i "$directory/$oldfilename" "$directory/$newfilename"
 echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
 #echo "$directory"
 #echo "$oldfilename"
 #echo "$newfilename"
 #echo
 fi
 done
exit 0

Save this script as renamefiles.sh in /home/pi and then execute it as follows:

sudo ./renamefiles.sh /media/usb/Music

This will strip out the spaces from the file names.

2 Create the script to scan the music.

Create a php script in /var/www/html called createmusiclist.php with the following lines:

<?php

function isCommandLineInterface()
{
 return (php_sapi_name() === 'cli');
}

function MakeFileList()
{
 $text="";
 $path = '/var/www/html/Music';
 $files = scandir($path,1);
 $files = array_diff(scandir($path), array('.', '..'));
 $myfile = fopen("musiclist.html", "w");
 $text="<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br>\n";
 fwrite($myfile,$text);
 $text="<HTML>\n<HEAD>\n<TITLE>Music List</TITLE>\n";
 fwrite($myfile,$text);
 $text="<meta http-equiv=\"refresh\" content=\"300\">\n";
 fwrite($myfile,$text);

 $text="</HEAD>\n<BODY>\n";
 fwrite($myfile,$text);

 $text="<H1 style=\"text-align: center;\">Music Page</H1>";
 fwrite($myfile,$text);
 $text="<H1 style=\"text-align: center;\">Music On USB Stick</H1><br>\n";
 fwrite($myfile,$text);

 $text="<a href=\"./killmusic.php?run=true\">Stop Music</a><br><p>\n";
 fwrite($myfile,$text);

 foreach ($files as &$value)
 {
 $text="<a href=\"./playfile.php?run=true&argument1=$value\">$value</a><br>\n";
 fwrite($myfile,$text);
 }
 $text="</BODY>\n</HTML>\n";
 fwrite($myfile,$text);
 fclose($myfile);
}

if (isCommandLineInterface()==TRUE)
{
 MakeFileList();
}
else
{
 if ($_GET['run'])
 {
 MakeFileList();
 header("Location: ./musiclist.html");

 }
}
?>

then type

sudo chown www-data:www-data createmusiclist.php

When executed, this script will create a file called musiclist.html which contains links to all of the music that someone can play.

Then create a web page called /var/www/html/music2.html with the following content:

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 <TITLE>Music Page</TITLE>
<meta http-equiv="refresh" content="300">
 </HEAD>

 <BODY>
<h1 style="text-align: center;">Music Page</h1>

<a href="./musiclist.html">Listen To Music</a><br>
<p>
<a href="./createmusiclist.php?run=true">Rescan Music On USB Drive</a><br>
</BODY>
</HTML>

again make sure this page is owned by www-data by typing:

sudo chown www-data:www-data musc2.html

3 Sit Back And Play Music

You should now able to access that web page, and either seelect the Listen to Music option or rescan Music on USB drive option.

If either of the pages, dont work, then remember that I have designed all of the scripts so that they can be run from the command line for debugging purposes or from a webpage.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

sdfsdfsdfd

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s