3
3
4
4
This sketch demonstrate how to write file into the Yún filesystem.
5
5
A shell script file is created in /tmp, and it is executed afterwards.
6
+
7
+ created 7 June 2010
8
+ by Cristian Maglie
6
9
7
10
*/
8
11
11
14
void setup () {
12
15
// Setup Bridge (needed every time we communicate with the Arduino Yún)
13
16
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 " );
20
22
21
23
// Setup File IO
22
- SD .begin ();
24
+ FileSystem .begin ();
23
25
24
26
// Upload script used to gain network statistics
25
27
uploadScript ();
@@ -31,35 +33,48 @@ void loop() {
31
33
delay (5000 );
32
34
}
33
35
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
34
38
void uploadScript () {
35
39
// Write our shell script in /tmp
36
40
// Using /tmp stores the script in RAM this way we can preserve
37
41
// 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
39
44
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
42
52
43
53
// Make the script executable
44
54
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
48
58
chmod.run ();
49
59
}
50
60
61
+
62
+ // this function run the script and read the output data
51
63
void runScript () {
52
- // Launch script and show results on the console
64
+ // Run the script and show results on the Serial
53
65
Process myscript;
54
66
myscript.begin (" /tmp/wlan-stats.sh" );
55
67
myscript.run ();
56
68
57
- Console.print (" WiFi RX/TX bytes: " );
69
+ String output = " " ;
70
+
71
+ // read the output of the script
58
72
while (myscript.available ()) {
59
- char c = myscript.read ();
60
- Console.print (c);
73
+ output += (char )myscript.read ();
61
74
}
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 ();
64
79
}
65
80
0 commit comments