[136] | 1 | /*
|
---|
| 2 | Running shell commands using Process class.
|
---|
| 3 |
|
---|
| 4 | This sketch demonstrate how to run linux shell commands
|
---|
| 5 | using an Arduino Y炭n. It runs the wifiCheck script on the Linux side
|
---|
| 6 | of the Y炭n, then uses grep to get just the signal strength line.
|
---|
| 7 | Then it uses parseInt() to read the wifi signal strength as an integer,
|
---|
| 8 | and finally uses that number to fade an LED using analogWrite().
|
---|
| 9 |
|
---|
| 10 | The circuit:
|
---|
| 11 | * Arduino Y炭n with LED connected to pin 9
|
---|
| 12 |
|
---|
| 13 | created 12 Jun 2013
|
---|
| 14 | by Cristian Maglie
|
---|
| 15 | modified 25 June 2013
|
---|
| 16 | by Tom Igoe
|
---|
| 17 |
|
---|
| 18 | This example code is in the public domain.
|
---|
| 19 |
|
---|
| 20 | http://arduino.cc/en/Tutorial/ShellCommands
|
---|
| 21 |
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include <Process.h>
|
---|
| 25 |
|
---|
| 26 | void setup() {
|
---|
| 27 | Bridge.begin(); // Initialize the Bridge
|
---|
| 28 | Serial.begin(9600); // Initialize the Serial
|
---|
| 29 |
|
---|
| 30 | // Wait until a Serial Monitor is connected.
|
---|
| 31 | while (!Serial);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | void loop() {
|
---|
| 35 | Process p;
|
---|
| 36 | // This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then
|
---|
| 37 | // sends the result to the grep command to look for a line containing the word
|
---|
| 38 | // "Signal:" the result is passed to this sketch:
|
---|
| 39 | p.runShellCommand("/usr/bin/pretty-wifi-info.lua | grep Signal");
|
---|
| 40 |
|
---|
| 41 | // do nothing until the process finishes, so you get the whole output:
|
---|
| 42 | while (p.running());
|
---|
| 43 |
|
---|
| 44 | // Read command output. runShellCommand() should have passed "Signal: xx&":
|
---|
| 45 | while (p.available()) {
|
---|
| 46 | int result = p.parseInt(); // look for an integer
|
---|
| 47 | int signal = map(result, 0, 100, 0, 255); // map result from 0-100 range to 0-255
|
---|
| 48 | analogWrite(9, signal); // set the brightness of LED on pin 9
|
---|
| 49 | Serial.println(result); // print the number as well
|
---|
| 50 | }
|
---|
| 51 | delay(5000); // wait 5 seconds before you do it again
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 |
|
---|