//https://forum.arduino.cc/t/ds18b20-sdcard-data-logger-with-rtc/1274999
//========Set the time for the RealTimeClock (RTC) with Set_RTC_Time.ino first==========
//For the RTC and the SD-Card-Connector
#include <SD.h>
#include <SPI.h>
#include "DS3231.h"
//For the DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
File myFile;
DS3231 rtc(SDA, SCL);
int pinCS = 53; // Pin 10 on Arduino Uno
//=====DS18B20======
// Data wire from the DS18B20 is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature
DallasTemperature sensors(&oneWire);
//-------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(pinCS, OUTPUT);
if (SD.begin()) { // SD Card Initialization
Serial.println("SD card is ready to use.");
}
else {
Serial.println("SD card initialization failed");
return;
}
rtc.begin();
//=======DS18B20=======
pinMode(ONE_WIRE_BUS, OUTPUT);
sensors.begin(); //Start up the library
}
//---------------------------------------------------------------
void loop() {
Serial.print(rtc.getDateStr());
Serial.print(",");
Serial.print(rtc.getTimeStr());
Serial.print(",");
//======DS18B20=========
sensors.requestTemperatures(); //Call sensor to request the Temperature
Serial.println(String(sensors.getTempCByIndex(0)) + "°C"); //Output the values on the monitor
myFile = SD.open("test_ds.txt", FILE_WRITE);
if (myFile) {
myFile.print(rtc.getDateStr());
myFile.print(",");
myFile.print(rtc.getTimeStr());
myFile.print(",");
sensors.requestTemperatures(); //Call sensor to request the Temperature
myFile.println(String(sensors.getTempCByIndex(0)) + "°C"); //Save the values on the SD-Card
myFile.close(); // close the file
}
// if the file didn't open, print an error:
else {
Serial.println("error opening txt-file");
}
delay(3000); //Wait for 3 sec
readSD();
}
//----------------------------------------------------------
void readSD() {
myFile = SD.open("test_ds.txt", O_READ); //If no exist, create file
if (myFile) {
Serial.println("test_ds.txt listing");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test_ds.txt");
}
}