aboutsummaryrefslogtreecommitdiff
path: root/libraries/Bridge/examples/Datalogger/Datalogger.ino
blob: 5878e05498891abd536aca01973f17ee5f0ab887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card mounted on the Arduino Yun using the Bridge library.
 	
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SD card slot of the Arduino Yun
 
 You can remove the SD card while the Linux and the 
 sketch are running but be careful not to remove it while
 the system is writing to it.
 
 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe
 adapted to the Yun Bridge library 20 Jun 2013
 by Federico Vanzati
 modified  21 Jun 2013
 by Tom Igoe
 
 This example code is in the public domain.
 	 
 */

#include <FileIO.h>
#include <Serial.h>

void setup() {
  // Initialize the Bridge and the Serial
  Bridge.begin();
  Serial.begin(9600);
  FileSystem.begin();

  while(!Serial);  // wait for Serial port to connect.
  Serial.println("Filesystem datalogger");
}


void loop () {
  // make a string that start with a timestamp for assembling the data to log:
  String dataString = "";
  dataString += addTimeStamp();
  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) {
      dataString += ","; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  // The FileSystem card is mounted at the following "/mnt/FileSystema1"
  File dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
  
  delay(15000);

}

// This function append a time stamp to the string passed as argument
String addTimeStamp() {
  String result;
  Process time;
  time.begin("date"); 
  time.addParameter("+%D-%T");
  time.run();

  while(time.available()>0) {
    char c = time.read();
    if(c != '\n')
      result += c;
  }
}