[136] | 1 | /*
|
---|
| 2 | SD card datalogger
|
---|
| 3 |
|
---|
| 4 | This example shows how to log data from three analog sensors
|
---|
| 5 | to an SD card mounted on the Arduino Y炭n using the Bridge library.
|
---|
| 6 |
|
---|
| 7 | The circuit:
|
---|
| 8 | * analog sensors on analog pins 0, 1 and 2
|
---|
| 9 | * SD card attached to SD card slot of the Arduino Y炭n
|
---|
| 10 |
|
---|
| 11 | Prepare your SD card creating an empty folder in the SD root
|
---|
| 12 | named "arduino". This will ensure that the Y炭n will create a link
|
---|
| 13 | to the SD to the "/mnt/sd" path.
|
---|
| 14 |
|
---|
| 15 | You can remove the SD card while the Linux and the
|
---|
| 16 | sketch are running but be careful not to remove it while
|
---|
| 17 | the system is writing to it.
|
---|
| 18 |
|
---|
| 19 | created 24 Nov 2010
|
---|
| 20 | modified 9 Apr 2012
|
---|
| 21 | by Tom Igoe
|
---|
| 22 | adapted to the Y炭n Bridge library 20 Jun 2013
|
---|
| 23 | by Federico Vanzati
|
---|
| 24 | modified 21 Jun 2013
|
---|
| 25 | by Tom Igoe
|
---|
| 26 |
|
---|
| 27 | This example code is in the public domain.
|
---|
| 28 |
|
---|
| 29 | http://arduino.cc/en/Tutorial/YunDatalogger
|
---|
| 30 |
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <FileIO.h>
|
---|
| 34 |
|
---|
| 35 | void setup() {
|
---|
| 36 | // Initialize the Bridge and the Serial
|
---|
| 37 | Bridge.begin();
|
---|
| 38 | Serial.begin(9600);
|
---|
| 39 | FileSystem.begin();
|
---|
| 40 |
|
---|
| 41 | while (!Serial); // wait for Serial port to connect.
|
---|
| 42 | Serial.println("Filesystem datalogger\n");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | void loop () {
|
---|
| 47 | // make a string that start with a timestamp for assembling the data to log:
|
---|
| 48 | String dataString;
|
---|
| 49 | dataString += getTimeStamp();
|
---|
| 50 | dataString += " = ";
|
---|
| 51 |
|
---|
| 52 | // read three sensors and append to the string:
|
---|
| 53 | for (int analogPin = 0; analogPin < 3; analogPin++) {
|
---|
| 54 | int sensor = analogRead(analogPin);
|
---|
| 55 | dataString += String(sensor);
|
---|
| 56 | if (analogPin < 2) {
|
---|
| 57 | dataString += ","; // separate the values with a comma
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | // open the file. note that only one file can be open at a time,
|
---|
| 62 | // so you have to close this one before opening another.
|
---|
| 63 | // The FileSystem card is mounted at the following "/mnt/FileSystema1"
|
---|
| 64 | File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
|
---|
| 65 |
|
---|
| 66 | // if the file is available, write to it:
|
---|
| 67 | if (dataFile) {
|
---|
| 68 | dataFile.println(dataString);
|
---|
| 69 | dataFile.close();
|
---|
| 70 | // print to the serial port too:
|
---|
| 71 | Serial.println(dataString);
|
---|
| 72 | }
|
---|
| 73 | // if the file isn't open, pop up an error:
|
---|
| 74 | else {
|
---|
| 75 | Serial.println("error opening datalog.txt");
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | delay(15000);
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | // This function return a string with the time stamp
|
---|
| 83 | String getTimeStamp() {
|
---|
| 84 | String result;
|
---|
| 85 | Process time;
|
---|
| 86 | // date is a command line utility to get the date and the time
|
---|
| 87 | // in different formats depending on the additional parameter
|
---|
| 88 | time.begin("date");
|
---|
| 89 | time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
|
---|
| 90 | // T for the time hh:mm:ss
|
---|
| 91 | time.run(); // run the command
|
---|
| 92 |
|
---|
| 93 | // read the output of the command
|
---|
| 94 | while (time.available() > 0) {
|
---|
| 95 | char c = time.read();
|
---|
| 96 | if (c != '\n')
|
---|
| 97 | result += c;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|