Baby Monitor Revisited

So I was thinking that the baby monitor that I talked about in my earlier blog was a bit generic and needed spicing up a bit so how about the ability to play a few nursery rhymes – yep that will do.

This is real quick and dirty solution but it also shows how to get a web page to call a php script which calls a shell script (and from that great things are born) 🙂

I didnt want the music to play in the web page, I wanted to play on speakers connected to the raspberry pi in the baby’s room. I also didnt want an additional power supply. I found these speakers on ebay for the grand sum of £2.90. They have a usb connector (for power) and a 3.5mm jack plug – perfect for the raspberry pi

usbspeakers

The first step is to get a webserver installed, so without further ado, over to the people who did it first.

https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md

Next I installed php

sudo apt-get -y install php5-common php5-cgi php5

then I installed an mp3 player (mpg123) as follows:

sudo apt-get install mpg123

Next I copied a bunch of nursery rhymes into /var/www/html/newmusic and made sure they were all owned by www-data by executing the follwing command

cd  /var/www/html/newmusic
chown www-data:www-data *.mp3

lsal

My files were numbered from 01 to 83 (more on this later)

After that I created a file called music1.html in /var/www/html

The logic flow is pretty simple in that you click on a song in the web page and it called a php script (playfile.php) with the name of the song as a parameter.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 <HTML>
 <HEAD>
 <TITLE>Nursery Rhymes Page</TITLE>
 </HEAD>

<BODY>
 <h1>

<a href="./killmusic.php?run=true">Stop Music</a><br>
 <p>
 <a href="./playfile.php?run=true&argument1=songs0183.mp3">All Songs</a><br>
 <p>
 <a href="./playfile.php?run=true&argument1=songs0109.mp3">Songs 01-09</a><br>
 <a href="./playfile.php?run=true&argument1=songs1019.mp3">Songs 10-29</a><br>
 <a href="./playfile.php?run=true&argument1=songs2029.mp3">Songs 20-39</a><br>
 <a href="./playfile.php?run=true&argument1=songs3039.mp3">Songs 30-39</a><br>
 <a href="./playfile.php?run=true&argument1=songs4049.mp3">Songs 40-49</a><br>
 <a href="./playfile.php?run=true&argument1=songs5059.mp3">Songs 50-59</a><br>
 <a href="./playfile.php?run=true&argument1=songs6069.mp3">Songs 60-69</a><br>
 <a href="./playfile.php?run=true&argument1=songs7079.mp3">Songs 70-79</a><br>
 <a href="./playfile.php?run=true&argument1=songs8083.mp3">Songs 80-83</a><br>
 <p>
 <a href="./playfile.php?run=true&argument1=01_A_Cat_Came_Fiddling_Out_Of_A_Barn.mp3">Song 01</a><br>
 <a href="./playfile.php?run=true&argument1=02_HickoryDickoryDoc.mp3">Song 02</a><br>
 <a href="./playfile.php?run=true&argument1=03_Blow_Wind_Blow!_And_Go_Mill_Go!.mp3">Song 03</a><br>
 <a href="./playfile.php?run=true&argument1=04_Dr._Foster_Went_To_Gloucester.mp3">Song 04</a><br>
 <a href="./playfile.php?run=true&argument1=05_The_North_Wind_Doth_Blow.mp3">Song 05</a><br>
 <a href="./playfile.php?run=true&argument1=06_Sally_Go_Round_The_Sun.mp3">Song 06</a><br>
 <a href="./playfile.php?run=true&argument1=07_Old_King_Cole.mp3">Song 07</a><br>
 <a href="./playfile.php?run=true&argument1=08_There_Was_An_Old_Woman.mp3">Song 08</a><br>
 <a href="./playfile.php?run=true&argument1=09_Here_We_Go_Round_The_Mulberry_Bush.mp3">Song 09</a><br>
 <a href="./playfile.php?run=true&argument1=10_Peter,_Peter,_Pumpkin_Eater.mp3">Song 10</a><br>

<a href="./playfile.php?run=true&argument1=11_Lucy_Locket_Lost_Her_Pocket.mp3">Song 11</a><br>
 <a href="./playfile.php?run=true&argument1=12_Hey_Diddle_Diddle.mp3">Song 12</a><br>
 <a href="./playfile.php?run=true&argument1=13_Higgledy_Piggledy,_My_Black_Hen.mp3">Song 13</a><br>
 <a href="./playfile.php?run=true&argument1=14_Two_Little_Dicky_Birds.mp3">Song 14</a><br>
 <a href="./playfile.php?run=true&argument1=15_Goosey,_Goosey_Gander.mp3">Song 15</a><br>
 <a href="./playfile.php?run=true&argument1=16_Cackle,_Cackle,_Mother_Goose.mp3">Song 16</a><br>
 <a href="./playfile.php?run=true&argument1=17_Dance_To_Your_Daddy.mp3">Song 17</a><br>
 <a href="./playfile.php?run=true&argument1=18_One,_Two,_Three,_Four,_Five.mp3">Song 18</a><br>
 <a href="./playfile.php?run=true&argument1=19_Little_Bo-Peep_Has_Lost_Her_Sheep.mp3">Song 19</a><br>
 <a href="./playfile.php?run=true&argument1=20_Mary_Had_A_Little_Lamb.mp3">Song 20</a><br>

<a href="./playfile.php?run=true&argument1=21_Baa,_Baa_Black_Sheep.mp3">Song 21</a><br>
 <a href="./playfile.php?run=true&argument1=22_Old_Mother_Hubbard.mp3">Song 22</a><br>
 <a href="./playfile.php?run=true&argument1=23_Oh_Where,_Oh_Where_Has_My_Little_Dog_Gone.mp3">Song 23</a><br>
 <a href="./playfile.php?run=true&argument1=24_Hark,_Hark,_The_Dogs_Do_Bark.mp3">Song 24</a><br>
 <a href="./playfile.php?run=true&argument1=25_Girls_And_Boys,_Come_Out_To_Play.mp3">Song 25</a><br>
 <a href="./playfile.php?run=true&argument1=26_Wee_Willie_Winkie.mp3">Song 26</a><br>
 <a href="./playfile.php?run=true&argument1=27_I_Saw_A_Ship_A-Sailling.mp3">Song 27</a><br>
 <a href="./playfile.php?run=true&argument1=28_It's_Raining,_It's_Pouring.mp3">Song 28</a><br>
 <a href="./playfile.php?run=true&argument1=29_Incy_Wincy_Spider.mp3">Song 29</a><br>
 <a href="./playfile.php?run=true&argument1=30_Three_Blind_Mice.mp3">Song 30</a><br>

<a href="./playfile.php?run=true&argument1=31_Pussy_Cat,_Pussy_Cat.mp3">Song 31</a><br>
 <a href="./playfile.php?run=true&argument1=32_Ride_A_Cock-Horse_To_Banbury_Cross.mp3">Song 32</a><br>
 <a href="./playfile.php?run=true&argument1=33_Cock-A-Doddle-Do.mp3">Song 33</a><br>
 <a href="./playfile.php?run=true&argument1=34_Polly,_Put_The_Kettle_On.mp3">Song 34</a><br>
 <a href="./playfile.php?run=true&argument1=35_Little_Tommy_Tucker.mp3">Song 35</a><br>

<a href="./playfile.php?run=true&argument1=36_Oranges_And_Lemons.mp3">Song 36</a><br>
 <a href="./playfile.php?run=true&argument1=37_See-Saw,_Margery_Daw.mp3">Song 37</a><br>
 <a href="./playfile.php?run=true&argument1=38_Georgie_Porgie,_Pudding_And_Pie.mp3">Song 38</a><br>
 <a href="./playfile.php?run=true&argument1=39_Jack_And_Jill_Went_Up_The_Hill.mp3">Song 39</a><br>
 <a href="./playfile.php?run=true&argument1=40_Round_And_Round_The_Garden.mp3">Song 40</a><br>

<a href="./playfile.php?run=true&argument1=41_Here_Am_I.mp3">Song 41</a><br>
 <a href="./playfile.php?run=true&argument1=42_Jack_Be_Nimble.mp3">Song 42</a><br>
 <a href="./playfile.php?run=true&argument1=43_Dickery,_Dickery,_Dare.mp3">Song 43</a><br>
 <a href="./playfile.php?run=true&argument1=44_Hush-A-Bye,_Baby.mp3">Song 44</a><br>
 <a href="./playfile.php?run=true&argument1=45_I_Had_A_Little_Nut_Tree.mp3">Song 45</a><br>
 <a href="./playfile.php?run=true&argument1=46_Thiss_Little_Pig_Went_To_Market.mp3">Song 46</a><br>
 <a href="./playfile.php?run=true&argument1=47_Tom,_Tom_The_Piper's_Son.mp3">Song 47</a><br>
 <a href="./playfile.php?run=true&argument1=48_To_Market,_To_Market,_To_Buy_A_Fat_Pig.mp3">Song 48</a><br>
 <a href="./playfile.php?run=true&argument1=49_Pease_Porridge_Hot.mp3">Song 49</a><br>
 <a href="./playfile.php?run=true&argument1=50_Pop_Goes_The_Weasel.mp3">Song 50</a><br>

<a href="./playfile.php?run=true&argument1=51_Hot_Cross_Buns.mp3">Song 51</a><br>
 <a href="./playfile.php?run=true&argument1=52_Pat-A-Cake,_Pat-A-Cake.mp3">Song 52</a><br>
 <a href="./playfile.php?run=true&argument1=53_Little_Boy_Blue.mp3">Song 53</a><br>
 <a href="./playfile.php?run=true&argument1=54_Diddle,_Diddle,_Dumpling.mp3">Song 54</a><br>
 <a href="./playfile.php?run=true&argument1=55_There_Was_An_Old_Woman.mp3">Song 55</a><br>
 <a href="./playfile.php?run=true&argument1=56_Oh,_The_Grand_Old_Duke_Of_York.mp3">Song 56</a><br>
 <a href="./playfile.php?run=true&argument1=57_Humpty_Dumpty_Sat_On_A_Wall.mp3">Song 57</a><br>
 <a href="./playfile.php?run=true&argument1=58_Lavender's_Blue,_Dilly,_Dilly.mp3">Song 58</a><br>
 <a href="./playfile.php?run=true&argument1=59_Ring-A-Ring_O'_Roses.mp3">Song 59</a><br>
 <a href="./playfile.php?run=true&argument1=60_Mary,_Mary,_Quite_Contrary.mp3">Song 60</a><br>

<a href="./playfile.php?run=true&argument1=61_Roses_Are_Red.mp3">Song 61</a><br>
 <a href="./playfile.php?run=true&argument1=62_Little_Jack_Horner.mp3">Song 62</a><br>
 <a href="./playfile.php?run=true&argument1=63_Simple_Simon_Met_A_Pieman.mp3">Song 63</a><br>
 <a href="./playfile.php?run=true&argument1=64_One,_Two,_Buckle_My_Shoe.mp3">Song 64</a><br>
 <a href="./playfile.php?run=true&argument1=65_Bobby_Shafto's_Gone_To_Sea.mp3">Song 65</a><br>
 <a href="./playfile.php?run=true&argument1=66_Row,_Row,_Row_Your_Boat.mp3">Song 66</a><br>
 <a href="./playfile.php?run=true&argument1=67_There_Was_A_Crooked_Man.mp3">Song 67</a><br>
 <a href="./playfile.php?run=true&argument1=68_Barber,_Barber,_Shave_A_Pig.mp3">Song 68</a><br>
 <a href="./playfile.php?run=true&argument1=69_Rub-A-Dub-Dub.mp3">Song 69</a><br>
 <a href="./playfile.php?run=true&argument1=70_Cobbler,_Cobbler,_Mend_My_Shoe.mp3">Song 70</a><br>

<a href="./playfile.php?run=true&argument1=71_Yankee_Doodle_Went_To_Town.mp3">Song 71</a><br>
 <a href="./playfile.php?run=true&argument1=72_One_Misty,_Moisty_Morning.mp3">Song 72</a><br>
 <a href="./playfile.php?run=true&argument1=73_Little_Miss_Muffet.mp3">Song 73</a><br>
 <a href="./playfile.php?run=true&argument1=74_Ding,_Dong,_Bell.mp3">Song 74</a><br>
 <a href="./playfile.php?run=true&argument1=75_I_Love_Little_Pussy.mp3">Song 75</a><br>
 <a href="./playfile.php?run=true&argument1=76_Six_Little_Mice_Sat_Down_To_Spin.mp3">Song 76</a><br>
 <a href="./playfile.php?run=true&argument1=77_Good_Night,_Sleep_Tight.mp3">Song 77</a><br>
 <a href="./playfile.php?run=true&argument1=78_I_See_The_Moon.mp3">Song 78</a><br>
 <a href="./playfile.php?run=true&argument1=79_Star_Light,_Star_Bright.mp3>Song 79</a><br>
 <a href="./playfile.php?run=true&argument1=80_Twinkle,_Twinkle,_Little_Star.mp3">Song 80</a><br>
 <a href="./playfile.php?run=true&argument1=81_The_Man_In_The_Moon.mp3">Song 81</a><br>
 <a href="./playfile.php?run=true&argument1=82_Rock-A-Bye,_Baby.mp3">Song 82</a><br>
 <a href="./playfile.php?run=true&argument1=83_How_Man_Miles_To_Babylon.mp3">Song 83</a><br>
 </h1>
 </BODY>
 </HTML>

playfile.php just checks whether its being called from a web page or from the command line (for testing purposes) and the calls a scellscript called playfile.sh

playfile.php shown below

<?php

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

if (isCommandLineInterface()==TRUE)
 {
 $argument1 = $argv[1];
 exec("/var/www/html/playfile.sh $argument1");
 }
 else
 {
 if ($_GET['run'])
 {
 $argument1 = $_GET['argument1'];
 # This code will run if ?run=true is set.
 $output=exec("./playfile.sh $argument1");
 echo "<pre>$output</pre>";
 }
 }
 ?>

playfile.sh just checks that thee is not a song playing already and if not plays a song with mpg123

playfile.sh shown below

#!/bin/bash
 PLAYER=mpg123
 RES=`ps -ef | grep $PLAYER | grep -v grep | wc -l`
 if [ $RES -gt 1 ]
 then
 echo "Previous song already playing"
 else
 $PLAYER /var/www/html/newmusic/$1 1>/tmp/log 2>&1
 fi

Now you may want to stop a song thats playing so the music1.html also called a php file called killmusic.php shown below. Again this php file just checks whether its being called from a web page or command line and called a shellscript called killmusic.sh

 

<?php

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

if (isCommandLineInterface()==TRUE)
 {
 exec("/var/www/html/killmusic.sh");
 }
 else
 {
 if ($_GET['run'])
 {
 $output=exec(".//killmusic.sh");
 echo "<pre>$output</pre>";
 }
 }
 ?>

 

and finally killmusic.sh which just tries to find the process of the music playing and kills it.

#!/bin/bash
 PLAYER=mpg123
 PS=`ps -ef | grep $PLAYER | grep -v grep | tail -1 | awk '{ print $2 }'`
 if [ "$PS" -gt 0 ]
 then
 echo "killing Process $PS..."
 kill $PS

 else

       echo "No processes running. You tried to fool me:) "

fi

a quick check on permissions:

sudo chown www-data:www-data music1.html killmusic.php killmusic.sh newmusic playfile.php playfile.sh

sudo chmod 755 killmusic.php killmusic.sh playfile.php playfile.sh

lsal2

Now you can open the web page music1.html and select the music to play.

 

pirhymes

and that’s all folks.

WhereThingsAreBorn 🙂

 

 

 

 

 

 

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