1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-20 21:01:25 +03:00

modified the FileWriteScript example

This commit is contained in:
Fede85
2013-07-01 16:20:05 +02:00
parent e6af3acdce
commit fc880cc566
2 changed files with 35 additions and 19 deletions

View File

@ -4,6 +4,9 @@
This sketch demonstrate how to write file into the Yún filesystem. 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. A shell script file is created in /tmp, and it is executed afterwards.
created 7 June 2010
by Cristian Maglie
*/ */
#include <FileIO.h> #include <FileIO.h>
@ -11,15 +14,14 @@
void setup() { void setup() {
// Setup Bridge (needed every time we communicate with the Arduino Yún) // Setup Bridge (needed every time we communicate with the Arduino Yún)
Bridge.begin(); Bridge.begin();
// Initialize the Serial
Serial.begin(9600);
// Setup Console while(!Serial); // wait for Serial port to connect.
Console.begin(); Serial.println("File Write Script example\n\n");
// Buffering improves Console performance, but we must remember to
// finish sending using the Console.flush() command.
Console.buffer(64);
// Setup File IO // Setup File IO
SD.begin(); FileSystem.begin();
// Upload script used to gain network statistics // Upload script used to gain network statistics
uploadScript(); uploadScript();
@ -31,35 +33,48 @@ void loop() {
delay(5000); 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() { void uploadScript() {
// Write our shell script in /tmp // Write our shell script in /tmp
// Using /tmp stores the script in RAM this way we can preserve // Using /tmp stores the script in RAM this way we can preserve
// the limited amount of FLASH erase/write cycles // 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("#!/bin/sh\n");
script.print("ifconfig wlan0 | grep \"RX bytes\" | tr ':' ' ' | awk \"{ print \\$3 \\\" \\\" \\$8 }\"\n"); // shell commands:
script.close(); // 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 // Make the script executable
Process chmod; Process chmod;
chmod.begin("chmod"); chmod.begin("chmod"); // chmod: change mode
chmod.addParameter("+x"); chmod.addParameter("+x"); // x stays for executable
chmod.addParameter("/tmp/wlan-stats.sh"); chmod.addParameter("/tmp/wlan-stats.sh"); // path to the file to make it executable
chmod.run(); chmod.run();
} }
// this function run the script and read the output data
void runScript() { void runScript() {
// Launch script and show results on the console // Run the script and show results on the Serial
Process myscript; Process myscript;
myscript.begin("/tmp/wlan-stats.sh"); myscript.begin("/tmp/wlan-stats.sh");
myscript.run(); myscript.run();
Console.print("WiFi RX/TX bytes: "); String output = "";
// read the output of the script
while (myscript.available()) { while (myscript.available()) {
char c = myscript.read(); output += (char)myscript.read();
Console.print(c);
} }
Console.println(); // remove the blank spaces at the beginning and the ending of the string
Console.flush(); output.trim();
Serial.println(output);
Serial.flush();
} }

View File

@ -8,6 +8,7 @@
Bridge KEYWORD3 Bridge KEYWORD3
FileIO KEYWORD3 FileIO KEYWORD3
FileSystem KEYWORD3
Console KEYWORD3 Console KEYWORD3
Process KEYWORD3 Process KEYWORD3
MailBox KEYWORD3 MailBox KEYWORD3