簡體   English   中英

將python腳本寫入並保存到arduino yun RAM

[英]Writing and saving python script to arduino yun RAM

我正在嘗試將python腳本保存到板載linino RAM中,但是我無法完全正常工作。 我是否正確編寫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();
}

大約一年前,當我初次接觸到這個問題時,我還是個菜鳥,顯然,SSH進入Arduino並在vi中編寫腳本並保存yun的linux方面要容易得多。 我這樣做並讓腳本正常運行沒有問題。 不要費心嘗試在arduino草圖中向Linux編寫腳本! 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM