2

I'm trying to save a python script to the onboard linino RAM, but I can't quite get it to work. Am I writing the python script file correctly? Can anyone view my code and tell me where I'm erring? I basically modified code from an example on the arduino site to try to make this work. I just want to write/save to the linino and then print out the output from the serial port. Thanks in advance!

#include <FileIO.h>

void setup() {
// Setup Bridge (needed every time we communicate with the Arduino Yún)
Bridge.begin();
// Initialize the Serial
Serial.begin(9600);

while(!Serial);  // wait for Serial port to connect.
Serial.println("File Write Script example\n\n");

// Setup File IO
FileSystem.begin();

// Upload script used to gain network statistics  
uploadScript();
}  

void loop() 
{
// Run stats script every 5 secs.
runScript();
Serial.println("Just ran script");
delay(5000);
}

// this function creates a file into the linux processor
void uploadScript() 
{
// Write our shell script in /tmp
// Using /tmp stores the script in RAM this way we can preserve 
// the limited amount of FLASH erase/write cycles
File script = FileSystem.open("/tmp/example.py", FILE_WRITE);
script.print("#!/usr/bin/python");
script.print("import urllib2");
script.print("import ast");
script.print("r = urllib2.urlopen('https://python.org')");
script.print("a = r.read()");
//script.print("y = ast.literal_eval(a)");
script.print("print a[:100]"); //i want to index something in the dictionary
script.close();  // close the file

// Make the script executable
Process chmod;
chmod.begin("chmod");      // chmod: change mode
chmod.addParameter("+x");  // x stays for executable
chmod.addParameter("/tmp/example.py");  // path to the file to make it executable
chmod.run();
}


// this function run the script and read the output data
void runScript() 
{
// Run the script and show results on the Serial
Process myscript;
myscript.begin("/tmp/example.py");
myscript.run();

String output = "";

// read the output of the script
while (myscript.available()) 
{
  output += (char)myscript.read();
}
// remove the blank spaces at the beginning and the ending of the string
output.trim();
Serial.println(output);
Serial.println("just rand"); //for debugging
Serial.flush();
}
NoBugs
  • 9,310
  • 13
  • 80
  • 146
mjmostachetti
  • 378
  • 1
  • 4
  • 14
  • Why do you write the script to `/tmp/example.py` but you run `/tmp/redluck2013.py`? `a` is a bytestring. It accepts only integers as an index. `"print a["key12321"]"` looks like an error, try `"print a[:100]"` instead (it should print the first 100 bytes from `https://example.com` (you should probably change the hostname too e.g., `https://python.org`) – jfs Feb 28 '14 at 20:51
  • I made all of your recommended changes, but in the serial monitor, it still only prints my Serial.println debuggin statements. Serial.println(output) doesn't print anything to the serial monitor. During the "Process myscript" section, do I have to tell the Linux command line to run "/tmp/example.py" with python? – mjmostachetti Feb 28 '14 at 21:31
  • I still see `"print a["key12321"]"` in the code. Try to run simple commands: `ls -l /tmp`, `df -h`, `/usr/bin/python -c 'print(1+1)'`, `cat /tmp/example.py`, `python /tmp/example.py 2>&1` – jfs Feb 28 '14 at 22:02
  • Yeah, on my end I edited that code and changed it to "print [a:100]", but still nothing printed in my serial monitor. I'll try the simple commands. – mjmostachetti Feb 28 '14 at 22:14
  • All of these simple commands run the script perfectly in my osx terminal. The ls -l /tmp shows that the example.py is in the tmp directory of the arduino, but it doesn't output anything when I run "python /tmp/example.py 2>&1" on the arduino. – mjmostachetti Feb 28 '14 at 22:26
  • Do I need "\n" after all of my script.print statements? – mjmostachetti Feb 28 '14 at 23:50
  • 1
    Have you tried to read `/tmp/example.py` back, to see its content? Find out how to get Process' exit status and stderr, to see whether child processes print any errors. – jfs Mar 01 '14 at 20:45
  • I'll try that when I get home in a few days. I really appreciate the help, j.f. Sebastian. – mjmostachetti Mar 01 '14 at 21:09
  • I'm still looking for documentation on stderr and exit status, but I also found that when I remove "script.print("r = urllib2.urlopen('https://python.org')");" from the code, the python code has no problems executing. Is there a max length of characters I may use for the URL? The URL in my actual code is 100+ characters. Or is there a per line max # of characters I can use in a sketch? – mjmostachetti Mar 03 '14 at 14:08
  • Does it mean that you can execute `print(1+1)` from `"/tmp/example.py"`? Does your `urllib2` code works on a desktop? – jfs Mar 03 '14 at 14:12
  • Ya, when I run the script from my terminal on my computer it runs flawlessly. I think there's an issue with how I entered the urllib2 line via arduino sketch. – mjmostachetti Mar 03 '14 at 15:41
  • 1
    I would try and find a way to copy Python scripts from the desktop to arduino without retyping them manually. – jfs Mar 03 '14 at 16:16

1 Answers1

1

As I was much more of a noob when I first approached this problem about a year ago, it is clearly much easier to SSH into the Arduino and writing the script in vi and saving the the linux side of the yun. I've had no problem doing this and getting the script to run well. Don't bother trying to write a script to linux in the arduino sketch! Cheers.

mjmostachetti
  • 378
  • 1
  • 4
  • 14