Arduino Yún Shell Commands
Use Process to run shell commands.
This sketch demonstrates running Linux shell commands on a Yún device.
It runs the wifiCheck script (located at /usr/bin/pretty-wifi-info.lua) on the Linux processor, then uses grep to get the signal strength.
On the board, parseInt() is called to read the Wi-Fi signal strength as an integer, and uses that number to fade an LED with
analogWrite()
.Hardware Required
Yún board or shield
Yún device connected to a wireless network
220 ohm resistor
LED
hook-up wires
breadboard
Circuit
A red LED connected to digital pin 9 through a 220 ohm resistor on the breadboard.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic
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 the sketch, wait for a serial connection to become active.1void setup() {2
3 Bridge.begin();4
5 Serial.begin(9600);6
7 while (!Serial);8}
Create a named Process with which you'll use to run the Wi-Fi status script and grep.
1void loop() {2
3 Process p;
Pass
runShellCommand()
the path of the script you wish to run, along with any additional commands. In this case, you'll call "grep Signal" to pull out just the signal strength of the Wi-Fi connection.p.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal");
Wait until the process finishes so you get the entire output
while(p.running());
Once the process has finished running, use
parseInt()
to look for an integer that represents the signal strength. It should be in the range of 0 - 100. Map the result to a value between 0 and 255 with map()
and use that value to adjust the brightness of the LED on pin 9 with analogWrite()
. Print the signal strength to the serial monitor and wait for a few seconds before starting again.1while (p.available()) {2
3 int result = p.parseInt();4
5 int signal = map(result, 0, 100, 0, 255);6
7 analogWrite(9, signal);8
9 Serial.println(result);10
11 }12
13 delay(5000);14}
The LED should change its brightness as the Wi-Fi signal strength fluctuates.
Complete Sketch
The complete sketch is below:
1/*2
3 Running shell commands using Process class.4
5 This sketch demonstrate how to run linux shell commands6
7 using a YunShield/Yún. It runs the wifiCheck script on the Linux side8
9 of the Yún, then uses grep to get just the signal strength line.10
11 Then it uses parseInt() to read the wifi signal strength as an integer,12
13 and finally uses that number to fade an LED using analogWrite().14
15 The circuit:16
17 * YunShield/Yún with LED connected to pin 918
19 created 12 Jun 201320
21 by Cristian Maglie22
23 modified 25 June 201324
25 by Tom Igoe26
27 This example code is in the public domain.28
29 http://www.arduino.cc/en/Tutorial/ShellCommands30
31 */32
33#include <Process.h>34
35void setup() {36
37 Bridge.begin(); // Initialize the Bridge38
39 SerialUSB.begin(9600); // Initialize the Serial40
41 // Wait until a Serial Monitor is connected.42
43 while (!SerialUSB);44}45
46void loop() {47
48 Process p;49
50 // This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then51
52 // sends the result to the grep command to look for a line containing the word53
54 // "Signal:" the result is passed to this sketch:55
56 p.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal");57
58 // do nothing until the process finishes, so you get the whole output:59
60 while (p.running());61
62 // Read command output. runShellCommand() should have passed "Signal: xx&":63
64 while (p.available()) {65
66 int result = p.parseInt(); // look for an integer67
68 int signal = map(result, 0, 100, 0, 255); // map result from 0-100 range to 0-25569
70 analogWrite(9, signal); // set the brightness of LED on pin 971
72 SerialUSB.println(result); // print the number as well73
74 }75
76 delay(5000); // wait 5 seconds before you do it again77}
Last revision 2016/05/25 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.