Skip to content

Commit fc880cc

Browse files
committed
modified the FileWriteScript example
1 parent e6af3ac commit fc880cc

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

hardware/arduino/avr/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
44
This sketch demonstrate how to write file into the Yún filesystem.
55
A shell script file is created in /tmp, and it is executed afterwards.
6+
7+
created 7 June 2010
8+
by Cristian Maglie
69
710
*/
811

@@ -11,15 +14,14 @@
1114
void setup() {
1215
// Setup Bridge (needed every time we communicate with the Arduino Yún)
1316
Bridge.begin();
14-
15-
// Setup Console
16-
Console.begin();
17-
// Buffering improves Console performance, but we must remember to
18-
// finish sending using the Console.flush() command.
19-
Console.buffer(64);
17+
// Initialize the Serial
18+
Serial.begin(9600);
19+
20+
while(!Serial); // wait for Serial port to connect.
21+
Serial.println("File Write Script example\n\n");
2022

2123
// Setup File IO
22-
SD.begin();
24+
FileSystem.begin();
2325

2426
// Upload script used to gain network statistics
2527
uploadScript();
@@ -31,35 +33,48 @@ void loop() {
3133
delay(5000);
3234
}
3335

36+
// this function creates a file into the linux processor that contains a shell script
37+
// to check the network traffic of the WiFi interface
3438
void uploadScript() {
3539
// Write our shell script in /tmp
3640
// Using /tmp stores the script in RAM this way we can preserve
3741
// the limited amount of FLASH erase/write cycles
38-
File script = SD.open("/tmp/wlan-stats.sh", FILE_WRITE);
42+
File script = FileSystem.open("/tmp/wlan-stats.sh", FILE_WRITE);
43+
// Shell script header
3944
script.print("#!/bin/sh\n");
40-
script.print("ifconfig wlan0 | grep \"RX bytes\" | tr ':' ' ' | awk \"{ print \\$3 \\\" \\\" \\$8 }\"\n");
41-
script.close();
45+
// shell commands:
46+
// ifconfig: is a command line utility for controlling the network interfaces.
47+
// wlan0 is the interface we want to query
48+
// grep: search inside the output of the ifconfig command the "RX bytes" keyword
49+
// and extract the line that contains it
50+
script.print("ifconfig wlan0 | grep 'RX bytes'\n");
51+
script.close(); // close the file
4252

4353
// Make the script executable
4454
Process chmod;
45-
chmod.begin("chmod");
46-
chmod.addParameter("+x");
47-
chmod.addParameter("/tmp/wlan-stats.sh");
55+
chmod.begin("chmod"); // chmod: change mode
56+
chmod.addParameter("+x"); // x stays for executable
57+
chmod.addParameter("/tmp/wlan-stats.sh"); // path to the file to make it executable
4858
chmod.run();
4959
}
5060

61+
62+
// this function run the script and read the output data
5163
void runScript() {
52-
// Launch script and show results on the console
64+
// Run the script and show results on the Serial
5365
Process myscript;
5466
myscript.begin("/tmp/wlan-stats.sh");
5567
myscript.run();
5668

57-
Console.print("WiFi RX/TX bytes: ");
69+
String output = "";
70+
71+
// read the output of the script
5872
while (myscript.available()) {
59-
char c = myscript.read();
60-
Console.print(c);
73+
output += (char)myscript.read();
6174
}
62-
Console.println();
63-
Console.flush();
75+
// remove the blank spaces at the beginning and the ending of the string
76+
output.trim();
77+
Serial.println(output);
78+
Serial.flush();
6479
}
6580

hardware/arduino/avr/libraries/Bridge/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
Bridge KEYWORD3
1010
FileIO KEYWORD3
11+
FileSystem KEYWORD3
1112
Console KEYWORD3
1213
Process KEYWORD3
1314
MailBox KEYWORD3

0 commit comments

Comments
 (0)