Using process control to play sounds

I am attempting to play sounds on the Raspberry Pi remotely by publishing an event from my phone and having the Pi start a script to play the sound when the event is received. The script, which is located in the /home/pi directory, reads as such:

sudo python /home/pi/

And in

import os
os.system('mpg123 -q Closed.mp3 &')

When I run the script (which is an executable) from the Pi directly, everything works fine. However, when I run it using Process::run(), no sound plays, despite the fact that the error code comes out to 0.

Code being flashed to the Pi:

Process proc = Process::run("/home/pi/");
bool done = proc.exited();
int num = proc.exitCode();
String error = proc.err().readString();
String out = proc.out().readString();

What are you using to play the sound? Is it a shield or speaker plugged into the audio jack?

I’m just using a regular speaker plugged into the audio jack.

Not addressing your actual issue but a hint on how to use Particle.publish() better

  char msg[256];
  snprintf(msg, sizeof(msg), "Exit:%d, Error:%s, Out:%s", num, (const char*)error, (const char*)out);
  Particle.publish("Result", msg, PRIVATE);

This will only publish one even with all the data in it, which will make obeying the publish rate limit easier too.

You may even be able to avoid your intermediate variables (especially temporary String objects might cause heap fragmentation).

1 Like

@13thDoctor which Rpi model are you using?

I haven’t check but it’s likely that the audio GPIO is initialized by the particle firmware when it runs, causing the audio playback to fail.

We should be able to use a temporarily workaround but there’s an issue already opened to fix the GPIO initialization bug. :slight_smile:

As Kenneth mentioned the old GPIO initialization might be interfering with the audio. There was a fix out in prerelease. I just published that release (firmware 0.5.1-pi.13) so you might want to go ahead and reflash to try again.

1 Like