Running a Python script through the Bridge to make sketches lighter

Hi everyboy, In this topic I am going to ask you how running python scriptS through the Yun.
I read on the (http://arduino.cc/en/Guide/ArduinoYun) that : "An installation of Python 2.7 is included with Linino, with which you can write applications or scripts." , so since using this way makes sketches lighter i decided to try, and I failed.

I created a folder in the sd card called 'arduino', and there i put the mailtest.py script, then I use the process example that comes with the arduino ide 1.5.4, modifying pretty by chances the lines:

first attempt: second attempt:

void runCurl() { void runCurl() {
Process p; Process p;
p.begin("python"); p.begin("python<mailtest.py>");
p.addParameter("mailtest.py"); // p.addParameter("/mnt/sd/arduino/mailtest.py");
p.run(); p.run();
} }

both of them failed, can you help me? are there any directories that i need to know?? concerning for example the sd??

THANKS FOR HELPING

Try with

p.begin("python");
p.addParameter("/mnt/sd/arduino/mailtest.py");

How do you know it has run? Should it send an email? If so, does it work when run from the command line alone, without involving the arduino side?

Hi, i run the python script with the powershell(windows 8.1) and it works fine, yes to understand if it works it has to send to me an email, and I have even tried the line you add, but nothing..

And have let it run on Linino using a ssh connection and directly calling your script from Linino command line (terminal)?

no, I just bought the yun and i am not so experienced, it would be great if you can help me...
my goal, anyway, is allowing the sketch to send me a notifications(in this case a mail), for example when a sensor detected something, automatically...thank fr the support!

First you should get a tool to make a ssh connection to Linino.
I'm not sure because I use a Mac, but as far as I know, PuTTY PuTTY: a free SSH and Telnet client should work fine. Maybe someone using Windows can jump in here.
After installing this software make a ssh connection using "ssh -p 22 root@arduino.local". Then run you script like "python /www/sd/myproject/mailtest.py" or "python /mnt/sd/arduino/mailtest.py" if it really is at that location.

BTW: I would help to see the python script you are trying to run. Please post it here.

Agree you need to test your python scripts on the linux side before trying to solve the problem at the sketch side. Putty does work on windows.
Two reasons your script may fail are:

  1. your yun is not configured to access internet
  2. your python script is using modules not installed on the yun (can you paste the script?)

This sketch will run your python script.
And for others looking at this topic, the python script is simelar to this:
http://forum.arduino.cc/index.php?topic=187319.msg1414629#msg1414629

It requre installation of python-openssl

#include <Process.h>

void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial

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

void loop() {
  Process p;
  p.runShellCommand("python /mnt/sd/arduino/www/gmail3_send.py 2>&1");
  while (p.running());
  while (p.available() > 0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
  Serial.println("end");
  delay(5000);  // wait 5 seconds before you do it again
}

After having installed pyhton open-ssl it works, why in your opinion it didn't worked with the previous sketch?

1.) 2>&1 what is this used for? during some attempts(before installing the open-ssl) i received some errors, maybe is this its role?
2.) can i use every kind of python scripts with this method: p.runShellCommand?
3.) does it work even in a function like in the example in the IDE?
4.)in which case is suggested to use addParameter, maybe when we want to pass a variable to the script?

Thanks to EVERYBODY for the support!!!

why in your opinion it didn't worked with the previous sketch?

I think it's bacause you missed the open-ssl.

This will work too

Process p;
  p.begin("python");
  p.addParameter("/mnt/sd/arduino/www/gmail3_send.py");
  p.run();

2>&1 what is this used for

It is redirection the errors to STD_in so errors will be shown in the serial monitor

I don't know if it will work with all other python scripts

Erni:

2>&1 what is this used for

It is redirection the errors to STD_in so errors will be shown in the serial monitor

I don't know if it will work with all other python scripts

It is a common unix command so it will work with all command line executions.
Best regards
Jantje

Thanks jantje

Actually my answer was regarding the last 3 questions

2.) can i use every kind of python scripts with this method: p.runShellCommand?
3.) does it work even in a function like in the example in the IDE?
4.)in which case is suggested to use addParameter, maybe when we want to pass a variable to the script?

hi guys, i am sorry for upping this post again, but got the same problem, for a photobooth camera that upload my images to my ftp server, the sketch take a picture thru runShellCommand, but then when i launch the script it does not work.
if i launche the same command from command line everything works flawlessy.

#include <Bridge.h>

boolean success = false; // a flag to indicate whether we've uploaded the file yet



// Picture process
Process picture;
Process uploadtoftp;

// Filename
String filename;

// Pin
int pir_pin = 8;

// Path
String path = "/mnt/sda1/arduino/";

void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until the serial console is connected
  delay(4000);
  while(!Serial);
  Bridge.begin();

  // Set pin mode
  pinMode(pir_pin,INPUT);
}

void loop(void) 
{
  
  if (digitalRead(pir_pin) == true) {

    Serial.println("CLICK");
    
    // Generate filename with timestamp
    filename = "";
    picture.runShellCommand("date +%s");
    while(picture.running());

    while (picture.available()>0) {
      char c = picture.read();
      filename += c;
    } 
    filename.trim();
    filename += ".jpg";
 
    // Take picture
    picture.runShellCommand("fswebcam --jpeg 80 " + path + filename + " -r 1280x720");
         while(picture.running());

    //launch python script to upload file to ftp
//     uploadtoftp.runShellCommand("/mnt/sda1/arduino/imupftp.py -f " + filename );
  //   while(uploadtoftp.running());

     uploadtoftp.begin("python");
     uploadtoftp.addParameter("/mnt/sda1/arduino/imupftp.py -f " + filename );
     uploadtoftp.run();
     Serial.println("/mnt/sda1/arduino/imupftp.py -f " + filename );
     while(uploadtoftp.running()){

    Serial.println("caricamento");
     }


  }



  
}

and here is the .py placed in /mnt/sda1/arduino folder:

#!/usr/bin/env python

import ftplib
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--filename", "-f", type=str, required=True)
args = parser.parse_args()

print args.filename

session = ftplib.FTP('ftp.daxxxxxxxxana.com','xxxx@xxxx.it','xxpwdxx')
file = open('/mnt/sda1/arduino/' + args.filename,'r')                  # file to send
print file
session.storbinary('STOR /www.danielepezzana.com/download/aurumcam/' + args.filename , file)
file.close()                                    # close file and FTP
session.quit()

DanielePezzana:
if i launche the same command from command line everything works flawlessy.

In my experience, most of the time that a script runs from the command line, and not from a Process object, it's because of relative file paths. When you run from the command line, any file paths that aren't fully qualified are relative to the current directory. But when run from a Process object, you can't make any assumptions about the current directory - it certainly ins't the directory where the script resides, and it certainly isn't the directory where you were logged in while running it from the command line.

I always recommend that any file references used with a Process object, or by a script called from a Process object, should be fully qualified (start with "/" and specify the full path name to the file. Use full pathnames for all files.

A debugging suggestion is to copy the output from a Process object call to the Serial port. This way, you can see the problem if the script generates an error output (for example, file not found because you used relative file paths.) Add something like this each time a Process is complete and you aren't expecting some special output:

  while (uploadtoftp.available()
      Serial.print((char)uploadtoftp.read());

This will read all available output from the process and echo it to the serial port. You just might get an error message that explains why things aren't working as expected.