Go Down

Topic: [SOLVED]Start Python script from sketch (Read 2340 times) previous topic - next topic

zeroG

Mar 09, 2016, 05:24 pm Last Edit: Mar 10, 2016, 12:42 pm by zeroG
Hi,

i wrote a little script in python, i saved it into the SD card and i started it through Putty with the command
Code: [Select]
python /mnt/sda1/arduino/www/myScript.py
The script works, it downloads a .txt file from a FTP server and saves it on the SD card.

But when i try to run it from the Arduino sketch, it doesn't start. I read several topics but any of the solution i found works for me. Can you please check my software?

Here's the python script:

Code: [Select]
#!/usr/bin/python

import ftplib

ftp=ftplib.FTP(bla bla bla)
filename = 'data.txt'
localfile = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, localfile.write, 1024)
ftp.quit()


And the Arduino sketch:

Code: [Select]
#include <Bridge.h>
#include <Process.h>

Process p;

void setup() {

  Bridge.begin();
  Serial.begin(9600);
  pinMode(13, OUTPUT);

}

void loop() {

  digitalWrite(13, HIGH);

  //First method
  //p.runShellCommand("python /mnt/sda1/arduino/www/myScript.py");
  //while(p.running());

  //Second method
  p.begin("python");
  p.addParameter("/mnt/sda1/arduino/www/myScript.py");
  p.run();
 
  digitalWrite(13, LOW);
 
  delay(5000);

}


Did i miss anything?

Thanks!

zeroG

#1
Mar 09, 2016, 07:13 pm Last Edit: Mar 09, 2016, 07:14 pm by zeroG
Update:

I tried with several methods without success:

Code: [Select]
//First method
  p.runShellCommand("python /mnt/sda1/arduino/www/myScript.py");
  while(p.running());

  //Second method
  p.begin("python");
  p.addParameter("/mnt/sda1/arduino/www/myScript.py");
  p.run();

  //Third method
  p.begin("python /mnt/sda1/arduino/www/myScript.py");
  p.runAsynchronously();

  //Fourth method
  p.runShellCommandAsynchronously("python /mnt/sda1/arduino/www/myScript.py");
  


I also tried with another Yun board, and i installed the latest version of the bridge library (1.6) on Arduino IDE 1.6.6

supersleeper

Are you sure it's processing to the bash?  I'd try a simple shell script and/or write to txt file.

Also, have you tried embedding your python script into a shell script and launching that?

i.e., nano mypythonscript.sh, add this in and save:  python /mnt/sda1/arduino/www/myScript.py

test launch of script from shell, then test launch of script from sketch.

supersleeper

For example, I'd start out by first doing this word for word to test it, then modify it to your needs:

The runScript() function will create a Process that runs the script and prints the results to the Serial Monitor. Create a named Process, and start your script by calling Process.begin(filepath) and Process.run().

void runScript() {
  Process myscript;
  myscript.begin("/tmp/wlan-stats.sh");
  myscript.run();

Keep in mind, you don't want to store any permanent files in /tmp as they will get wiped in this example.  So if you create a shell script to launch your python script, keep it in your home folder.

mart256

#4
Mar 10, 2016, 03:19 am Last Edit: Mar 10, 2016, 03:28 am by mart256
Ive run scripts as you say and that should work.
Did you mount the sd card first? Each time Yun resets the sd card is unmounted.

Also, is very important to use absolute file paths inside the script, when using the open command you are not specifiyng the data.txt path, it should be like this: open('/path_to_file/data.txt', ..)

zeroG

Also, is very important to use absolute file paths inside the script, when using the open command you are not specifiyng the data.txt path, it should be like this: open('/path_to_file/data.txt', ..)
Thank you, the problem was the data file path inside the script! It has to be an absolute path. Now it works!

Go Up