#include <SD.h>
#include <DHT.h>
#include <RTClib.h>
#include <Wire.h>
#define DHTPIN 21
#define DHTTYPE DHT22
#define SD_CS 5 // Pin CS yang terhubung ke SD card pada GPIO5 pada ESP32
#define SD_MOSI 23 // Pin DI yang terhubung ke SD card pada GPIO23 pada ESP32
#define SD_MISO 19 // Pin DO yang terhubung ke SD card pada GPIO19 pada ESP32
#define SD_SCK 18 // Pin SCK yang terhubung ke SD card pada GPIO18 pada ESP32
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"};
long seconds=00;
long minutes=00;
long hours=00;
DHT dht(DHTPIN, DHTTYPE);
File sd_file;
void setup() {
DateTime now = rtc.now();
Serial.begin(9600);
pinMode(SD_CS, OUTPUT);
dht.begin();
Wire.begin();
// SD Card Initialization
if (!SD.begin(SD_CS)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("SD card is initialized. Ready to go");
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
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
File sd_file = SD.open("data.txt", FILE_WRITE);
if (sd_file) {
senddata(now, hum, temp); // Passing the required arguments
} else {
Serial.println("error opening file");
}
delay(3000);
}
void senddata(DateTime now, float hum, float temp){
File sd_file = SD.open("data.txt", FILE_WRITE);
if (!sd_file) {
Serial.println("error opening file");
return;
}
for(long seconds = 0; seconds < 60; seconds += 2) {
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
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(now.minute());
sd_file.print(":");
sd_file.print(seconds < 10 ? "0" + String(seconds) : String(seconds));
sd_file.print(" ");
sd_file.print(temp);
sd_file.print(" ");
sd_file.println(hum); // 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(now.minute());
Serial.print(":");
Serial.print(seconds < 10 ? "0" + String(seconds) : String(seconds));
Serial.print(" ");
Serial.print(temp);
Serial.print(" ");
Serial.println(hum); // Newline to end the line
if (seconds >= 58) {
minutes++;
}
if (minutes > 59) {
hours++;
minutes = 0;
}
sd_file.flush(); //saving the file
delay(2000);
}
sd_file.close(); //closing the file
}