Bluetooth for Squeezeplug & Squeezelite


35 posts   Page 1 of 2   1, 2
by castalla » Wed Jan 09, 2013 9:42 am
Quick guide to setting up a bt speaker for Squeezelite

You need at least version 6 of truehl's Squeezeplug - with Triode's Squeezelite; Squeezeplay on a PC or an android LMS controller app; lots of patience.

Be prepared to carry out multiple reboots! You need to have ssh access, as all the commands are carried out at the command line.

Try at your own risk! See point 26 for some issues.

1. Obtain a working bt dongle - plug into a usb port

2. At the command line type
Code: Select all
apt-get update
. Then Install bluetooth and bluez-utils

2. Copy a test.wav file to /home (I just copied a win7 wav file from my PC)

3. Probably best to reboot here

4. At command line, type
Code: Select all
hciconfig
- this will report the status of the bt dongle - if it's showing UP RUNNING IPSCAN, then proceed.

5. Switch on your bt speaker or headset in pairing mode

6. Type
Code: Select all
hcitool scan


7. This should give you the bt device addess of the speaker: xx:xx:xx:xx:xx:xx

8. Make a note of this number for future use!

9. Type
Code: Select all
bluez-simple-agent hci0 [the device address]
- this should return a request for the passcode - usually 0000

10. If there are no error messages then the pairing was successful!

11. Type
Code: Select all
bluez-test-audio [address]


12 Type
Code: Select all
bluez-test-device trusted [address] yes


13. Now, add the bt device to asound.conf (in /etc):

add the following to the end of what already exists in asound.conf - be sure to use the device address from above

Code: Select all
pcm.NAME {
      type plug
      slave {
            pcm {
                type bluetooth
                device XX:XX:XX:XX:XX:XX
                profile "auto"
            }
      }
      hint {
           show on
           description "My bluetooth speaker"
      }
   }

   ctl.NAME {
      type bluetooth
   }


Edit NAME and XX: string to your choice

14.Type
Code: Select all
aplay -L
- this should show NAME as a sound device

15.Type
Code: Select all
aplay -D NAME /home/test.wav
- this should play through your speaker

16.Probably best to reboot to check if a new reboot picks up your bt speaker when you switch it on

17. After reboot and pairing, try the
Code: Select all
aplay -D NAME /home/test.wav
again to check.

18. If the test works, you're in business.

19. Install squeezelite using truehl's setup, if not already installed

20. The installed squeezelite may not be the latest version - it also starts on boot-up (as instance 1). You can check the version by typing squeezelite -? You need at least version 0.9beta2 or later

21. Type
Code: Select all
cd /usr/bin


22. Type
Code: Select all
squeezelite -m ab:cd:ef:12:34:56 -n WhateverYouLike -o NAME -r 44100 -z


This starts instance 2 of squeezelite with output to the bt speaker

23. Start Squeezeplay or an android app controller and switch to WhateverYouLike as your player (Settings - Choose Player)

24. Switch on your bt speaker - wait for it to pair

25. Choose a stream to play on Squeezeplay - you may have to adjust the volume on the player

26. At this point things become murky - switching off the bt speaker seems to cause a random crash of the Pi. It may be necessary to turn the player off before switching the bt speaker off. I've also found that the default audio (analog output, in my case) becomes corrupted. Fixing this was difficult and involved reimaging the whole squeezeplug image agan from scratch.

I'm not sure if -r 44100 is an appropriate value - advice needed

The second instance is not sticky - the command needs to be added to the boot sequence - advice needed on how best to do this!

27. You can remove a paired device with
Code: Select all
bluez-test-device remove [address]


28. It works - but seems flaky. I'm sure somebody else can come up with a better way! All suggestons, improvements welcome.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Wed Jan 09, 2013 2:36 pm
Ooops ... missed this step.
Replace step 11. with 11a.


11a. Add this to the [General] section of /etc/bluetooth/audio.conf

Code: Select all
Disable=Media
Enable=Socket


Then carry out the following:

Code: Select all
bluez-test-audio connect [address]
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Wed Jan 09, 2013 3:58 pm
I've been playing with this procedure for a while.

WARNING - it's not at all stable. I've encountered big problems with noise appearing on the default audio output after the bt output is stopped.

Contributions to solve this are welcomed!
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Fri Jan 11, 2013 9:28 pm
For the record - I've now got ths working reliably without crackles, etc.

It seems the problem was a specific SD card - corrupted? Using a different hgher spec. SD card seems to have solved the problem.

Please change step 22. above to :

22. Type

squeezelite -m ab:cd:ef:12:34:56 -n WhateverYouLike -o NAME -r 192000 -z
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Sun Jan 13, 2013 5:29 pm
Edit: same issue with v2 board! Grrrrrrrrrrrrrrrrrrrr ....

I've discovered network problems with the original v1 Raspi.

Switching off the bluetooth device causes the network to crash.

This issue seems absent with a v2 board.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Mon Jan 14, 2013 1:43 am
My final post, unless there's a major breakthrough.

After hours of testing, reconfiguring, reimaging, I've come to the end of the line getting BT speakers to work reliably with the PI. It works, but falls over badly when the speaker is switched off ...

The problem is that as soon as the speaker is switched off, the Pi goes into a fit and disappears from the network. There's no recovery except a reboot.

Makes BT speakers useless.

Unless somebody has a brainwave or knows a solution, then I've exhausted all the possibilities.

Is there some obscure config setting to set? I'm all googled out on this!
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Tue Jan 15, 2013 9:43 pm
My descent nto the bluetooth circle of hell continues ...

The only way to cleanly switch off the bt device is to issue the following:

Code: Select all
bluez-test-device disconnect [device address]


The device can then be switched off.

If the device is simply switched off then the network connection is killed and the PI has to be hard rebooted.

Unless there's a way to tie this command to the squeezelite instance using the bluetooth output, then I can't see how squeezelite can be used with bluetooth.
Last edited by castalla on Fri Jul 25, 2014 11:23 am, edited 1 time in total.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by mba » Thu Jan 17, 2013 8:13 pm
Hi castalla, thanks for your guide its was real invalvuable and überkool to have music playing on two sqeezelite devices on the same pi. My goal is to have normal livingroom audio through HDMI to my AVR and a bluetooth speaker in the kitchen/bathroom.

I don't experience a complete system breakdown when turning off my BT headset (which I use for testing), instead the squeezelite instance running on the BT alsa device starts outputting lots of :
ALSA lib audio/pcm_bluetooth.c:835:(bluetooth_playback_poll_revents) read error: Resource temporarily unavailable (11): Resource temporarily unavailable

This resource doesn't become available if I switch on the headset again.

Maybe if you remove -z on your squeezelite commandline you will see something similar?

Another minor issue is the two players is way out sync when synchronizing them, I havent tried adding a timeoffset so maybe it can be nulled.

Br.
Martin
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by castalla » Thu Jan 17, 2013 9:56 pm
Thanks for the feedback ...

I've seen those error messages too when without -z

BUT, my Pi dies before the squeelite process ... even if I just start the Pi - switch on the BT then switch ii off without any actions, then 10 secs. later the Pi ethernet dies and the Pi is removed from the network.

I've tried with 2 different Pi's, two different speakers - same outcome.

I only have one dongle - so I can't test if it's a dongle issue.

Which dongle do you have?
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Fri Jan 18, 2013 1:40 am
Progress and then failure!

Using a wifi dongle lets me switch off the headset without a crash.

If I switch the bt device on again, then the Pi crashes.

Something not right here - I suspect the usb implementation in firmware
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by mba » Fri Jan 18, 2013 7:22 pm
Just did a rpi-update and now I get a kernel oops once in a while when turning off the headset. I don't have a dump yet, I will take a photo next time it happens.

Meanwhile I wrote a little script which checks for the presence of a device with a specific mac address and start squeezelite when its connected and kills it again when it disconnects. It works really well and ensures that squeezelite audio over bluetooth is available every time you turn on your bt speakers.

Code: Select all
#!/bin/bash
pid=0
address="00:23:78:07:E5:6D"
squeezelite_started=false
while (sleep 1)
do
  if [[ $squeezelite_started == false ]] ; then
    connected=$(hcitool con) > /dev/null
    if [[ $connected =~ .*${address}.* ]] ; then
      squeezelite -m ab:cd:ef:12:84:36 -n Jabra -o jabra -r 44100&
      echo $! > /tmp/jabra_squeezelite.pid
      squeezelite_started=true
      echo $address connected started Squeezelite
    fi
  fi

  if [[ $squeezelite_started == true ]] ; then
    connected=$(hcitool con) > /dev/null
    if [[ ! $connected =~ .*${address}.* ]] ; then
      pid=$(cat /tmp/jabra_squeezelite.pid)
      kill -9 $pid
      rm /tmp/jabra_squeezelite.pid
      squeezelite_started=false
      echo $address disconnected stopped Squeezelite
    fi
  fi
done
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by castalla » Fri Jan 18, 2013 7:57 pm
That looks very interesting.

I thought rpi-update was deprecated?

Just that from the command line?
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Fri Jan 18, 2013 10:32 pm
Rpi-updated .... still crashing.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by mba » Sat Jan 19, 2013 10:00 am
I thought rpi-update was deprecated?

I don't think rpi-update is deprecated, how would one update the firmware if not by using rpi-update?

Just that from the command line?

You could add the script as a startup daemon, it needs of course some adjustments but that would probably be the best way. But for testing you could just run it from the commandline.

Which dongle do you have?

I am using a noname EDR BT 2.0 dongle. Maybe I will order a couple of different ones on ebay for testing, anyway I could use one with an external antenna for better reception.
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by mba » Sat Jan 19, 2013 10:24 am
I just ordered two different BT dongles on ebay for further testing, hopefully we can pinpoint where the problem is. Cause this is a pretty great idea for multiroom audio. Wondering what the limit of squeezelite / bt devices instances that can run on one pi at once?
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by castalla » Sat Jan 19, 2013 12:32 pm
It's so infuriating .... everything works apart from this disconnect/reconnect issue!

Any idea which log(s) might actually have a record of what's happening ?

I thought the bt spec limits connections to one device at any given time?

There's a Veho speaker which runs on wifi - but I'm not shelling out any more cash on something that may or may not work!
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by mba » Sat Jan 19, 2013 3:38 pm
I don't worry too much about an error like this, its work in progress and part of normal development. If things just worked then there wouldn't be the same sense of achievement or satisfaction. Its been a while since I started trying to get some kind og squeezebox setup working with the raspberry pi, initially I tried getting SqueezePlay working but my attempts was not 100% succesful, maybe due to premature alsa-drivers or whatever. Alsa drivers matured over time and then came Triode along with his excellent SqueezeLite. My inital goal was reached and I have now perfect 100% stable internet radio and can play my music collection. Next goal was multiroom audio, I read your post about bluetooth and thought this is excellent! And I am sure that in a short while this will work too :D

Back to the problem. After a bit of testing I got another kernel crash when switching the headset off and on again.
dump.jpg
dump.jpg (33.9 KiB) Viewed 3507 times


I got this error when starting squeezelite at the same time
ALSA lib audio/pcm_bluetooth.c:1614:(audioservice_expect) BT_START_STREAM failed : Success(0)
ALSA lib audio/pcm_bluetooth.c:1573:(audioservice_recv) Too short (1 bytes) IPC packet from bluetoothd
[15:12:25.894222] alsa_open:304 unable to set hw params: Invalid argument
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by castalla » Sat Jan 19, 2013 3:52 pm
Keep up the good work!

It means nothing to me, I'm afraid. I'm really just an end-user at heart.

Hope somebody more techy can figure it out and contribute some fix!
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Wed Jan 23, 2013 2:00 pm
A brief update:

Simply issuing a bluez-test-device disconnect [bt address] just crashed the network connection.

:?: :?: :?:
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Wed Jan 23, 2013 6:34 pm
Firmware dated 22 January - network crash again.

WILL THIS EVER WORK??????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Thu Jan 24, 2013 12:58 pm
Another update:

Tried Fedora ... same issues - network crashes on attempting a connection.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Sat Feb 02, 2013 5:59 pm
@mnb

Not sure what's changed ... I updated the kernel and firmware - made no difference,

I then decided to try the original bt dongle I bought (Conceptronic - Broadcomm chip) which wouldn't work at all previously. This time it connected to my cheapo bt speakers (although it won't autopair, as far as I can see). I launched a second squeezelite - played fine.

I then logged out of PuTTy - switched off the bt speaker and waited for the crash ... which never came!

--------------------

So, I have a working bt setup ... of sorts ... at last. I haven't tried switching the speaker on again to see what happens.

--------------------

I'd like to try your script - but can you explain a couple of points?

1. the script starts with address - that's the bt speaker, correct?

2. the squeezelite run command ends with & - is that the same as -z ??

3. I already have one instance of squeezelite running - default analog output. So, will your script start and stop the second instance, leaving the first untouched?

4. As an executable, where can I put the command to start on boot after the first instance is running?

(I know you are busy (!!!) but hope you can get a moment to answer my newbie queries ....)
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by mba » Sat Feb 02, 2013 9:42 pm
Ok, that sounds promising. Hope you will have a working solution soon.
I tried answering your questions below:

1. the script starts with address - that's the bt speaker, correct?
Yes that is correct, just change it to the address of your speaker

2. the squeezelite run command ends with & - is that the same as -z ??
No, -z daemonizes squeezelite while & just detaches it from the console. This way I can send standard output and error to a log file.

3. I already have one instance of squeezelite running - default analog output. So, will your script start and stop the second instance, leaving the first untouched?
Yes. The script stores the pid of new squeezelite instance with: echo $! > /tmp/jabra_squeezelite.pid and uses that pid to kill it when the speaker is disconnected. (oh, just saw that I named the file after the headset I was testing with. Please rename it to something that makes sense on your system :-))

4. As an executable, where can I put the command to start on boot after the first instance is running?
It could be changed into a normal init script, or else you could store it verbatim in /root/ and call it from /etc/rc.local
AMOTE - a LIRC client for Android. Build your own Android-based universal remote. Get it on Google Play! Visit www.datscharf.dk/amote
Posts: 109
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark
by castalla » Sat Feb 02, 2013 11:48 pm
Thanks so much for the reply!

I'll work on this tomorrow. I suppose I could run it as a daemon?

(For whatever reason, the cheapo speaker is now auto-pairing!)
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm
by castalla » Sun Feb 03, 2013 4:11 pm
One step forward, possibly 2 steps back .... !!!

1. Speaker has stopped autopairing again .... scratch head???

2. The script works BUT there's horrendous crackle on bt

3. If I just run the squeezelite process (same params) from the command line, then the sound is perfect - scratch head???

I put the script in /home and run as ./script - it's 0644 permissions.
Posts: 472
Joined: Thu Jul 19, 2012 3:46 pm