3 Replies Latest reply on Jul 13, 2015 8:52 PM by Sandesh_Ka

    How to make interprocess communication between python script and arduino program running on edison board ?

    Sandesh_Ka

      I want to make a python script to be executed when an event occurs in arduino program , after script is executed its result is passed to the arduino code and rest of execution is carried out. how to make it possible ? Please give the example code of this

        • 1. Re: How to make interprocess communication between python script and arduino program running on edison board ?
          Frederick Blais

          If I recall correctly, in the Arduino program, you can use the system("python /path to your script") function to call your script. system() will return an int that is the exit code of the script.

           

          In your python script, you could import sys and call sys.exit(number) to return that number to your Arduino program. That would be an easy way to do it. Another way would be to use a fifo (named pipe)

          • 2. Re: How to make interprocess communication between python script and arduino program running on edison board ?
            Frederick Blais

            I just tried the exit code trick and it was not working as good as I was expecting. The named pipe way works, here is an example.

             

            The Arduino read 5 random integer generated from a Python script :

             

            sketch:

            FILE *to_python;
            FILE *from_python;
            
            
            char buffer[32];
            
            
            void setup() {
              Serial.begin(9600);
              Serial.println("START");
              system("python /home/root/pipe.py &");
              from_python = fopen("/tmp/pipe1", "r");
              to_python = fopen("/tmp/pipe2", "w");
              if (from_python==NULL)
                Serial.println("Error: PIPE 1");
              if (to_python==NULL)
                Serial.println("Error: PIPE 2");
            }
            
            
            void loop() {
              for(int i=0; i<5; i++) {
                processData();
                displayData( getResults() );
                delay(1000);
              }
            
            
              fputs("STOP", to_python);
              fflush(to_python);
              Serial.println("STOP");
              while(1); //STOP
            }
            
            
            void processData() {
              fputs("GO", to_python);
              fflush(to_python);
            }
            
            
            int getResults() {
              fgets(buffer, 32, from_python);
              return atoi(buffer);
            }
            
            
            void displayData(int data) {
              Serial.print("DATA: ");
              Serial.print(data);
              Serial.println("");
            }
            
            
            
            
            

             

            Python script:

            from random import randint
            
            PATH2 = "/tmp/pipe2"
            PATH2 = "/tmp/pipe2"
            
            
            toArduino = open("/tmp/pipe1", "w")
            fromArduino = open("/tmp/pipe2", "r")
            
            print toArduino
            print fromArduino
            
            while(True):
                print "Reading..."
                data = fromArduino.read(2)
                if data != "GO":
                    toArduino.close()
                    fromArduino.close()
                    quit()
                print "Writing..."
                toArduino.write("{}\n".format(randint(1,100)))
                toArduino.flush()
            
            
            

             

            You just have to create your 2 pipes in the terminal

            mkfifo /tmp/pipe1

            mkfifo /tmp/pipe2


            Good Luck!

            1 of 1 people found this helpful
            • 3. Re: How to make interprocess communication between python script and arduino program running on edison board ?
              Sandesh_Ka

              Thanks a lot Frederick Blais . It worked me for as well . Will get to you if i have any doubts in working more on it