Since Uno and Mega has no Serial, replace Serial with Console at Bridge Sample IDE code

  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.

 created 7 June 2010
 by Cristian Maglie

 This example code is in the public domain.

  To see the Console, pick your Yún's name and IP address in the Port menu
 then open the Port Monitor. You can also see it by opening a terminal window
 and typing
 ssh root@ yourYunsName.local 'telnet localhost 6571'
 then pressing enter. When prompted for the password, enter it.

#include <Console.h>
#include <FileIO.h>

void setup() {
  // Setup Bridge (needed every time we communicate with the Arduino Yún)
  // Initialize the Serial
  //while (!Serial); // wait for Serial port to connect.
  //Serial.println("File Write Script example\n\n");
  while (!Console) {
    ; // wait for Console port to connect.
  Console.println("File Write Script example\n\n");
  // Setup File IO
  // Upload script used to gain network statistics

void loop() {
  // Run stats script every 5 secs.

// 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 ="/tmp/", FILE_WRITE);
  // Shell script header
  // 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: change mode
  chmod.addParameter("+x");  // x stays for executable
  chmod.addParameter("/tmp/");  // path to the file to make it executable;

// this function run the script and read the output data
void runScript() {
  // Run the script and show results on the Serial
  Process myscript;
  String output = "";
  // read the output of the script
  while (myscript.available()) {
    output += (char);
  // remove the blank spaces at the beginning and the ending of the string


Comments powered by CComment