This example writes to a file using the FileIO classes into the Yún's filesystem. A shell script file is created in /tmp, and is executed afterwards.
There is no circuit for this example.
Include the FileIO header, for communicating with the filesystem.
setup(), initialize Bridge, Serial communication, and FileSystem. Wait for an active serial connection, and call a custom function
uploadScript() which will upload your file, before starting
loop() will just execute your script every 5 seconds by calling another custom function,
uploadScript() function will create a shell script in the Linux file system that will check the network traffic of the WiFi interface. Create the file and open it by creating an instance of the
File@ class, and calling FileSystem.open()@@ indicating where you would like to create the script. You'll store the script in "/tmp", which resides in RAM, to preserve the limited number of FLASH memory read/write cycles.
Write the contents of the script to the file with
File.print(). begin by printing the header, "#!/bin/s", then the utility
ifconfig. @ifconfig@ is a command line utility for controlling network interfaces. you'll be looking at the WiFi interface, which is referred to as "wlan0". The utility
grep will search the output of
ifconfig. You're looking for the number of bytes received , so search for the keywords "RX bytes" and close the file.
Instantiate a Process to make the script executable.
chmod@ is a command that will change the modes of files. By sending the chmod@@ command and filepath, you can make your shell script run like an application.
runScript() function will create a Process that runs the script and prints the results to the Serial Monitor. Create a named Process, and start your script by calling
Create a string to hold the output, and read the output into it
Remove the blank spaces at the beginning and end of the string and print it to the serial monitor :
The complete sketch is below :
Last revision 2015/08/11 by SM