最新消息:

Arduino库教程-Bridge-Process

Arduino 少儿编程 1530浏览 0评论
Arduino库教程

Process

  • 这个yun设备例子展示了怎样用Bridge库的Process类来在AR9331上运行Linux process。特别地,在这个例子里,你将会用curl 和 cat 来转化从一个web服务器来的数据,并且获得在Linux processor的信息。

硬件要求

  • Yún 开发板 或者 shield
  • 连接到互联网的网络

电路

  • 这个例子没有额外的电路
    Arduino库教程-Bridge-Process
    图由 Fritzing 软件绘制

样例代码

  • 程序包括Process类。 #include <Process.h>

  • 在setup()里,初始化Bridge并且开始串口连接。在运行setup()剩下部分前,等待串口连接被激活。

void setup() {
  Bridge.begin();
  Serial.begin(9600);

  while (!Serial);

[Get Code]

  • setup()剩下部分用来调用你的两个自定义函数,runCurl() 和 runCpuInfo()。loop()里什么都没有。
runCurl();
  runCpuInfo();
}

void loop() {
  // Do nothing here.
}

[Get Code]

  • runCurl()将会开始 curl 命令,并且下载Arduino logo 如ASCII。创建一个命名好的Process并且通过调用myProcess.begin("curl")启动它;用addParameter()函数增加URL来获取,并且用run()运行它们。
void runCurl() {
  Process p;            
  p.begin("curl");      
  p.addParameter("http://arduino.cc/asciilogo.txt"); 
  p.run();

[Get Code]

  • 当从process里有有效数据,打印它到串口监视器:
while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}

[Get Code]

  • 对于runCpuInfo()函数,为cat创建一个新的process。增加参数到cat,使它通过路径到cpu,进入文件夹,然后运行process。
void runCpuInfo() {
  Process p;
  p.begin("cat");       
  p.addParameter("/proc/cpuinfo"); 
  p.run();

[Get Code]

  • 当process有有效数据时,打印它到串口监视器:
while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}

[Get Code]

  • 完整程序如下:
/*
  Running process using Process class.

 This sketch demonstrate how to run linux processes
 using a Yún101/YunShield/Yún

 created 5 Jun 2013
 by Cristian Maglie

 This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Process

 */

#include <Process.h>

void setup() {
  // Initialize Bridge
  Bridge.begin();

  // Initialize Serial
  SerialUSB.begin(9600);

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

  // run various example processes
  runCurl();
  runCpuInfo();
}

void loop() {
  // Do nothing here.
}

void runCurl() {
  // Launch "curl" command and get Arduino ascii art logo from the network
  // curl is command line program for transferring data using different internet protocols
  Process p;        // Create a process and call it "p"
  p.begin("curl");  // Process that launch the "curl" command
  p.addParameter("http://www.arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl"
  p.run();      // Run the process and wait for its termination

  // Print arduino logo over the Serial
  // A process output can be read with the stream methods
  while (p.available() > 0) {
    char c = p.read();
    SerialUSB.print(c);
  }
  // Ensure the last bit of data is sent.
  SerialUSB.flush();
}

void runCpuInfo() {
  // Launch "cat /proc/cpuinfo" command (shows info on Atheros CPU)
  // cat is a command line utility that shows the content of a file
  Process p;        // Create a process and call it "p"
  p.begin("cat");   // Process that launch the "cat" command
  p.addParameter("/proc/cpuinfo"); // Add the cpuifo file path as parameter to cut
  p.run();      // Run the process and wait for its termination

  // Print command output on the SerialUSB.
  // A process output can be read with the stream methods
  while (p.available() > 0) {
    char c = p.read();
    SerialUSB.print(c);
  }
  // Ensure the last bit of data is sent.
  SerialUSB.flush();
}

[Get Code]
更多

  • Bridge: 从网页浏览器进入开发板的引脚。
  • Console ASCII Table: 示范了怎样打印多种格式到控制台。
  • Console Pixel: 通过控制台控制一个LED灯。
  • Console Read: 从控制台那里分析信息,然后重复发送返回。
  • Datalogger: 在SD卡上保存传感器信息。
  • File Write Script: 示范怎样在Process上写入和执行外壳脚本。
  • HTTP Client: 建造一个简单的客户端,可以下载网页并且打印到串口监视器。
  • HTTP Client Console: 建造一个简单的客户端,可以下载网页并且用控制台通过WIFI打印到串口监视器。
  • Mailbox Read Messages: 用REST API通过一个网页发送文本信息到。
  • Process: 示范怎么用Process运行 Linux 命令。
  • Remote Due Blink: 示范怎么远程上传程序到DUE开发板上。
  • Shell Commands: 用Process 来运行 shell 命令。
  • SpacebrewYun: 在Arduino IDE软件的例子上看更多关于 Spacebrew 文档信息。
  • Temboo: 在Arduino IDE软件的例子上看更多关于 Temboo 文档信息。
  • Temperature Web Panel: 当浏览者要求时,粘贴传感数据到网页上。
  • Time Check: 从网络的时间服务器获得时间,并且打印到串口监视器。
  • WiFi Status: 运行一个预配置的脚本,报告返回当前wifi网络的强度。
  • Yun First Config: 用串口监视器不费力地连接你的云产品到wifi网络,并且在上面回答一些简单的问题。
  • Yun Serial Terminal: 通过串口监视器进入Linux终端。

您必须 登录 才能发表评论!