Hello everyone,
I am trying to run a simple bash command on my RPi located in home directory but nothing really works. I found code example but I guess it works only with Photon.
You’ll need to feed the run() command an absolute path; run("/home/pi/script.sh")
Also make sure that the script has permission to execute. From the Raspberry Pi command line, run: chmod +x /home/pi/script.sh
This should work as long as your script outputs a float like the example from the doc. If you want to output a string, that’s another story. I’m was still trying to work this one out late into the night. It’s been a while since I’ve wrote any C.
I kept getting errors similar to:
conversion from ‘FdStream’ to non-scalar type ‘String’ requested
Great, what’s FdStream? I don’t know, I can’t find a lick of documentation on it or where it came from. My best clue for how to handle it is the line: float cpuTemp = proc.out().parseFloat();
So we know know we can parse it into a Float, neat. Don’t bother trying parseString(), too easy. But I think we’re on to something.
That page is talking about handling serial data. So it seems that we’re dealing with a similar type of data stream and it mentions a lovely little function called readString()
I had to declare a string on it’s own line: String myIP = proc.out().readString();
And finally
Particle.publish("My IP", String(myIP), PRIVATE);
So my final program looks like this:
// -----------------------------------------
// Execute a script and publish the result
// -----------------------------------------
void setup() {
// Nothing to set up here
}
void loop() {
// Run a simple script that outputs the current IPv4 address for this Raspberry Pi.
Process proc = Process::run("/home/pi/scripts/getIP.sh");
// Wait for the script to finish
proc.wait();
// Convert the FdStream to a string
String myIP = proc.out().readString();
// Publish the event to the Particle cloud. It will be visible in the Console.
Particle.publish("My IP", String(myIP), PRIVATE);
// Repeat after a 30 second pause. Don't need to be running the command constantly.
delay(30000);
}