Code:
/*
KinCony Company - https://www.kincony.com
-----------------------------------------------------
This program logs temperature data from a DS18B20 sensor
to an SD card, including the current date and time.
The date and time can be set via serial commands.
Serial Command to Set Date and Time:
------------------------------------
DYYYY-MM-DDTHH:MM:SS (Example: D2024-09-01T20:00:00)
- Sets the date to September 1, 2024, and the time to 20:00:00.
Serial Command to Get Current Date and Time:
--------------------------------------------
current time
- Returns the current date and time in the format:
Current Date: YYYY-MM-DD
Current Time: HH:MM:SS
*/
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#include <SPI.h>
#include <TimeLib.h>
// Pin assignments
#define ONE_WIRE_BUS 26
#define SD_CS_PIN 5
// DS18B20 sensor setup
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// SD card setup
File dataFile;
void setup() {
// Start serial communication
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
// Start the SD card
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card initialization failed!");
return;
}
// Open the file for writing
dataFile = SD.open("/temperature_log.csv", FILE_WRITE);
if (dataFile) {
// Write the header
dataFile.println("Date,Time,Temperature (°C)");
dataFile.close();
} else {
Serial.println("Error opening file for writing");
}
// Initialize the time to a default value
setTime(0, 0, 0, 1, 1, 2020); // Default to Jan 1, 2020 00:00:00
}
void loop() {
// Check for serial input
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
// Set the time if the input starts with 'D'
if (input.startsWith("D")) {
int year = input.substring(1, 5).toInt();
int month = input.substring(6, 8).toInt();
int day = input.substring(9, 11).toInt();
int hour = input.substring(12, 14).toInt();
int minute = input.substring(15, 17).toInt();
int second = input.substring(18, 20).toInt();
setTime(hour, minute, second, day, month, year);
Serial.println("Time updated successfully!");
}
// Return the current time if the input is "current time"
else if (input == "current time") {
String currentDate = String(year()) + "-" + String(month()) + "-" + String(day());
String currentTime = String(hour()) + ":" + String(minute()) + ":" + String(second());
Serial.print("Current Date: ");
Serial.println(currentDate);
Serial.print("Current Time: ");
Serial.println(currentTime);
}
}
// Get the current date and time
String formattedDate = String(year()) + "-" + String(month()) + "-" + String(day());
String formattedTime = String(hour()) + ":" + String(minute()) + ":" + String(second());
// Request temperature measurement
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
// Open the file for appending
dataFile = SD.open("/temperature_log.csv", FILE_APPEND);
if (dataFile) {
// Write the data as a new line
dataFile.print(formattedDate);
dataFile.print(",");
dataFile.print(formattedTime);
dataFile.print(",");
dataFile.println(temperatureC, 2); // 2 decimal places for temperature
dataFile.close();
// Print to the Serial Monitor as well
Serial.print(formattedDate);
Serial.print(",");
Serial.print(formattedTime);
Serial.print(",");
Serial.println(temperatureC, 2);
} else {
Serial.println("Error opening file for appending");
}
// Wait for 5 seconds before the next measurement
delay(5000);
}