This example shows how to log data from three analog sensors to an SD card mounted on the Arduino Yún using the Bridge library. The SD card is not connected to the 32U4, but the AR9331, which is why Bridge must be used.
Prepare your SD card by creating an empty folder in the root directory named "arduino". When OpenWrt-Yun finds this folder on an attached storage device, it creates a link to the SD to the "/mnt/sd" path.
You can remove the SD card while Linux and the sketch are running but be careful not to remove it while data is writing to the card.
There is no circuit for this example.
Include the FileIO header, for communicating with the SD card.
setup(), initialize Bridge, Serial communication, and FileSystem (for communicating with the OpenWrt-Yun file system). Wait for an active serial connection before starting the remainder of the sketch.
loop(), create a string that starts with a timestamp to organize the data to be logged. You'll create the
getTimeStamp() function below.
Read the data from the sensors and append them to the string, separating the values with a comma :
Open the file you'll be writing the data to using a
File object and
FileSystem.open(). With the modifier
FILE_APPEND, you can write information to the end of the file. If the file doesn't already exist, it will be created. In this case, you'll be creating and writing to a file at the root directory of the SD card named "datalog.txt".
If the file opens successfully, write the string to it, close the file, and print the information to the Serial monitor.
If there is a problem opening the file, send an error to the Serial monitor :
Last, you'll write the function
getTimeStamp() to retrieve the time the information was read. It will be returning a string. First, create a string to hold the current time. You'll also create an instance of Process called "time". start the process and call the "date" application. "date" is a command line utility that returns the current date and the time. Using
time.addParameter(), you'll specify the parameters D and T, which will return the date (mm/dd/yy), and the current time (hh:mm:ss). Run the process and read the result into the string.
The complete sketch is below :
Last revision 2015/08/12 by SM