将Python脚本写入并保存到Arduino Yun的RAM中
我正在尝试把一个Python脚本保存到Linino的内存里,但总是搞不定。请问我写的Python脚本文件正确吗?有没有人能看看我的代码,告诉我哪里出错了?
我基本上是根据Arduino网站上的一个例子修改的代码,想让它能正常工作。我只是想把数据写入Linino,然后从串口打印输出。
提前谢谢大家!
#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();
}
1 个回答
1
大约一年前,我刚开始接触这个问题的时候,还是个新手。现在回想起来,直接通过SSH连接到Arduino,然后在vi编辑器里写脚本并保存到Yun的Linux系统上,确实简单多了。我这样做没有遇到任何问题,脚本运行得也很好。别费劲在Arduino的草图里写Linux的脚本了!祝好。