#include <SD.h> //Library untuk mengakses dan mengatur kartu SD.
#include <DHT.h> //Library untuk sensor suhu dan kelembaban DHT.
#include <RTClib.h> //
#include <LiquidCrystal_I2C.h> //
#define DHTPIN 8
#define DHTTYPE DHT22
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu"};
long seconds=00;
long minute=00;
long hours=00;
LiquidCrystal_I2C lcd(0x27, 20, 4); // Inisialisasi LCD I2C 20x4 dengan alamat 0x27
int CS_pin = 10;
DHT dht(DHTPIN, DHTTYPE);
File sd_file;
float lastTemp = 0;
float lastHum = 0;
void setup() {
DateTime now = rtc.now();
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
// SD Card Initialization
if (SD.begin()) {
Serial.println("SD card is initialized. Ready to go");
}
else {
Serial.println("Failed");
return;
}
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
File sd_file = SD.open("data.txt", FILE_WRITE);
if (sd_file) {
Serial.print("Hari");
Serial.print(" ");
Serial.print("Tgl");
Serial.print("/");
Serial.print("Bln");
Serial.print("/");
Serial.print("Thn");
Serial.print(" ");
Serial.print("Waktu");
Serial.print(" ");
Serial.print("Suhu(C)");
Serial.print(" ");
Serial.print("Kelembaban(%)");
Serial.println(); // Newline to end the line
sd_file.print("Hari");
sd_file.print(" ");
sd_file.print("Tgl");
sd_file.print("/");
sd_file.print("Bln");
sd_file.print("/");
sd_file.print("Thn");
sd_file.print(" ");
sd_file.print("Waktu");
sd_file.print(" ");
sd_file.print("Suhu(C)");
sd_file.print(" ");
sd_file.print("Kelembaban(%)"); // Newline to end the line
}
sd_file.close(); //closing the file
}
void loop() {
DateTime now = rtc.now();
float hum = dht.readHumidity(); // Reading the humidity
float temp = dht.readTemperature(); // Reading the temperature
// Update minute
minute = now.minute();
// Update seconds
seconds = now.second();
// Check if the temperature or humidity has changed significantly
if (abs(temp - lastTemp) >= 0.1 || abs(hum - lastHum) >= 0.1) {
updateLCD(now, temp, hum);
lastTemp = temp;
lastHum = hum;
}
// Save data to SD card
saveData(now, hum, temp);
delay(3000);
}
void updateLCD(DateTime now, float temp, float hum) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(", ");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
lcd.print("T&H: ");
lcd.print(temp, 1);
lcd.print("C & ");
lcd.print(hum, 1);
lcd.print("%");
}
void saveData(DateTime now, float hum, float temp) {
File sd_file = SD.open("data.txt", FILE_WRITE);
if (sd_file) {
sd_file.print(daysOfTheWeek[now.dayOfTheWeek()]);
sd_file.print(", ");
sd_file.print(now.day(), DEC);
sd_file.print("/");
sd_file.print(now.month(), DEC);
sd_file.print("/");
sd_file.print(now.year(), DEC);
sd_file.print(" ");
sd_file.print(now.hour());
sd_file.print(":");
sd_file.print(minute < 10 ? "0" + String(minute) : String(minute));
sd_file.print(":");
sd_file.print(seconds < 10 ? "0" + String(seconds) : String(seconds));
sd_file.print(" ");
char tempStr[6];
char humStr[6];
dtostrf(temp, 4, 1, tempStr); // Mengonversi nilai float temp ke string dengan 1 angka di belakang koma
dtostrf(hum, 4, 1, humStr); // Mengonversi nilai float hum ke string dengan 1 angka di belakang koma
sd_file.print(tempStr);
sd_file.print(" ");
sd_file.println(humStr); // Newline to end the line
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(", ");
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(minute < 10 ? "0" + String(minute) : String(minute));
Serial.print(":");
Serial.print(seconds < 10 ? "0" + String(seconds) : String(seconds));
Serial.print(" ");
Serial.print(tempStr);
Serial.print(" ");
Serial.println(humStr); // Newline to end the line
sd_file.flush(); //saving the file
sd_file.close(); //closing the file
} else {
Serial.println("error opening file");
}
}