There are several ways. The following uses a DS1307 RTC. The DS3231 is the same. I have never used a library for this. Strip out the LCD stuff etc.
/*
This programme reads temperatures from three DS18B20 sensors at one
second intervals.
The temperatures are displayed on Nokia 5110 LCD
Two of the temperatures, plus the difference between them, is transmitted
over bluetooth in a format suitable for BlueTooth Terminal/Graphics (CETIN)
in order to display three graphs in real time.
Every tenth reading is recorded on SD card in real numbers to two decimal
places. Each line is timestamped.
A new file is created at midnight using the date as the filename.
Daily files can be read via bluetooth by sending MMDD
Credit to Hacktronics, Mellis & Igoe, Garage Box, Zoomkat, Bildr.org, etc.
Kudos to lar3ry in Saskatchewan, Stanley in KL, Coding Badly in Tx.
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h> // Nokia 5110
#include "Wire.h"
#include <SD.h>
#include <SPI.h> // SD 5110
#define DS1307_ADDRESS 0x68
char filename[] = "00000000.CSV";
File myFile;
char dumpName[] = "00000000.CSV";
File dumpFile;
static PCD8544 lcd;
// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};
// Green group Dee Why (red or amber LED shields)
byte InThermo[8] = {0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0x9F };
byte OutThermo[8] = {0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {0x28, 0x54, 0xF7, 0x2D, 0x04, 0x00, 0x00, 0x68};
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
byte second, minute, hour, weekDay, day, month, year;
int k=0;
const int chipSelect = 4;
float tempC, InTemp, OutTemp, DrainTemp, diff;
// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char strIn[8];
char strOut[8];
char strDrain[8];
char strdiff[8];
char charBuf [13];
String readString;
String stringOne, stringTwo, stringThree, stringFour;
String stringFive, stringSix;
//_________________________________________
void setup() {
lcd.begin(84, 48);
// Register the custom symbols...
lcd.createChar(DEGREES_CHAR, degrees_glyph);
lcd.createChar(SLASH_CHAR, slash_glyph);
Wire.begin();
Serial.begin(9600);
delay(300);//Wait for newly restarted system to stabilize
lcd.setCursor (0,0);
lcd.println("Init SD CARD");
// make sure that the default chip select pin 53 is set to
// output, even if you don't use it:
pinMode(53, OUTPUT);//MEGA
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
lcd.println("Card failed");
// don't do anything more:
return;
}
lcd.println("CARD OK");
delay(2000);
GetClock();
getFileName();
lcd.setCursor(0,3);
lcd.println(filename);
delay(2000);
lcd.clear();
//Sequence for bluetooth
// "E256,-321,982\n"
//so insert three variable between four strings, two are the same twice,
//to make a fourth string
stringOne = "E";
stringTwo = ",";
stringThree = "\n";
running();
}
//________________________________________________________________
void loop() {
GetClock();
if (hour == 0 && minute == 0 && second <2)
{
getFileName();
}
while (Serial.available())
{
delay(3);
char c = Serial.read();
readString += c;
}// end while
if (readString.length() >0)
{
getDump();
readString="";
} // end if
//get the values from the DS8B20's
sensors.requestTemperatures();
InTemp = sensorValue(InThermo);
OutTemp = sensorValue(OutThermo);
DrainTemp = sensorValue(DrainThermo);
diff = OutTemp - InTemp;
dtostrf(InTemp,4, 2, strIn);
dtostrf(OutTemp,4, 2, strOut);
dtostrf(diff,4, 2, strdiff);
stringFour = stringOne + strIn + stringTwo + strOut;
stringFour = stringFour + stringTwo + strdiff + stringThree;
Serial.println(stringFour);
lcd.setCursor(49,0);
lcd.print(InTemp);
lcd.setCursor(49,1);
lcd.print (OutTemp);
lcd.setCursor(49,2);
lcd.print(DrainTemp);
lcd.setCursor(49,3);
lcd.print(diff);
lcd.print(" ");//suppress trailing fig > 9.99
lcd.setCursor(17,5);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.print(" ");
k=k+1;
if (k>9 )
{
myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
myFile.print(hour);
myFile.print(":");
myFile.print(minute);
myFile.print(":");
myFile.print(second);
myFile.print(",");
myFile.print(InTemp);
myFile.print(",");
myFile.print(OutTemp);
myFile.print(",");
myFile.print(DrainTemp);
myFile.print(",");
myFile.print(diff);
myFile.println();
myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
k=0;
}
delay(850);
} // loop ends here
//sensorValue function
float sensorValue (byte deviceAddress[])
{
tempC = sensors.getTempC (deviceAddress);
return tempC;
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal bers
return ( (val/16*10) + (val%16) );
}
void running(){
lcd.setCursor(0,0);
lcd.print("In");
lcd.setCursor(31,0);
lcd.print("\001C ");
lcd.setCursor(0,1);
lcd.print("Out");
lcd.setCursor(31,1);
lcd.print("\001C ");
lcd.setCursor(0,2);
lcd.print("Drain");
lcd.setCursor(31,2);
lcd.print("\001C ");
lcd.setCursor(0,3);
lcd.print("diff");
lcd.setCursor(31,3);
lcd.print("\001C ");
lcd.setCursor(15,4);
lcd.print("Bluetooth!");
}
void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}
void GetClock(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
day = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void getDump() {
stringSix = "2015" + readString + ".csv";
stringSix.toCharArray(charBuf, 15);
Serial.println("");
Serial.println(charBuf);
File dumpFile = SD.open(charBuf);
if (dumpFile)
{
lcd.clear();
lcd.setCursor (0,1);
lcd.println("DUMP FROM ");
lcd.setCursor (0,2);
lcd.println(charBuf);
while (dumpFile.available())
{
Serial.write(dumpFile.read());
}
dumpFile.close();
lcd.clear();
running();
}
else {
Serial.println("error opening file");
}
}