Learning   Examples | Foundations | Hacking | Links

Examples > Process

Process

This example for the Arduino Yún shows how to use the Bridge library's Process class to run Linux processes on the AR9331. Specifically, in this example, you'll be using curl and cat to transfer data from a web server and get information on the Linino's processor.

Hardware Required

  • Arduino Yún
  • a network connection to the internet

Circuit

There is no circuit for this example.

Code

Include the Process class in your sketch. #include <Process.h>

In setup(), you'll want to initialize Bridge and start a serial connection. Before running the rest of setup() wait for a serial connection to become active.

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

  while (!Serial);

The rest of setup() is used to call your two custom functions, runCurl() and runCpuInfo(). There's nothing in loop().

runCurl();
  runCpuInfo();
}

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

runCurl() will launch the curl command and download the Arduino logo as ASCII. Create a named Process and start it by calling myProcess.begin("curl");. Add the URL to retrieve with the addParameter() method, and run it all with run().

void runCurl() {
  Process p;           
  p.begin("curl");     
  p.addParameter("http://arduino.cc/asciilogo.txt");
  p.run();

When there is data available from the process, print it out to the serial monitor :

while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}

For the runCpuInfo() function, you'll create a new process for cat. Add the parameter to cat passing it the path to the cpu Info file, then run the process.

void runCpuInfo() {
  Process p;
  p.begin("cat");      
  p.addParameter("/proc/cpuinfo");
  p.run();

When there is data available from the process, print it out to the serial monitor :

while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}

The full code is below :

/*
  Running process using Process class.

 This sketch demonstrate how to run linux processes
 using an Arduino Yún.

 created 5 Jun 2013
 by Cristian Maglie

 This example code is in the public domain.

 */


#include <Process.h>

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

  // Initialize Serial
  Serial.begin(9600);

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

  // 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://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();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.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 Serial.
  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
}
 

Share