I have been struggling with this issue for a while. As ShapeShifter explained in 2015 and as far as I understood from the documentations I could find, Process.write() is supposed to write to the standard input of the script running in the Linux side of the Arduino. Here is what ShapeShifted said:
Anything sent with Process.write() can be read by the Python script as standard input.
However no matter what I tried to do, I cannot make this work. Here is my Arduino code:
#include <Console.h>
#include <Process.h>
Process p;
void setup() {
Bridge.begin();
Console.begin();
while(!Console);
Console.println("Console ready.");
p.runShellCommandAsynchronously("/usr/bin/python /root/test.py");
}
void loop() {
Console.println(p.write("Testing"));
p.flush();
delay(500);
}
The reason why I print the output of p.write() is that I want to check how many bytes is sent, because as you know, Process.write() returns the number of bytes written. I have tried with single characters, strings of various lengths, but the results is always 1. That is, p.write() thinks it's writing only 1 byte. Things on the Python side is also odd:
import sys
with open("test.txt", "a") as fd:
fd.write(val + "\n")
while True:
val = sys.stdin.readline().strip()
with open("test.txt", "a") as fd:
fd.write(val + "\n")
What this code snippet is supposed to do is actually pretty straightforward: It takes input and saves to a text file in an infinite loop. When I ssh into the Linux console and run this script manually, everything works as expected. Besides, the line p.runShellCommandAsynchronously("/usr/bin/python /root/test.py"); successfully launches the script from the sketch. Here is the output of ps | grep python command:
1741 root 7376 S python -u bridge.py
2272 root 1492 S /bin/ash -c /usr/bin/python /root/test.py
2273 root 5764 S /usr/bin/python /root/test.py
3461 root 1492 S grep python
However, there wouldn't be any file "text.txt" this time. At first I was thinking that the code blocks at the point where it is waiting for an input, but it seems that the very first lines of the code isn't executed either.
By the way, I have no trouble at the reverse process. That is, I can successfully read output of a Linux process in the sketch using Process.available() and Process.parseInt() (Yes I was trying to communicate integer values). I have no idea why Process.write() just wouldn't work. Any help is sincerely appreciated.