mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
modified the FileWriteScript example
This commit is contained in:
parent
e6af3acdce
commit
fc880cc566
@ -4,6 +4,9 @@
|
||||
This sketch demonstrate how to write file into the Yún filesystem.
|
||||
A shell script file is created in /tmp, and it is executed afterwards.
|
||||
|
||||
created 7 June 2010
|
||||
by Cristian Maglie
|
||||
|
||||
*/
|
||||
|
||||
#include <FileIO.h>
|
||||
@ -11,15 +14,14 @@
|
||||
void setup() {
|
||||
// Setup Bridge (needed every time we communicate with the Arduino Yún)
|
||||
Bridge.begin();
|
||||
// Initialize the Serial
|
||||
Serial.begin(9600);
|
||||
|
||||
// Setup Console
|
||||
Console.begin();
|
||||
// Buffering improves Console performance, but we must remember to
|
||||
// finish sending using the Console.flush() command.
|
||||
Console.buffer(64);
|
||||
while(!Serial); // wait for Serial port to connect.
|
||||
Serial.println("File Write Script example\n\n");
|
||||
|
||||
// Setup File IO
|
||||
SD.begin();
|
||||
FileSystem.begin();
|
||||
|
||||
// Upload script used to gain network statistics
|
||||
uploadScript();
|
||||
@ -31,35 +33,48 @@ void loop() {
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
// this function creates a file into the linux processor that contains a shell script
|
||||
// to check the network traffic of the WiFi interface
|
||||
void uploadScript() {
|
||||
// Write our shell script in /tmp
|
||||
// Using /tmp stores the script in RAM this way we can preserve
|
||||
// the limited amount of FLASH erase/write cycles
|
||||
File script = SD.open("/tmp/wlan-stats.sh", FILE_WRITE);
|
||||
File script = FileSystem.open("/tmp/wlan-stats.sh", FILE_WRITE);
|
||||
// Shell script header
|
||||
script.print("#!/bin/sh\n");
|
||||
script.print("ifconfig wlan0 | grep \"RX bytes\" | tr ':' ' ' | awk \"{ print \\$3 \\\" \\\" \\$8 }\"\n");
|
||||
script.close();
|
||||
// shell commands:
|
||||
// ifconfig: is a command line utility for controlling the network interfaces.
|
||||
// wlan0 is the interface we want to query
|
||||
// grep: search inside the output of the ifconfig command the "RX bytes" keyword
|
||||
// and extract the line that contains it
|
||||
script.print("ifconfig wlan0 | grep 'RX bytes'\n");
|
||||
script.close(); // close the file
|
||||
|
||||
// Make the script executable
|
||||
Process chmod;
|
||||
chmod.begin("chmod");
|
||||
chmod.addParameter("+x");
|
||||
chmod.addParameter("/tmp/wlan-stats.sh");
|
||||
chmod.begin("chmod"); // chmod: change mode
|
||||
chmod.addParameter("+x"); // x stays for executable
|
||||
chmod.addParameter("/tmp/wlan-stats.sh"); // path to the file to make it executable
|
||||
chmod.run();
|
||||
}
|
||||
|
||||
|
||||
// this function run the script and read the output data
|
||||
void runScript() {
|
||||
// Launch script and show results on the console
|
||||
// Run the script and show results on the Serial
|
||||
Process myscript;
|
||||
myscript.begin("/tmp/wlan-stats.sh");
|
||||
myscript.run();
|
||||
|
||||
Console.print("WiFi RX/TX bytes: ");
|
||||
String output = "";
|
||||
|
||||
// read the output of the script
|
||||
while (myscript.available()) {
|
||||
char c = myscript.read();
|
||||
Console.print(c);
|
||||
output += (char)myscript.read();
|
||||
}
|
||||
Console.println();
|
||||
Console.flush();
|
||||
// remove the blank spaces at the beginning and the ending of the string
|
||||
output.trim();
|
||||
Serial.println(output);
|
||||
Serial.flush();
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
Bridge KEYWORD3
|
||||
FileIO KEYWORD3
|
||||
FileSystem KEYWORD3
|
||||
Console KEYWORD3
|
||||
Process KEYWORD3
|
||||
MailBox KEYWORD3
|
||||
|
Loading…
Reference in New Issue
Block a user