Hardware Reference
In-Depth Information
Secondly, we will convert these measurements to strings as follows:
String temp = String((int) t);
String hum = String((int) h);
We are now going to build a string that contains the date and time of the measurement us-
ing the Time library. This library contains functions to get the current time, day, or
month, for example, which are calculated from the initial call to the NTP server. For ex-
ample, calling the day() function will return the current day. The following piece of
code returns a string with the date and time:
String log_time = String(day()) + "/" +
String(month()) + "/" + String(year()) + " " +
String(hour()) + ":" + String(minute()) + ":" +
String(second());
We then assemble this string with the temperature and humidity measurements using com-
mas as separators between the different strings, as shown in the following line of code:
String dataString = log_time + "," + temp + "," + hum;
We are now going to write this data on the SD card. We first need to open the card to write
data using the following line:
File dataFile = SD.open("datalog.txt", FILE_WRITE);
If that's successful, we will put the contents of the dataString variable to this file
named datalog.txt , as shown in the following code:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
Finally, we will repeat the operation every 10 seconds, but you can, of course, modify this
delay with the following line:
Search WWH ::




Custom Search