Page 1 of 1

Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Wednesday 23 November 2016 0:33
by dutchnld
Hi All,

I've been looking all over the forum but couldnt find anything related to my wish...
I'd like to use the 3.5MM audio output of the raspberry pi to play a sound (whatever) when something happens.

How can you trigger a sound when a (virtual) switch turns off/on?
Preferably a long sound, but if it can play a defined .wav or .mp3 thats flexible using the right file.

Usage examples:

- Your door sensor is activated after 2AM, while your sleeping -> Sound alert!
- A button or dummy switch is activated -> you get a (happy) sound alert. (Dishwasher is ready, at a certain time (alarm), etc ...)

I really hope that one of you was able to control the audio-output of a Pi using Domoticz!

Thanks!

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Wednesday 23 November 2016 3:10
by trixwood
Script the notification yourself :-)

Setup your pi for sound...

http://www.raspberrypi-spy.co.uk/2013/0 ... ine-audio/



Create your own script file in the script folder... use aplay :-) or any of the alternatives...
something like

Code: Select all

#!/bin/bash
aplay ~/Luftalarm.wav
(with the right location/name of the music file & player commando...)

Fill it in into Settings->Notification->Custom HTTP/Action (or create a virtual sensor which triggers the script)

And you got what you want...

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Sunday 27 November 2016 22:54
by dutchnld
Hi trixwood,

Thanks a lot for your suggestion!

With your advice I only got halfway unfortunately.
I'm able to execute a script and play a .wav file perfectly, using the commandline.
Both a .sh and .pl script work.

/home/pi/domoticz/scripts/wavtest.sh
/home/pi/domoticz/scripts/wavtest.pl

But when I enter it in a dummy switch nothing happens.
I use this url in the on/off action:

script:///home/pi/domoticz/scripts/wavtest.sh
or
script:///home/pi/domoticz/scripts/wavtest.pl


Normal domoticz log (error log is empty!):
2016-11-27 22:47:09.252 User: jonas initiated a switch command (410/AlarmScript/On)
2016-11-27 22:47:10.222 Executing script: /home/pi/domoticz/scripts/wavtest.sh
2016-11-27 22:47:11.301 User: jonas initiated a switch command (410/AlarmScript/Off)
2016-11-27 22:47:12.292 Executing script: /home/pi/domoticz/scripts/wavtest.pl
2016-11-27 22:47:13.253 User: jonas initiated a switch command (410/AlarmScript/On)
2016-11-27 22:47:14.239 Executing script: /home/pi/domoticz/scripts/wavtest.sh


Contents of my script:

#!/bin/bash
aplay ~/police_s.wav > /dev/null 2>&1 &


I tried the following:
- changed permissions with sudo chmod +x wavtest.sh (and wavtest.pl)
- changed owner with
sudo chown pi:pi wavtest.pl
sudo chown pi:root wavtest.pl
- changed your script by adding /dev/... (read it in the forum)
- even tried to run the .pl file with a .sh file (works in command line)


Any help is greatly appreciated!

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 28 November 2016 10:45
by trixwood
replace

script:///home/pi/domoticz/scripts/wavtest.sh

with

script://wavtest.sh

script:// points to the script folder of domoticz :-)

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 28 November 2016 12:18
by oopee
If above (script path) does not help, try to include full path to aplay in your script.

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 28 November 2016 19:59
by dutchnld
Hi trixwood and oopee,

i tried both your suggestions but still no sound. :(

First I changed the path and tried my dummy switch -> nothing
Then I got into the commandline and typed 'which aplay' and it returned /usr/bin/aplay

So I changed the script to:

Code: Select all

#!/bin/bash
/usr/bin/aplay ~/police_s.wav > /dev/null 2>&1 &
My log is still showing no errors and gives this:

2016-11-28 19:51:15.049 User: Admin initiated a switch command (410/AlarmScript/Off)
2016-11-28 19:51:16.019 Executing script: /home/pi/domoticz/scripts/wavtest.sh
2016-11-28 19:51:17.190 User: Admin initiated a switch command (410/AlarmScript/On)
2016-11-28 19:51:18.173 Executing script: /home/pi/domoticz/scripts/wavtest.sh

What I would like to do is have the most simple (hello world?) script to check if the combination of my dummy-switch and the script really works.
Then I only have to focus on the audio script, which then only works in command line.

Thanks again!

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 28 November 2016 22:20
by pvm
When the script is executed by any other user than pi it will not find the sound file. Replace the ~/ with the full path: /home/pi/

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 28 November 2016 22:44
by dutchnld
@pvm you are the (wo)man of the day!!!

problem is solved by adding the full path of the .wav file.

After that I also removed the full path of aplay and it still works.

Final script:
#!/bin/bash
aplay /home/pi/police_s.wav > /dev/null 2>&1 &
Usage examples:

- cat entered the house after 9PM (so i can close the lock and won't find dead birds/mice on the stairs they catch in the middle of the night)
- email from specific person/company arrives (it opens a virtual switch via outlook run a program) and can now play a sound!
- when a burglar enters the house it plays a nasty loud noise (although they know where my Pi is then....)
- the doorbutton is pressed and a sound plays (cheap extra doorbel this way) ( i have 2 clickonclickoff doorbells around the house)
- and probably many more i find useful later....

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Tuesday 29 November 2016 19:00
by capman
A want the same thing. But my domoticz is running on a synology , and a running max2play on a seperate raspberry pi. A think that a just use the script and change it to the ip adres of the rpi ?

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Tuesday 29 November 2016 19:13
by pvm
I'm not a max2play expert but I guess you have to clear the current playlist and put the required track in?

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Friday 20 January 2017 12:42
by EDsteve
Hi,

i am working on something similar. Sound should play after event triggers. My script as posted in this threat:

Code: Select all

#!/bin/bash
aplay /usr/share/sounds/alsa/Front_Center.wav  > /dev/null 2>&1 &
The script runs perfectly from the command line with sudo.

But domoticz just shows this without error or sound:

Code: Select all

2017-01-20 18:51:41.964 Executing script: /home/fa/domoticz/scripts/creepsound.sh
SOLUTION:
my user was not in the audio group.
For linux noobs like me:

Code: Select all

sudo usermod -g <audio> username

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 06 March 2017 15:42
by MCPXXL
Hello
I am really new in pi and scripting as a windows user

I have the same problems like the other or better more of them because everything is a miracle atm

So i got running my aplay script from command prompt with ./mp3.sh on my usb audio

Code: Select all

#!/bin/bash
sudo aplay ~/mp3/bob_marley-a_la_la_la_la_long_v2.wav  > /dev/null 2>&1 &
exit 0

On command prompt i run this without messages and sound plays until it ends
pi@raspberrypi:~/domoticz/scripts$ /home/pi/domoticz/scripts/mp3.sh



On Activation:
script:///home/pi/domoticz/scripts/mp3.sh

After a lot of 256 errors i now get this shown in log

2017-03-06 15:20:10.314 Executing script: /home/pi/domoticz/scripts/mp3.sh

But no sound is to hear :-(


Is there any idea what the hell i am to stupid for ?

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Monday 13 March 2017 16:48
by MCPXXL
no one any idea ?

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Tuesday 06 November 2018 20:59
by OTiby
I've the same problem

Everything is standard, followed: https://www.raspberrypi-spy.co.uk/2013/ ... ine-audio/

I hear the sound when I run in the command line:

Code: Select all

aplay /home/pi/police_s.wav > /dev/null 2>&1 &
But when I make a dummy switch in Domoticz and add as 'On Action':

Code: Select all

script://aplay/home/pi/police_s.wav > /dev/null 2>&1 &
It doesn't work, I don't hear any sound

In Domoticz I use a user: admin

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Wednesday 07 November 2018 18:48
by OTiby
Found it,

The script wasn't executable yet..


But now I've another problem, when the music plays, domoticz doesn't execute other events.

So I want to flash a light, it wait till the music is ended.
But in the event maker I set them on at the same moment.. :S

Is the rpi or domoticz not capable to execute this together?

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Friday 09 November 2018 9:33
by lost
OTiby wrote: Wednesday 07 November 2018 18:48 The script wasn't executable yet..
But now I've another problem, when the music plays, domoticz doesn't execute other events.
(...)
Is the rpi or domoticz not capable to execute this together?
1: Your script call line is directly using aplay (not a .sh script file with this line inside): How can aplay be installed without +x rights?
2: Not sure, directly calling aplay, you're last & for running the sound in background is taken into account.

Try making a true script file, with commands launched from it in background when needed, so Domoticz event system does not have to wait till you're command end before going ahead.

I have such speech synthesis notifications (using espeak) that for sure do not lock Domoticz when processed (they are indeed sometimes mixed together when they overlap)!

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Sunday 16 June 2019 21:48
by Futuro135
Hi all!

I want domoticz notifications to play through google home mini.
I paird raspberry pi and google home mini.
I made a script that plays an audio file. If I run it from the terminal, then the sound plays in google home. If the script is launched from Domoticz, then the sound plays through the built-in sound card.
Can anyone suggest what to do?
Here is the script

Code: Select all

#!/bin/bash
/usr/bin/mplayer -ao alsa /home/pi/mysound.mp3 > /dev/null 2>&1

Re: Play Audio alert / Sound using the 3.5 MM Raspberry Pi connector

Posted: Wednesday 20 January 2021 12:13
by Remco
Hi There, I've got the same script as above; when using in terminal the sounds play well, also on the good 3,5 mm output. only when I want to activate the script in Domoticz it shows in the log that its executed but the sound isn't playing over te speakers on that 3,5mm output. I read something about user rights for the audio group, but which user should I add? Pi and root is recently added and didn't fix the problem.