Problem playing audio file from node red

Hi,
"aplay somefile.wav" with or without -D default works fine om my raspberry to play audio to paired bluetooth speaker.
The same command from within exec node gives error: "aplay: main830: audio open error: unknown error 524"
Other commands issued using exec node work fine.

I controll the moving parts of my christmas village this way but no music ...

What is going wrong?

Thanks in advance

6akaeddy2

This got me audio out of the headphone socket - something similar might work for Bluetooth

Thanks Cymplecy,
I looked at your suggestion. Aplay should play via bluetooth. It does when invoked in a terminal session so I think this is the default now. Using "aplay -D default file.wav" using node red exec however gives the error. Tried other options then "default". "-D pulse" works from terminal. From nore red exec it gives an other error:

ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

aplay: main:830: audio open error: Connection refused

using "paplay file.wav" from exec node results in "Connection failure: Connection refused
pa_context_connect() failed: Connection refused"

I'm out of idees

How are you running node-red? If running it as a service try stopping it (node-red-stop) and run it directly in the terminal using
node-red
If it plays now then possibly the node-red user is not in the audio group.

Hi Colin,
I start node red as a service, but i have to login opening the web interface. Node red exec node "whoami" returns my username, so i think (p)aplay is called as me?

Yede

Have you tried what I suggested?

Hi Colin,
I thought i did without result, now for second time is does work.
I see no user node-red in /etc/passwd? how to add it to group audio then?
added myself to pulse and pulse-access. don't know is that was smart...
What is my next step?
Thanks so far!

Look in the systemd service to see what is the user running the service
cat /lib/systemd/system/nodered.service
There should be lines near the start setting the User and Group.

[service]
Type=simple
#Run as normal pi user - change to the username you wish to ron NodepRED as
User=yede
Group=yede
WorkingDirectory=/home/yede

yede is my username. instead of standard user pi

So is that the same user that you were logged in as when you ran node-red. If so then it is not the group, it must be something to do with the environment when logged in via ssh versus the service running.

yes, same user as logged in via ssh (headless pi zero 1) and started node-red manualy

What OS are you running? You can find this by running lsb_release -c

hello zenofmud,

Bullseye

Tomorrow, tuesday 12-12 no time for reactions. have to go to office ;-(

When using a headless rpi running Bullseye to get aplay to work through an Exec node for Bluetooth speakers I had to make PulseAudio specifically available to Node Red. Have a look at the section of my notes PdeJ - Connect to Bluetooth

1 Like

Thanks. Seems to the point. Will try this tomorrow evening.

A combination of the suggestions of PdJ and Colin seems to work. after rebooting the pizero i now issue "node-red-stop" and "node-red &". then "bluetoothctl power on" and "bluetoothctl connect mac-addres-speaker".
Then the exec node "aplay -D pulse somevfile.wav" works.

Okay for now. Would like a solution whitout manualy restarting node-red in the future. probably start node red not as a service but from login script of user?

You should be able to add those commands into the systemd startup script for node red.
sudo systemctl status nodered will tell you where that is.

Hello Colin,

With my lack of experience I need a bit more guidance.
could you give me another nudge please?

sudo systemctl status nodered just after boot is:

yede@pizero1:~ $ sudo systemctl status nodered
Failed to get journal cutoff time: Bad message
● nodered.service - Node-RED graphical event wiring tool
     Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-12-15 10:12:17 CET; 1min 31s ago
       Docs: http://nodered.org/docs/hardware/raspberrypi.html
   Main PID: 247 (node)
      Tasks: 11 (limit: 414)
        CPU: 33.011s
     CGroup: /system.slice/nodered.service
             └─247 node --max_old_space_size=256 /usr/lib/node_modules/node-red/bin/../red.js

Dec 15 10:12:17 pizero1 systemd[1]: Started Node-RED graphical event wiring tool.
Dec 15 10:12:52 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info]
Dec 15 10:12:52 pizero1 Node-RED[247]: Welcome to Node-RED
Dec 15 10:12:52 pizero1 Node-RED[247]: ===================
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Node-RED version: v3.1.0
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Node.js  version: v18.17.1
Dec 15 10:12:53 pizero1 Node-RED[247]: 15 Dec 10:12:52 - [info] Linux 6.1.21+ arm LE
Dec 15 10:13:29 pizero1 Node-RED[247]: 15 Dec 10:13:29 - [info] Loading palette nodes
Dec 15 10:13:50 pizero1 Node-RED[247]: 15 Dec 10:13:50 - [info] Dashboard version 3.6.2 started at /ui

yede@pizero1:~ $ node-red-stop

after stopping and starting like you told me it is:

● nodered.service - Node-RED graphical event wiring tool
     Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2023-12-15 10:33:24 CET; 3h 41min ago
       Docs: http://nodered.org/docs/hardware/raspberrypi.html
    Process: 247 ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 247 (code=exited, status=0/SUCCESS)
        CPU: 51.453s

Dec 15 10:13:57 pizero1 Node-RED[247]: 15 Dec 10:13:57 - [info] Starting flows
Dec 15 10:13:59 pizero1 Node-RED[247]: 15 Dec 10:13:59 - [info] Started flows
Dec 15 10:13:59 pizero1 Node-RED[247]: 15 Dec 10:13:59 - [info] [mqtt-broker:9ef57e0297253107] Connected to broker: mqtt://localhost:1883
Dec 15 10:33:24 pizero1 systemd[1]: Stopping Node-RED graphical event wiring tool...
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] Stopping flows
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] [mqtt-broker:9ef57e0297253107] Disconnected from broker: mqtt://localhost:1883
Dec 15 10:33:24 pizero1 Node-RED[247]: 15 Dec 10:33:24 - [info] Stopped flows
Dec 15 10:33:24 pizero1 systemd[1]: nodered.service: Succeeded.
Dec 15 10:33:24 pizero1 systemd[1]: Stopped Node-RED graphical event wiring tool.
Dec 15 10:33:24 pizero1 systemd[1]: nodered.service: Consumed 51.453s CPU time.

/lib/systemd/system/nodered.service is:

# systemd service file to start Node-RED

[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=yede
Group=yede
WorkingDirectory=/home/yede

Environment="NODE_OPTIONS=--max_old_space_size=256"
# define an optional environment file in Node-RED's user directory to set custom variables externally
EnvironmentFile=-/home/yede/.node-red/EnvironmentFile
# uncomment and edit next line if you need an http proxy
#Environment="HTTP_PROXY=my.httpproxy.server.address"
# uncomment the next line for a more verbose log output
#Environment="NODE_RED_OPTIONS=-v"
# uncomment next line if you need to wait for time sync before starting
#ExecStartPre=/bin/bash -c '/bin/journalctl -b -u systemd-timesyncd | /bin/grep -q "systemd-timesyncd.* Synchronized to time server"'

ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
#ExecStart=/usr/bin/env node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
RestartSec=20
# Tag things in the log
SyslogIdentifier=Node-RED
#StandardOutput=syslog

[Install]
WantedBy=multi-user.target
------

Actually there may be an easier way. What happens if you run an exec node containing
bluetoothctl power on ; bluetoothctl connect mac-addres-speaker
before your aplay exec node?

Hi Colin,

Sorry for late reaction. Busy period of the year.
I issue both commands. Manuallly when testing, or as part of the script that runs my music, steppermotors and servo's.
For now I can live with the current situation: restart node-red manually after booting the pizero and don't switch it off until next year.
happy hollidays.
Yede