Share data between Python Script and Arduino

So I looked this thread (http://forum.arduino.cc/index.php?topic=188998.0) but as a hobbyst I didn’t understand it.

However I found another way to share strings and stuff between a python script and Arduino using Process library, here’s a quick example:

Arduino code:

#include <Process.h>

void setup() {
  // Initialize Bridge
  Bridge.begin();

  // Initialize Serial
  Serial.begin(9600);

  // Wait until a Serial Monitor is connected.
  while (!Serial);
  runPython(); // call python
}

void loop() {
 //not used
}

//our Python callback
void runPython() {
  
  Process p;        // Create a process and call it "p"
  p.runShellCommand("python /root/any_folder/script.py");  // use CLI commands just like 
                                                     //inside linux console

  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}

Python script:

print "Hello world"

This is tested using Arduino Yun and connecting Serial Monitor to COM8 (or any usb COM).

Once you open Arduino Serial Monitor you should read “Hello World”. Then you can do more complex scripts and use two-way communication.

A nice clean example. Good job!

mart256:
So I looked this thread (http://forum.arduino.cc/index.php?topic=188998.0) but as a hobbyst I didn’t understand it.

Spend a little time trying to figure it out, it’s a very handy mechanism. You can think of it as string variables shared between the two processors. Eithe side can call put() to set a variable, or get() to read it. In addition, you can get or put values remotely using simple HTTP calls. For example:

I’m designing a system now where the Arduino code reads some sensors, setting the values in bridge variables, and sets outputs according to other bridge variables. Some Linux code reads and logs historical sensor variables, while remote nodes use HTTP calls to get current values and set output values. A lot of power, and very little code.

Where the bridge variables fall down, and which causes confusion to some, is these are not commands and responses: when one side changes a values, the other side is not notified, it must get the value and look for a change. Also, either side can get the same value multiple times if it reads faster than the are written, or can miss values if it reads slower. It’s completely asynchronous. That’s not a problem if the code is designed properly, but for those cases where a strictly synchronous call/response is required, your example looks to be a viable sokution

Agreed - the keystore is really simple and powerful.

To address the problem of the two sides not being synchonised/messages being read twice or missed, the Bridge also implements Mailbox. It's even less well documented than Keystore, but there are a few examples around. When the linux side writes to a mailbox it stays there until its read by the AVR, and they're queued so they're never lost (whereas Keystore is idempotent [look it up its a usefull word!] for both read and write)

Yes, mailbox addresses the synchronization issues, and you can send messages using an HTTP call. Both get/put and mailbox have their merits, depending on the situation.

The method in the first post also has it's benefits. It's good that the Bridge toolbox has so many good tools to choose from.