Arduino Yún Process

Demonstrates how to use Process to run Linux commands.

This example for a Yún device 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 Linux processor.

Hardware Required

  • Yún board or shield

  • a network connection to the internet

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

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.

1void setup() {
2
3 Bridge.begin();
4
5 Serial.begin(9600);
6
7 while (!Serial);

The rest of

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

1runCurl();
2
3 runCpuInfo();
4}
5
6void loop() {
7
8 // Do nothing here.
9}

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()
.

1void runCurl() {
2
3 Process p;
4
5 p.begin("curl");
6
7 p.addParameter("http://arduino.cc/asciilogo.txt");
8
9 p.run();

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

1while (p.available()>0) {
2
3 char c = p.read();
4
5 Serial.print(c);
6
7 }
8
9 Serial.flush();
10}

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.

1void runCpuInfo() {
2
3 Process p;
4
5 p.begin("cat");
6
7 p.addParameter("/proc/cpuinfo");
8
9 p.run();

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

1while (p.available()>0) {
2
3 char c = p.read();
4
5 Serial.print(c);
6
7 }
8
9 Serial.flush();
10}

The full code is below :

1/*
2
3 Running process using Process class.
4
5 This sketch demonstrate how to run linux processes
6
7 using a YunShield/Yún
8
9 created 5 Jun 2013
10
11 by Cristian Maglie
12
13 This example code is in the public domain.
14
15 http://www.arduino.cc/en/Tutorial/Process
16
17 */
18
19#include <Process.h>
20
21void setup() {
22
23 // Initialize Bridge
24
25 Bridge.begin();
26
27 // Initialize Serial
28
29 SerialUSB.begin(9600);
30
31 // Wait until a Serial Monitor is connected.
32
33 while (!SerialUSB);
34
35 // run various example processes
36
37 runCurl();
38
39 runCpuInfo();
40}
41
42void loop() {
43
44 // Do nothing here.
45}
46
47void runCurl() {
48
49 // Launch "curl" command and get Arduino ascii art logo from the network
50
51 // curl is command line program for transferring data using different internet protocols
52
53 Process p; // Create a process and call it "p"
54
55 p.begin("curl"); // Process that launch the "curl" command
56
57 p.addParameter("http://www.arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl"
58
59 p.run(); // Run the process and wait for its termination
60
61 // Print arduino logo over the Serial
62
63 // A process output can be read with the stream methods
64
65 while (p.available() > 0) {
66
67 char c = p.read();
68
69 SerialUSB.print(c);
70
71 }
72
73 // Ensure the last bit of data is sent.
74
75 SerialUSB.flush();
76}
77
78void runCpuInfo() {
79
80 // Launch "cat /proc/cpuinfo" command (shows info on Atheros CPU)
81
82 // cat is a command line utility that shows the content of a file
83
84 Process p; // Create a process and call it "p"
85
86 p.begin("cat"); // Process that launch the "cat" command
87
88 p.addParameter("/proc/cpuinfo"); // Add the cpuifo file path as parameter to cut
89
90 p.run(); // Run the process and wait for its termination
91
92 // Print command output on the SerialUSB.
93
94 // A process output can be read with the stream methods
95
96 while (p.available() > 0) {
97
98 char c = p.read();
99
100 SerialUSB.print(c);
101
102 }
103
104 // Ensure the last bit of data is sent.
105
106 SerialUSB.flush();
107}

Last revision 2016/05/25 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.