Raspberry Pi streaming internet radio to bluetooth

Introduce yourself, create test postings or talk nonsense
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Raspberry Pi streaming internet radio to bluetooth

Post by mimosa »

I may finally have found a use for my trusty Pi Model B. It was quite tricky to get it to play with my bluetooth speaker, but I was eventually able to do so using this very clear guide:
https://www.sigmdel.ca/michel/ha/rpi/bl ... 02_en.html
This method is with ALSA and bluez-alsa:
https://github.com/Arkq/bluez-alsa
My speaker now automatically pairs and connects when turned on, so that part is good. To get it to actually play the audio stream, the guide recommends creating the following .asoundrc:

Code: Select all

defaults.bluealsa.interface "hci0" 
defaults.bluealsa.device "30:21:3E:31:C6:2B" 
defaults.bluealsa.profile "a2dp" 
defaults.bluealsa.delay 10000
Then once you have turned the volume up in alsamixer, the stream can be played by something like:

Code: Select all

mpv --audio-device: alsa/bluealsa "mms://mediau.yle.fi/liveradiopuhe" # Finnish radio
which is from memory, but I installed mpv, mplayer and vlc to try them all, and each has a different syntax; all work.

The problem is, I want to run it headless, constantly streaming audio (if I want to listen to a different station, I can ssh into the Pi and change it). Then I turn my speaker on in the morning, without having to switch on the Salix desktop and get sucked into the internet and its distractions, and listen to the news in Finnish.

This might be rather like the sort of "plug and play" behaviour you see when using normal AV applications on the desktop - you plug in your headphones, and the loudspeakers go off, if they were turned on in the first place.

But what happens is, whichever player I'm using tries to buffer the stream, and complains tremendously, as soon as the speaker is turned off. Quite apart from that, the Pi expects the speaker to be connected before the stream is started. But then, I have to ssh into it and do it manually, which defeats the whole object.

Does anyone have any suggestions? I am reluctant to mess with the bluetooth part, which works - I tried every which way with pulse, and it was never successful.
User avatar
gapan
Salix Wizard
Posts: 6238
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan »

Not a direct solution to your problem, but have you thought about using a distribution that is dedicated to audio playback like volumio or pimusicbox?
https://volumio.org/
http://www.pimusicbox.com/

You could easily turn it on/off or manage it in any way from your smartphone or hardware buttons you put on the pi itself.

Hardware buttons/switches connected to the pi's GPIO ports could also be used to start/stop applications with your current setup I guess too, requiring some minimal scripting and some wiring of course.
Image
Image
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa »

Thanks, I wasn't aware of those. In my case it would have to be the buttons (which are in any case more in the spirit of the Pi).

You could do it with one button that would restart the stream, after you connected the speaker.

The approach I was thinking of was to create a dummy sound "device" that Alsa would send the stream to when the speaker was not connected, but make the speaker the default so the stream would be diverted to it when available.

Alternatively, perhaps a script that would detect the speaker going on and off and start and stop the stream accordingly. That might actually be easier than delving into the fine points of Alsa.

I must be the last person on the planet who doesn't have a smartphone. But if I did have one, it would defeat the present object, which is to listen to an internet stream without otherwise connecting to the internet and looking for that "important" email or news. The Guardian website gets me every time :lol:
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa »

As an interim solution, I am just leaving the speaker on all the time with the volume muted.

After connecting to it initially through bluetooth, it is enough to do

Code: Select all

ssh pi@raspberrypi
player <stream> &
disown && exit
I wasn't familiar with the disown command, which I think is rather nice. It means the process isn't killed when you exit the shell.
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa »

I've now got round to writing a little script to turn the stream on and off according to whether the speaker is connected, which means I control it with its on and off switch instead of just turning the volume down:

Code: Select all

#!/bin/bash
SWITCH='OFF'

PLAYER='mpv '
OPTIONS='--audio-device=alsa/bluealsa '
STREAM='mms://mediau.yle.fi/liveradiopuhe ' 

while (sleep 10)
do
        if [ `busctl tree org.bluez | wc -l` -lt 5 ] ; then # this tells us the speaker isn't connected, TODO find a better way
#               echo "The speaker is off"
                if [ $SWITCH = 'ON' ] ; then
                        killall $PLAYER
                        SWITCH='OFF'
                fi
        else
                if [ $SWITCH = 'OFF' ] ; then
#                       echo "The speaker has just been turned on"
                        killall $PLAYER
                        $PLAYER $OPTIONS $STREAM > /dev/null 2>&1 &
                        SWITCH='ON'
                fi
        fi

done
The way I'm testing for that with busctl tree is hackish, but works. It's less expensive that querying the bluetooth stack directly.
User avatar
gapan
Salix Wizard
Posts: 6238
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan »

Nice!
Image
Image
User avatar
laprjns
Salix Warrior
Posts: 1105
Joined: 28. Aug 2009, 01:30
Location: Connecticut USA

Re: Raspberry Pi streaming internet radio to bluetooth

Post by laprjns »

mimosa wrote: 10. Jun 2018, 13:12The way I'm testing for that with busctl tree...
Given yourself to the dark side have we.
“Don’t you see that the whole aim of Newspeak is to narrow the range of thought?"
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: Raspberry Pi streaming internet radio to bluetooth

Post by mimosa »

The Pi runs Debian, so, yes :twisted:

There is actually a version of Slack for it, which I was going to try out, but since it works ... I have it on all the time with irssi running in a detached screen session, so I'm back in the Salix chat, too.
User avatar
gapan
Salix Wizard
Posts: 6238
Joined: 6. Jun 2009, 17:40

Re: Raspberry Pi streaming internet radio to bluetooth

Post by gapan »

Slackware 14.2 is softfloat, so I'm not that willing to try it. In current they switched to hardfloat, so next slackware release will be a better match for the pi!
Image
Image
galmei
Posts: 166
Joined: 1. Jun 2018, 21:54

Re: Raspberry Pi streaming internet radio to bluetooth

Post by galmei »

With reference to this viewtopic.php?f=16&t=7717#p44597 message, the booting of a headless Raspberry Pi is picked up. Simply by retrofitting a button and a small program, a shutdown with halt and reboot can be manually generated. With the same button can also be booted again. After shutdown with halt, the power supply can then be switched off.
Link: Shutdown/Start Button https://gilyes.com/pi-shutdown-button/
The link refers to one of many examples. The program shown in this example is a short Python script.

A second button allows a hardware reset.
Link: Momentary Reset Button http://www.savagehomeautomation.com/pro ... utton.html

But it can also achieve more. By adding a single integrated 3-pin IR receiver chip circuit and an IR remote control, the Pi can be taught all the multimedia features, including turning the speaker, headphone and PI itself on and off.

Some of many:
Controlling your Pi with an infrared remote http://www.raspberry-pi-geek.com/Archiv ... red-remote

In German:
Raspberry Pi Tutorials – Raspberry Pi: Per IR Remote Befehle ausführen https://tutorials-raspberrypi.de/raspbe ... e-control/
Jede Infrarot Fernbedienung am Raspberry Pi 2 nutzen – So installierst du günstig einen IR-Empfänger – Part 1 http://powerpi.de/jede-infrarot-fernbed ... er-teil-1/
Jede Infrarot Fernbedienung am Raspberry Pi 2 nutzen – So installierst du günstig einen IR-Empfänger – Part 2 http://powerpi.de/so-richtest-du-dir-pe ... in-teil-2/

The LIRC package is needed.

Additional:
Raspberry pi board GPIO pinout http://www.panu.it/raspberry/
Post Reply