Page 1 of 1

PHP, MPG123, and Bluetooth

Posted: Mon Apr 15, 2019 1:51 am
by pedalhoundz
I'm making a baby monitoring system with three pi's.

One is connected to a touch screen and is the 'Parent's' pi.
One has sensors connected to it and a bluetooth speaker. This is the 'Baby's' Pi
The other is just a dedicated camera pi with MotionEyeOS on it.

I've made a web interface on a local server to work with using Apache and PHP.

There is a bank of 3 mp3 files to choose from to play a lullaby in the baby's crib via the bluetooth speaker.

I'm using mpg123 player to play these as I cannot solve the problem with X11 forwarding pertaining to VLC player.
Note: I do have pulse audio, pavucontrol, blueman installed and my speaker automatically connects upon startup and works.
When I test out music in my terminal on mpg123 it plays out of the bluetooth speaker loud and proud.

Code: Select all

mpg123 -C /var/www/Lullabies2/twinkle.mp3
YET when I try to call these commands through my buttons on my web interface.. it only plays through HDMI and not bluetooth.

CODE FOR BUTTONS

Code: Select all

<form name="update" method="post">
<p style="text-align:center;">
<button name="Twinkle" type="submit" style="align:center; background:white; font-family:Bradley Hand ITC;font-weight:bolder;font-size:21px; color:crimson; background-color: whitesmoke; border: 3pt ridge lightskyblue;border-radius:10px;cursor:pointer; margin-top:10px">Twinkle Twinkle, Little Star</button>
</p>

<p style="text-align:center;">
<button name="Rockabye" type="submit" style="align:center; background:white; font-family:Bradley Hand ITC;font-weight:bolder;font-size:21px; color:crimson; background-color: whitesmoke; border: 3pt ridge lightskyblue;border-radius:10px;cursor:pointer; margin-top:14px">Rockabye, Baby</button>
</p>

<p style="text-align:center;">
<button name="Lullaby" type="submit" style="align:center; background:white; font-family:Bradley Hand ITC;font-weight:bolder;font-size:21px; color:crimson; background-color: whitesmoke; border: 3pt ridge lightskyblue;border-radius:10px;cursor:pointer; margin-top:14px">Lullaby and Goodnight</button>
</p>

<p style="text-align:center;">
<button name="STOPMUSIC" type="submit" style="align:center; background:white; font-family:Bradley Hand ITC;font-weight:bolder;font-size:18px; color:crimson; background-color: black; border: 3pt ridge whitesmoke;cursor:pointer; margin-top:14px">STOP MUSIC</button>
</p>

</form>

PHP CODE

Code: Select all

<?php
ini_set('dsplay_errors', '1');
if (isset($_POST['Twinkle']))
{
	shell_exec("mpg123 -C /var/www/Lullabies2/twinkle.mp3 2>&1");
}
if (isset($_POST['Rockabye']))
{
	shell_exec("mpg123 -C /var/www/Lullabies2/rockabyebaby.mp3 2>&1");
}
if (isset($_POST['Lullaby']))
{
	shell_exec("mpg123 -C /var/www/Lullabies2/lullabygoodnight.mp3 2>&1");
}
if (isset($_POST['STOPMUSIC']))
{
	echo shell_exec("pkill mpg123");
}
?>
Am I missing something obvious as to routing the mp3's to the bluetooth? Why would it play out of bluetooth when running from terminal and only from hdmi when running from PHP?

Re: PHP, MPG123, and Bluetooth

Posted: Mon Apr 15, 2019 6:50 pm
by Andyroo
Could it be tied into the user with the destination is set at user lever rather than system level?

PHP normally runs under www-data and not Pi.

Maybe you need to do a destination select at the start of the play?

Re: PHP, MPG123, and Bluetooth

Posted: Mon Apr 15, 2019 7:24 pm
by Douglas6
Try adding the www-data user to the audio group.

Re: PHP, MPG123, and Bluetooth

Posted: Mon Apr 15, 2019 8:03 pm
by pedalhoundz
Thanks for the suggestions.
Douglas6 wrote:Try adding the www-data user to the audio group.
The www-data user is indeed already in the audio group. Before this I could get no music playing anywhere from PHP.
Andyroo wrote: Could it be tied into the user with the destination is set at user lever rather than system level?

PHP normally runs under www-data and not Pi.

Maybe you need to do a destination select at the start of the play?
Do you or anyone else know how to properly add the routing to the destination? I have the MAC address of my speaker..
I've seen things like

Code: Select all

 mpg321 -a bluealsa:HCI=hci0,DEV=XX:XX:XX:XX:XX:XX,PROFILE=a2dp /usr/share/sounds/alsa/Front_Center.wav
but I don't believe I can use bluealsa and pulse audio volume control at the same time? I've tried this code before and it returns errors.