From b464f3c52d35278d28232088c92b1bfa141f5de4 Mon Sep 17 00:00:00 2001
From: Fede85 <f.vanzati@gmail.com>
Date: Mon, 1 Jul 2013 16:20:05 +0200
Subject: modified the FileWriteScript example

---
 .../examples/FileWriteScript/FileWriteScript.ino   | 53 ++++++++++++++--------
 libraries/Bridge/keywords.txt                      |  1 +
 2 files changed, 35 insertions(+), 19 deletions(-)

(limited to 'libraries')

diff --git a/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino b/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino
index d5bbb26..f279de9 100644
--- a/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino
+++ b/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino
@@ -3,6 +3,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 
  
  */
  
@@ -11,15 +14,14 @@
 void setup() {
   // Setup Bridge (needed every time we communicate with the Arduino Yún)
   Bridge.begin();
-
-  // Setup Console
-  Console.begin();
-  // Buffering improves Console performance, but we must remember to
-  // finish sending using the Console.flush() command.
-  Console.buffer(64);
+  // Initialize the Serial
+  Serial.begin(9600);
+  
+  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();
 }
 
diff --git a/libraries/Bridge/keywords.txt b/libraries/Bridge/keywords.txt
index faf0773..81b9391 100644
--- a/libraries/Bridge/keywords.txt
+++ b/libraries/Bridge/keywords.txt
@@ -8,6 +8,7 @@
 
 Bridge	KEYWORD3
 FileIO	KEYWORD3
+FileSystem	KEYWORD3
 Console	KEYWORD3
 Process	KEYWORD3
 MailBox	KEYWORD3
-- 
cgit v1.2.3-18-g5258