2013-06-20 13:24:27 +02:00
|
|
|
/*
|
|
|
|
SD card datalogger
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example shows how to log data from three analog sensors
|
2013-07-01 17:58:03 +02:00
|
|
|
to an SD card mounted on the Arduino Yún using the Bridge library.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-06-20 13:24:27 +02:00
|
|
|
The circuit:
|
2013-07-01 17:58:03 +02:00
|
|
|
* analog sensors on analog pins 0, 1 and 2
|
|
|
|
* SD card attached to SD card slot of the Arduino Yún
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
Prepare your SD card creating an empty folder in the SD root
|
|
|
|
named "arduino". This will ensure that the Yún will create a link
|
2013-07-01 17:58:03 +02:00
|
|
|
to the SD to the "/mnt/sd" path.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
You can remove the SD card while the Linux and the
|
2013-06-22 08:03:22 +02:00
|
|
|
sketch are running but be careful not to remove it while
|
|
|
|
the system is writing to it.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-06-20 13:24:27 +02:00
|
|
|
created 24 Nov 2010
|
|
|
|
modified 9 Apr 2012
|
|
|
|
by Tom Igoe
|
2013-07-01 17:58:03 +02:00
|
|
|
adapted to the Yún Bridge library 20 Jun 2013
|
2013-06-20 13:24:27 +02:00
|
|
|
by Federico Vanzati
|
2013-06-22 08:03:22 +02:00
|
|
|
modified 21 Jun 2013
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-06-20 13:24:27 +02:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/YunDatalogger
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-06-20 13:24:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <FileIO.h>
|
|
|
|
|
|
|
|
void setup() {
|
2013-06-22 08:03:22 +02:00
|
|
|
// Initialize the Bridge and the Serial
|
2013-06-20 13:24:27 +02:00
|
|
|
Bridge.begin();
|
2013-06-22 08:03:22 +02:00
|
|
|
Serial.begin(9600);
|
2013-06-21 19:48:56 +02:00
|
|
|
FileSystem.begin();
|
2013-06-20 13:24:27 +02:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial); // wait for Serial port to connect.
|
2013-07-01 17:58:03 +02:00
|
|
|
Serial.println("Filesystem datalogger\n");
|
2013-06-20 13:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop () {
|
|
|
|
// make a string that start with a timestamp for assembling the data to log:
|
2013-07-01 17:58:03 +02:00
|
|
|
String dataString;
|
|
|
|
dataString += getTimeStamp();
|
2013-06-20 13:24:27 +02:00
|
|
|
dataString += " = ";
|
|
|
|
|
|
|
|
// read three sensors and append to the string:
|
|
|
|
for (int analogPin = 0; analogPin < 3; analogPin++) {
|
|
|
|
int sensor = analogRead(analogPin);
|
|
|
|
dataString += String(sensor);
|
|
|
|
if (analogPin < 2) {
|
2013-07-01 17:58:03 +02:00
|
|
|
dataString += ","; // separate the values with a comma
|
2013-06-20 13:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the file. note that only one file can be open at a time,
|
|
|
|
// so you have to close this one before opening another.
|
2013-06-21 19:48:56 +02:00
|
|
|
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
|
2013-07-01 17:58:03 +02:00
|
|
|
File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
|
2013-06-20 13:24:27 +02:00
|
|
|
|
|
|
|
// if the file is available, write to it:
|
|
|
|
if (dataFile) {
|
|
|
|
dataFile.println(dataString);
|
|
|
|
dataFile.close();
|
|
|
|
// print to the serial port too:
|
2013-06-22 08:03:22 +02:00
|
|
|
Serial.println(dataString);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2013-06-20 13:24:27 +02:00
|
|
|
// if the file isn't open, pop up an error:
|
|
|
|
else {
|
2013-06-22 08:03:22 +02:00
|
|
|
Serial.println("error opening datalog.txt");
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 13:24:27 +02:00
|
|
|
delay(15000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-01 17:58:03 +02:00
|
|
|
// This function return a string with the time stamp
|
|
|
|
String getTimeStamp() {
|
2013-06-22 08:03:22 +02:00
|
|
|
String result;
|
2013-06-20 13:24:27 +02:00
|
|
|
Process time;
|
2013-10-21 09:58:40 +02:00
|
|
|
// date is a command line utility to get the date and the time
|
|
|
|
// in different formats depending on the additional parameter
|
2013-07-01 17:58:03 +02:00
|
|
|
time.begin("date");
|
|
|
|
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
|
2013-10-21 09:58:40 +02:00
|
|
|
// T for the time hh:mm:ss
|
2013-07-01 17:58:03 +02:00
|
|
|
time.run(); // run the command
|
2013-06-20 13:24:27 +02:00
|
|
|
|
2013-07-01 17:58:03 +02:00
|
|
|
// read the output of the command
|
2013-10-21 09:58:40 +02:00
|
|
|
while (time.available() > 0) {
|
2013-06-20 13:24:27 +02:00
|
|
|
char c = time.read();
|
2013-10-21 09:58:40 +02:00
|
|
|
if (c != '\n')
|
2013-06-22 08:03:22 +02:00
|
|
|
result += c;
|
2013-06-20 13:24:27 +02:00
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-07-01 17:58:03 +02:00
|
|
|
return result;
|
2013-06-20 13:24:27 +02:00
|
|
|
}
|