Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alarm audio playback to a Bluetooth speaker from Raspberry Pi Zero W #59

Closed
HumanSkunk opened this issue Aug 22, 2020 · 31 comments
Closed
Labels
stale Hello?

Comments

@HumanSkunk
Copy link

Hi,

Firstly the plugin is awesome I have managed to integrate a number of sensors and Webhooks into the capability however I am looking to getting the alarm to play audio to a connected bluetooth speaker on the Pi.

I have managed to test and verify a bluetooth speaker with the pi and configured the connection with bluetoothctl and succeed in getting audio to playback using bluealsa. The Pi will auto reconnect to the bluetooth speaker when it boots.

I have set the location of the audio file in the config and installed but I am unsure what command is being used to initiate the playback. In order to push via bluetooth and not default to HDMI (which I think its doing) you need to insert arguments into the playback instruction:

aplay -D bluealsa /home/pi/alarm.wav

Would there be any way to have this feature added? Or if it is a function that is currently built in could you point me in the right direction to resolve?

@HumanSkunk HumanSkunk added the enhancement New feature or request label Aug 22, 2020
@MiguelRipoll23
Copy link
Owner

Hello!

The command that is used to play audio under the hood:

ffplay -loglevel error -nodisp -i (PLUGIN_PATH_HERE)/src/../sounds/en-US/target-home.mp3 -autoexit

One of the things that we noticed while investigating #44 is that in order to change the audio output device you need to change a system variable:

  1. Edit your ~/.bashsrc
  2. Add the following lines at the end of the file:

export AUDIODEV=hw:1,0
export SDL_AUDIODRIVER=alsa

  1. Save the file.
  2. Try the plugin.

You may need to try hw:2,0, hw:3,0 until you finally get sound from your Bluetooth speaker.

@HumanSkunk
Copy link
Author

Thanks for the quick response. Probably a dumb location but where is the bashsrc file located so I am able to edit it? Is it within the plugin folder?

@MiguelRipoll23
Copy link
Owner

MiguelRipoll23 commented Aug 22, 2020

There are no dumb questions, the ~ symbol means the user directory :)

So if you are logged in as root the file path would be /root/.bashrc on your Raspberry Pi.

@HumanSkunk
Copy link
Author

doh! It was only when I tried to edit it the first time it was coming up empty so I knew I was in the wrong file. I have now added the lines to the file and will give it a go.

If I leave the audio path blank does it default to the sounds included in the plugin?

@MiguelRipoll23
Copy link
Owner

Yup! The default sounds are simple informative messages generated with a Google AI voice.

@HumanSkunk
Copy link
Author

I have gone through and changed the file .bashrc file in my user directory to both 1,0 , 2,0 and 3,0 but none playback the alarm noise when I manually trigger it.

I know the speaker is working as if I run:

aplay -D bluealsa /home/pi/alarm.wav

It plays...

A bit lost now. I see there is a command option in the settings can I simply add the above command under the "Current Mode: Triggered"?

@MiguelRipoll23
Copy link
Owner

Can you paste the output of the following command here to see which identifier corresponds to your speaker?

aplay -l

BTW, yes you can use the command options to play the audio file manually but the plugin won’t be able to stop the sound.

@HumanSkunk
Copy link
Author

Ran the command and got this output:

**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7

@MiguelRipoll23
Copy link
Owner

MiguelRipoll23 commented Aug 22, 2020

It looks like you have to play with the subdevice identifier, can you try to change the number in bold (from zero to seven)

export AUDIODEV=hw:0,0
export SDL_AUDIODRIVER=alsa

restart Homebridge and use the plugin?

@HumanSkunk
Copy link
Author

Should I restart Homebridge after each change to the bashrc file, even though that file isn't being specifically read by Homebridge itself?

@MiguelRipoll23
Copy link
Owner

Yes, restart it after each change as the plugin creates a shell that needs the updated environment variables for the audio command.

@HumanSkunk
Copy link
Author

Thanks. I will run through it methodically and update. Homebridge takes to a while to restart on the Pi Zero W!

Just to rule out me incorrectly calling the sound file. If I simply want to the use the alarm.wav file, do I simply put the file name /home/pi/alarm.wav in the audio path setting? Or do I have to name it 'current_triggered.mp3' so it plays back the correct audio file?

@MiguelRipoll23
Copy link
Owner

MiguelRipoll23 commented Aug 22, 2020

On the Custom Sounds Path option you have to set the path of a directory, for example /root/sounds, then when you restart Homebridge the plugin will create a file there indicating that you have to use a specific name as you’re mentioning.

Sorry! I need to improve the documentation but I’ve been quite busy and right now I’m finishing another project.

@MiguelRipoll23
Copy link
Owner

Oh, and even though the extension is different i think it may play the sound anyways so use .mp3.

@HumanSkunk
Copy link
Author

So I went through the process 8 times changing the subdevice identifier but seemingly didn't resolve it. Still unsure what the issue is...

Just to minimise the variables I just used the default sounds.

@MiguelRipoll23
Copy link
Owner

Hopefully this works:

export AUDIODEV=bluealsa
export SDL_AUDIODRIVER=alsa

(Restart Homebridge and test)

@HumanSkunk
Copy link
Author

so... I still cant get any sound output when I change the states in the plugin but when I run: ffplay -loglevel error -nodisp -i /usr/local/lib/node_modules/homebridge-securitysystem/sounds/en-US/current-triggered.mp3 -autoexit I get the audio sent to the speaker.

Which seems to me I may have something set wrong in the settings of the app. I have pasted a crop of the config below I have a couple of Webhooks so I have obfuscated those for obvious reasons!

{
"name": "Home Alarm",
"default_mode": "Off",
"disabled_modes": [
"Home",
"Night"
],
"arm_seconds": 0,
"show_mode_pause_switch": false,
"pause_minutes": 0,
"siren_sensor": false,
"siren_sensor_seconds": 5,
"trigger_seconds": 0,
"reset_minutes": 10,
"siren_switch": true,
"siren_mode_switches": false,
"override_off": false,
"unsafe_mode_switches": false,
"hide_mode_off_switch": false,
"audio": true,
"audio_volume": "50",
"audio_language": "en-US",
"audio_alert_looped": false,
"save_state": false,
"test_mode": false,
"webhook_url": "https://maker.ifttt.com/trigger",
"webhook_target_away": "/xxxxxxx/with/key/xxxxxxx",
"webhook_target_off": "/xxxxxxx/with/key/xxxxxxx",
"accessory": "security-system"
}

@MiguelRipoll23
Copy link
Owner

MiguelRipoll23 commented Aug 22, 2020

That’s really weird.

Set the audio volume option to 100 just in case, also run in debug mode by using homebridge -D.

There you would see the full command that the plugin is executing, if it works when you execute it by yourself then it could be a permissions issue.

Can you confirm me if it works by executing the command that gets printed in debug mode?

@HumanSkunk
Copy link
Author

100% a permission issue. Getting closer to a resolution.

I run Homebridge with pm2 usually just to auto start it when the pi boots. So stopping Homebridge with pm2 and running it direct in the terminal with -D plays perfectly as expected and this is the log output:

8/22/2020, 22:27:04] [Home Alarm] Current mode (Off)
[8/22/2020, 22:27:04] [Home Alarm] Command option for current mode is not set.
[8/22/2020, 22:27:04] [Home Alarm] Webhook option for current mode is not set.
[8/22/2020, 22:27:04] [Home Alarm] ffplay -loglevel error -nodisp -i /usr/local/lib/node_modules/homebridge-securitysystem/src/../sounds/en-US/current-off.mp3 -autoexit -volume 50
[8/22/2020, 22:27:05] [Home Alarm] Webhook event (Sent)

So for some reason pm2 is not allowing it. I set up pm2 ages ago so will try and edit the config so it will run Homebridge in debug mode and report back.

@HumanSkunk
Copy link
Author

I have now removed pm2 and am using hb-service. After a bit of panic as I thought it completely obliterated my config (as it sets up a new instance) I have managed to restore everything over by moving my config files over to the new directory... but things still aren't right.

If I run Homebridge as a service by sudo hb-service start, (which exists in var/lib/homebridge) Homebridge will start fine run fine but won't play any of the audio commands.

If I run Homebridge directly by homebridge, (which exists in /home/pi/.homebridge) Homebridge will start fine and run fine but will play the audio commands.

Do you know if there is any way to start the hb-service in debug mode? I have tried looking through the help pages but there isn't anything obvious.

@MiguelRipoll23
Copy link
Owner

On the Homebridge UI, navigate to Config, click Settings on the Homebridge Config UI plugin and expand the Advanced section then check Enable / disable debug level logging.

Can you check which user allows the sound to be played? Either root, pi or homebidge.

@HumanSkunk
Copy link
Author

so with debug logging there are no permission errors seen in the log, the output is identical to a previous post:

[8/23/2020, 00:33:42] [Home Alarm] Current mode (Away)
[8/23/2020, 00:33:42] [Home Alarm] Command option for current mode is not set.
[8/23/2020, 00:33:42] [Home Alarm] Webhook option for current mode is not set.
[8/23/2020, 00:33:42] [Home Alarm] ffplay -loglevel error -nodisp -i /usr/local/lib/node_modules/homebridge-securitysystem/src/../sounds/en-US/current-away.mp3 -autoexit -volume 50
[8/23/2020, 00:33:43] [Home Alarm] Webhook event (Sent)

I am running the hb-service as the pi user as I installed it with the command sudo hb-service install --user pi Running it this way won't play the sound.

running Homebridge directly from the terminal is calling it from the npm directory of /usr/local/lib/node_modules/homebridge/binwhich has the pi user permissions. Running it this way allows the sound to be played.

One option to try is running hb-service as root, but I don't really want to do that long term.

@MiguelRipoll23
Copy link
Owner

Maybe the steps followed here can help you:
arkq/bluez-alsa#2 (comment)

It looks like it requires root but it can be changed to allow non-root users 🙂

@HumanSkunk
Copy link
Author

Will give that a look tomorrow, it is getting pretty later here tonight and I have been trying with this for a bit too long!

I tried running hb-service as root but it wouldn't start. No logs were added to the log file and the config-ui wouldn't start.

So annoying that it works when Homebridge isn't being run as a service. It 100% is a permission issue, but I would have thought that running it as a service would be running it with high permissions than locally within my user directory.

@HumanSkunk
Copy link
Author

I will admit playing around with deamons for bluetooth looks a little bit outside of my comfort zone...

@MiguelRipoll23
Copy link
Owner

That’s understandable.

I’ll check this out next week and look for alternatives.

@HumanSkunk
Copy link
Author

Did a bit more playing which ultimately caused my to brick the pi due to running a command which obliterated a large amount of files! Luckily I took a back up of the Homebridge config.

After a morning of rebuilding, I used the Homebridge image in the end as I was running Stretch Lite anyways. Using the Config-X restore it rebuilt everything and loaded my original config, installed bluealsa and connected my speaker.

Added the lines to the .bashrc and gave the whole thing a restart. Ran the ffplay -loglevel error -nodisp -i /usr/local/lib/node_modules/homebridge-securitysystem/sounds/en-US/current-triggered.mp3 -autoexit and audio came out the speaker. Annoyingly using the home app and changing the alarm state does not trigger it like before.

@HumanSkunk
Copy link
Author

In the end I have purchased a cheap USB speaker and connected it to the Pi. With a bit of tweaking on the default sound card I have it outputting successfully to the device. Still cant work out why the bluetooth connection isn't working, but it isn't something I need right now.

@MiguelRipoll23
Copy link
Owner

Once I got some free time I’d like to investigate what’s happening here, I consider it quite important for the plugin and I definitely want it working.

I’ll see what I can do, soon hopefully.

@HumanSkunk
Copy link
Author

Let me know if you want me to test anything or generate any logs if you need anything to help debug it. Happy to help.

@MiguelRipoll23 MiguelRipoll23 added the pinned All the way up label Aug 28, 2020
@MiguelRipoll23 MiguelRipoll23 added help wanted Extra attention is needed and removed enhancement New feature or request labels Apr 10, 2021
@MiguelRipoll23 MiguelRipoll23 removed the pinned All the way up label Jun 6, 2021
@MiguelRipoll23 MiguelRipoll23 removed the help wanted Extra attention is needed label Jul 30, 2021
@github-actions
Copy link
Contributor

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale Hello? label Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Hello?
Projects
None yet
Development

No branches or pull requests

2 participants