Use bluetooth audio on Raspberry PI

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

marigo
Posts: 30
Joined: Sunday 31 May 2015 11:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands, Veenendaal
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by marigo »

If you use mplayer with the "-softvol" option then you can control the volume from the command line with "9" or "0".

I am searching for an option to control the volume within domoticz with a slider button like the dimmer option. Somehow you have to parse the keystroke "9" or "0" to the mplayer proces but at this moment I am searching on how to accomplish this.

Maybe anyone have some experience to get this working?

EDIT:
This is a Lua script with a "dimmer" to control volume, but I can change it only once:

Code: Select all

commandArray = {}

if (devicechanged['TEST'] == 'Set Level') then
        v = math.floor(otherdevices_svalues['TEST'] - 20)
        os.execute("mplayer -ao alsa:device=bluetooth -af volume='" .. v .."' /mnt/music/top40/*.mp3")
        print("radio volume " .. v)
end

return commandArray


In the log I see that the script is running for more then 10 seconds which means a loop somewhere.

Can someone advise?

EDIT2:

I have found something to control the volume and play the next track with FIFO and this is working within Domoticz

https://pbrisbin.com/posts/controlling_mplayer/

I have made three "one-push-buttons" and three scripts

1- volume up

Code: Select all

#!/bin/bash
sudo echo volume 1 > /root/.mplayer_fifo &
2- volume down

Code: Select all

#!/bin/bash
sudo echo volume - > /root/.mplayer_fifo &
3 - next track

Code: Select all

#!/bin/bash
sudo echo pausing_keep_force pt_step 1 > /root/.mplayer_fifo &
You need the following line to play your audio via your bluetooth adapter:

Code: Select all

sudo mplayer -ao alsa:device=bluetooth -softvol -af volume=-20 /mnt/music/03-Albums/*.mp3 -shuffle &
fransiefrans
Posts: 59
Joined: Sunday 14 June 2015 13:22
Target OS: Raspberry Pi / ODroid
Domoticz version: stable
Location: Netherlands
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by fransiefrans »

In order to get the izSynth working, I had to connect my bluetooth speaker first.
Therefor I used the BTaudio Wiki but it looks like the bluez package changed.
I can't get it to connect with my speaker.
Is there someone who got it the work with the new bluez?
Skaven
Posts: 3
Joined: Saturday 10 May 2014 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sweden
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by Skaven »

Hello, I'm wondering if somebody can explain to my how I setup this on my raspberry pi 3 white the integrated Bluetooth chip on raspberry pi 3 board, can I use the integrated Bluetooth chip? If yes can you explain how to setting it up?
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by pvm »

Skaven wrote:Hello, I'm wondering if somebody can explain to my how I setup this on my raspberry pi 3 white the integrated Bluetooth chip on raspberry pi 3 board, can I use the integrated Bluetooth chip? If yes can you explain how to setting it up?
When following the wiki., what output / errors do you get?
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by curious »

I am trying to follow this wiki on my Raspberry 2.
Unfortunately I am stuck .

At first I get a message after entering

Code: Select all

sudo apt-get install bluetooth bluez bluez-utils bluez-alsa

Code: Select all

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package bluez-alsa is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package bluez-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  bluez

E: Package 'bluez-utils' has no installation candidate
E: Package 'bluez-alsa' has no installation candidate
Should not be a problem, but after that there is no audio.conf nor is there a bluetooth-agent (command not found)
I use jessie-lite

My bluetooth device is recognized when entering lsusb

What to do
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by freijn »

Very interesting this one as well.. Will read..
tontze
Posts: 317
Joined: Thursday 12 January 2017 15:30
Target OS: Linux
Domoticz version: Beta Ch
Location: Finland
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by tontze »

-----------------------------------------
Smartthings
zigbee2mqtt
RFLink 433mhz / Nrf 2.4Ghz
Mi Light
esp8266MiLight Hub
OpenHab/HomeAssistant/Domoticz
HP T610 & Debian 5.10.19-1 x86_64[/b]
User avatar
DewGew
Posts: 581
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10618
Location: Sweden
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by DewGew »

Is BTAudio out of date?
I tried to install bluetooth bluez bluez-utils bluez-alsa but i get this:

Code: Select all

Package bluez-alsa is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package bluez-utils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
However the following packages replace it:
  bluez

E: Package 'bluez-utils' has no installation candidate
E: Package 'bluez-alsa' has no installation candidate
Is there an other way for this?
Raspberry Pi 3 | domoticz | Aeon Labs Z-Stick GEN5 | RFlink gateway
NanoPi NEO-air | REGO6XX interface | Machinon theme | Homebridge | Domoticz Google Assistant | ideAlarm
User avatar
ledfreak3d
Posts: 98
Joined: Sunday 01 November 2015 15:30
Target OS: Linux
Domoticz version: 3.8025
Location: Hoorn
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by ledfreak3d »

yep no longer works wiki needs to be update

bt audio works fine from the command line
but will never work for me when run from a lua script
cant find why.

not on a rpi3 or on a oppc+ board
Unleashe the magic smoke ;)
solo111
Posts: 1
Joined: Tuesday 01 January 2019 18:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by solo111 »

ledfreak3d wrote: Monday 17 December 2018 20:14 yep no longer works wiki needs to be update

bt audio works fine from the command line
but will never work for me when run from a lua script
cant find why.

not on a rpi3 or on a oppc+ board
Good day! I join you .. I have the same trouble. My script does not start when I press the button. I read that this is due to the rights of the user. But I have a user in Domoticz by default. And what user do you have?
User avatar
ledfreak3d
Posts: 98
Joined: Sunday 01 November 2015 15:30
Target OS: Linux
Domoticz version: 3.8025
Location: Hoorn
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by ledfreak3d »

prety sure its not a user acount issue tried root and pi does not make any difference
Unleashe the magic smoke ;)
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

edit: 30 juli 2019 - see Thorgal789 post below and my post right after that.
I started with BT-audio from the wiki page also, but I soon have found out that this does not work anymore. I assume, because of some changes to the bluetooth packages. I’m certainly not an expert, but did get a little bit further.
I want to use this bluetooth speaker to make some alert sounds as feedback.

I used pulseaudio. I believe this is preferred audio system by newer distro’s.

Code: Select all

sudo apt install pulseaudio pulseaudio-utils pulseaudio-module-bluetooth

sudo nano /etc/bluetooth/main.conf
(/etc/bluetooth/audio.conf is not used anymore)

add (or change if exist):

Code: Select all

# This section contains general options
    [General]
    Enable=Source,Sink,Media,Socket
Change/add the following configuration in your /etc/pulse/system.pa config.

Code: Select all

Sudo nano /etc/pulse/system.pa

# Modify: /etc/pulse/system.pa

### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluez5-discover.so
load-module module-bluez5-discover
.endif
and in this same file change, or add the line:

Code: Select all

load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulseaudio.socket
The /tmp/socket makes it possible to connect to pulseaudio with different users.

I added/changed the same lines in /etc/pulse/default.pa

In /etc/pulse/client.conf
change, or add the lines:

Code: Select all

default-server = unix:/tmp/pulseaudio.socket
autospawn = no
I had to remove all files from: /home/pi/.config/pulse/ and /root/.config/pulse/ to make this work.
Especially the client.conf file in there.

In /etc/pulse/daemon.conf I changed/added the line:

Code: Select all

 exit-idle-time = -1
 allow-module-loading = yes
 enable-memfd = yes
First stop pulse audio:

Code: Select all

 pulseaudio -k
Next, Start pulseaudio at boot as service (or daemon? I’m not sure about the difference)
For this part I used this source: link )

Code: Select all

 sudo nano /etc/systemd/system/pulseaudio.service 
And change or add:

Code: Select all

[Unit]
Description=PulseAudio system service

[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio
Restart=on-failure

[Install]
WantedBy=multi-user.target

Now enable and start this unit file by running the following commands:

Code: Select all

 sudo systemctl --system enable pulseaudio.service
sudo systemctl --system start pulseaudio.service 
It should be running now. You can check the status of the service with the command:

Code: Select all

 sudo systemctl --system status pulseaudio.service
I got an error about a cookie file. I will report how I solved this later.

Now add the user who needs access to pulseaudio to the pulse and pulse-access groups:

Code: Select all

sudo usermod -aG pulse,pulse-access <username>
And I added root too.
sudo usermod -aG pulse,pulse-access root
change <username> with pi or your normal username.
Changes to groups are effective after you logout and log back in.

Bluetooth:
On ubuntu:

Code: Select all

sudo add-apt-repository ppa:bluetooth/bluez
sudo apt update
sudo apt upgrade
sudo apt install bluez bluetooth
sudo service bluetooth restart
sudo /etc/init.d/bluetooth start
or try using the method donwloading from kernel.org as described here: link

I'm using version 5.49

Code: Select all

bluetoothctl -v
5.49
I also installed Bluealsa. But now I'm not sure if this is nescesary.
source

Code: Select all

sudo apt install bluealsa

Type:

Code: Select all

sudo bluetoothctl 
then press enter and input the administrator password.
Next, enter

Code: Select all

[Bluetooth] agent on 
and press enter. Then type

Code: Select all

[Bluetooth] default-agent 
and press enter. Type

Code: Select all

[Bluetooth] scan on 
and press enter one more time. The unique addresses of all the Bluetooth devices around will appear and look something like an alphanumeric XX:XX:XX:XX:XX:XX. If you make the device you want to pair discoverable (or put it into pairing mode), the device nickname may appear to the right of the address. If not, you will have to do a little trial and error or waiting to find the correct device.
To pair the device, type

Code: Select all

[Bluetooth] pair XX:XX:XX:XX:XX:XX 
XX:XX:XX:XX:XX:XX is the device bluetooth address. Change it with the address of your speaker (for example)
Connect to the device, type

Code: Select all

 connect XX:XX:XX:XX:XX:XX 
Then:

Code: Select all

 trust XX:XX:XX:XX:XX:XX 
type 'exit' to leave bluetoothctl.


In order to pair up with Bluetooth devices, you need to add, or change the following text to the file /etc/dbus-1/system.d/pulse.conf:

Code: Select all

 sudo nano /etc/dbus-1/system.d/pulseaudio-system.conf
Check if this file exist. In my case there where two files. One pulse.conf en this one. I merged them to pulseaudio-system.conf.

Code: Select all

<!DOCTYPE busconfig PUBLIC
 "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
        <policy user="root">
                <allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
        </policy>
        <policy user="pulse">
                <allow own="org.pulseaudio.Server"/>
                <allow send_destination="org.bluez"/>
                <allow send_interface="org.bluez.Manager"/>
        </policy>
        <policy context="default">
                <deny own="org.pulseaudio.Server"/>
                <deny send_destination="org.bluez"/>
                <deny send_interface="org.bluez.Manager"/>
        </policy>
</busconfig>
According to this same source I changed/added in the file:
/etc/dbus-1/system.d/bluetooth.conf

Code: Select all

    <allow send_interface="org.bluez.ThermometerWatcher1"/>
    <allow send_interface="org.bluez.HeartRateWatcher1"/>
    <allow send_interface="org.bluez.CyclingSpeedWatcher1"/>

 <!-- allow users of bluetooth group to communicate -->
  <policy group="bluetooth">
    <allow send_destination="org.bluez"/>
  </policy>

It’s easiest to reboot the system now.

Code: Select all

sudo shutdown -r now
After this reboot, find out the name to use for the bluetooth device to make some noise.

Code: Select all

sudo pacmd list-sinks
(I still cannot get info without sudo. That I do not understand)
find the: bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink” part. We will need this addres.
I use paplay like this:

Code: Select all

paplay -p --device=bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink --volume=65536 /home/pi/sounds/dingdong-1.wav 
volume 65536 is volume set to 100%. So be careful. You could set it in a range of 0 – 65536. paplay does not play mp3’s. Mplayer can.
Another method I use is by using mplayer, like this:

Code: Select all

mplayer -ao pulse::bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink /home/pi/1.mp3 -softvol -volume 25 
This is working well for me, but just for a while. After some time my bluetooth speaker disconnects. After that I had some trouble reconnecting.
In the next post I solved this.

Edit: some corrections
edit: 29 juli 2019 - made some changes
Last edited by AllesVanZelf on Tuesday 30 July 2019 21:15, edited 8 times in total.
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

Keep Bluetooth connection alive
I did some more research and might found a way to keep bluetooth connection to the bluetoth speaker open.
But not sure yet if this will be the solution on the long run.
All feedback is more then welcome!

source 1
source 2

Make a script to reconnect the Bluetooth device:

Code: Select all

 sudo nano /home/pi/bin/autoconnect-bluetooth-device.sh
with content:

#!/bin/bash
bluetoothctl << EOF
connect XX:XX:XX:XX:XX:XX
EOF

Code: Select all

 chmod +x /home/pi/bin/autoconnect-bluetooth-device.sh
I save my own scripts in this /home/pi/bin directory.

The next script will check if the connection is still alive, and when not – start the last script.

Code: Select all

 sudo nano /home/pi/bin/connect-bt-devs.sh

#!/bin/bash
address="XX:XX:XX:XX:XX:XX"
while (sleep 1)
do
  connected=$(hcitool con) > /dev/null
  if [[ ! $connected =~ .*${address}.* ]] ; then
    /home/pi/bin/autoconnect-bluetooth-device.sh
  fi
done

Code: Select all

chmod +x /home/pi/bin/connect-bt-devs.sh
The next step creating a service to keep checking.

Code: Select all

sudo nano /etc/systemd/system/connectbtspeaker.service
with content:
[Unit]
Description=keep connection Bluetooth speaker alive
Documentation=https://www.domoticz.com/

[Service]
# Ubuntu/Debian convention:
Type=simple
ExecStart=/home/pi/bin/connect-bt-devs.sh

[Install]
WantedBy=multi-user.target
Before the last step, make sure the device/speaker is paired and connected. Then enable the service:

Code: Select all

 sudo systemctl enable connectbtspeaker.service
sudo systemctl start connectbtspeaker.service
sudo systemctl status connectbtspeaker.service
sudo systemctl stop connectbtspeaker.service

I’m not yet sure if the next command is nescesary.

Code: Select all

sudo systemctl preset connectbtspeaker.service
preset to set the service to be auto-started at boot, if the local policy permits it.
Connecting was not enough. The speaker go to sleep. for this I found another solution.

Update:
I found out from here: source, that it is possible to keep the bluetooth connection alive with a hcitool spinq command.
Now put this simple script into /etc/init.d/

Code: Select all

sudo nano /etc/init.d/bluetooth-alive
#! /bin/sh
hcitool spinq
exit 0
Then execute these.

Code: Select all

sudo chmod +x /etc/init.d/bluetooth-alive
sudo service bluetooth-alive enable
sudo update-rc.d bluetooth-alive defaults
sudo service bluetooth-alive start
Edit (29-7-2019):
If I do something silly or stupid, please tell me!! :) But the speaker is up and running for a few month now.
Last edited by AllesVanZelf on Monday 29 July 2019 11:53, edited 2 times in total.
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Thorgal789
Posts: 849
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by Thorgal789 »

I m sorry, I can't test it now, I have uninstalled my alexa application for the moment (and I had similar problem too, and I haven't tested so much complicated script), but be sure it will be usefull on my side ^^.
Thx for all.
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

I have edited the above post a little, because I had some new findings.
I also have found out that I have to (re)start bluetooth after pulseaudio. else it will not work here.

Code: Select all

sudo service pulseaudio restart
sudo service bluetooth restart
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

hmmm still no stable connection. Anyone could review my settings?
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Thorgal789
Posts: 849
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by Thorgal789 »

Nothing to see with your method, but on my side I m using Bluealsa.
My speaker is connected 24/24, I can say if the connection is stable during a week, because I move it a lot for test ATM. But I m just using this command to send sound.

For Wav, realy stable

Code: Select all

aplay -D bluealsa:HCI=hci0,DEV=11:75:58:E4:71:30,PROFILE=a2dp sound.wav
for mp3, instable

Code: Select all

mpg123 -a bluealsa:HCI=hci0,DEV=11:75:58:E4:71:30,PROFILE=a2dp sound.mp3
I already have some problem with pulseaudio.
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

Hello Thorgal789,
I'm very interested in this. Could you tell me what your configuration files are?
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Thorgal789
Posts: 849
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by Thorgal789 »

All by defaut ^^.
Sound use rapsberry speaker by defaut like a defaut configuration.

The speaker configuration is on the command line and by-pass the defaut configuration, I think there a way a set it by defaut, but I had lot of problem with sound core and I don't want to broke it again so I m using this solution.

Code: Select all

sudo apt-get install bluealsa
Take care, perhaps conflicts with pulseaudio., don't remember all the requierement.

Code: Select all

pi@raspberrypi: $ amixer -D bluealsa
Simple mixer control 'TimeBox-mini-audio - A2DP',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 127
  Mono:
  Front Left: Playback 127 [100%] [on]
  Front Right: Playback 127 [100%] [on]
I just use bluetoothctl to pair the speaker (and configure bluetooth connexion), not sure using the raspberry GUI is working all the time.

Code: Select all

pi@raspberrypi:/var/run/bluealsa $ bluetoothctl
[NEW] Controller B8:27:EB:C3:32:96 raspberrypi [default]
[NEW] Device 11:75:58:E4:71:30 TimeBox-mini-audio
[TimeBox-mini-audio]# info 11:75:58:E4:71:30
Device 11:75:58:E4:71:30
        Name: TimeBox-mini-audio
        Alias: TimeBox-mini-audio
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: yes
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Use bluetooth audio on Raspberry PI

Post by AllesVanZelf »

Earlier I ran into trouble with Alsa and bluetooth and bluez-alsa. I did know that bluealsa is in repository now. Maybe it was there all the time ;)
I removed pulseaudo:

Code: Select all

 sudo apt purge pulseaudio pulseaudio-module-bluetooth
(first made some backups of configuration files).

installed bluealsa as you said:

Code: Select all

sudo apt-get install bluealsa
created a file /etc/asound.conf

Code: Select all

sudo nano /etc/asound.conf
with content:

Code: Select all

defaults.bluealsa.service "org.bluealsa"
defaults.bluealsa.device "5D:57:C3:CB:0D:B3"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 3000
And now I cam play files as:

Code: Select all

 aplay -D bluealsa /home/pi/soundfiles/3.wav
It survived a first reboot.
Thank you very much Thorgal789 for pointing this out to me!!

Esit: The last smiley is a: - D
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest