gpt4 book ai didi

python - 编写并保存 python 脚本到 arduino yun RAM

转载 作者:太空狗 更新时间:2023-10-29 12:32:44 26 4
gpt4 key购买 nike

我正在尝试将 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 sketch 中为 linux 编写脚本!干杯。

关于python - 编写并保存 python 脚本到 arduino yun RAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com