1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-11 22:24:13 +01:00
Arduino/libraries/Bridge/examples/FileWriteScript/FileWriteScript.ino

85 lines
2.3 KiB
Arduino
Raw Normal View History

2013-06-07 18:35:30 +02:00
/*
Write to file using FileIO classes.
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.
2013-07-01 16:20:05 +02:00
created 7 June 2010
by Cristian Maglie
2013-06-07 18:35:30 +02:00
2013-07-01 17:59:15 +02:00
This example code is in the public domain.
2013-09-09 20:56:15 +02:00
http://arduino.cc/en/Tutorial/FileWriteScript
2013-07-01 17:59:15 +02:00
2013-06-07 18:35:30 +02:00
*/
#include <FileIO.h>
void setup() {
// Setup Bridge (needed every time we communicate with the Arduino Yún)
Bridge.begin();
2013-07-01 16:20:05 +02:00
// Initialize the Serial
Serial.begin(9600);
while(!Serial); // wait for Serial port to connect.
Serial.println("File Write Script example\n\n");
2013-06-07 18:35:30 +02:00
// Setup File IO
2013-07-01 16:20:05 +02:00
FileSystem.begin();
2013-06-07 18:35:30 +02:00
// Upload script used to gain network statistics
uploadScript();
}
void loop() {
// Run stats script every 5 secs.
runScript();
delay(5000);
}
2013-07-01 16:20:05 +02:00
// this function creates a file into the linux processor that contains a shell script
// to check the network traffic of the WiFi interface
2013-06-07 18:35:30 +02:00
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
2013-07-01 16:20:05 +02:00
File script = FileSystem.open("/tmp/wlan-stats.sh", FILE_WRITE);
// Shell script header
2013-06-07 18:35:30 +02:00
script.print("#!/bin/sh\n");
2013-07-01 16:20:05 +02:00
// 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
2013-06-07 18:35:30 +02:00
// Make the script executable
Process chmod;
2013-07-01 16:20:05 +02:00
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
2013-06-07 18:35:30 +02:00
chmod.run();
}
2013-07-01 16:20:05 +02:00
// this function run the script and read the output data
2013-06-07 18:35:30 +02:00
void runScript() {
2013-07-01 16:20:05 +02:00
// Run the script and show results on the Serial
2013-06-07 18:35:30 +02:00
Process myscript;
myscript.begin("/tmp/wlan-stats.sh");
myscript.run();
2013-07-01 16:20:05 +02:00
String output = "";
// read the output of the script
2013-06-07 18:35:30 +02:00
while (myscript.available()) {
2013-07-01 16:20:05 +02:00
output += (char)myscript.read();
2013-06-07 18:35:30 +02:00
}
2013-07-01 16:20:05 +02:00
// remove the blank spaces at the beginning and the ending of the string
output.trim();
Serial.println(output);
Serial.flush();
2013-06-07 18:35:30 +02:00
}