//SIMPLE DATA LOGGER PROGRAM
//AUTHOR NADHIF FAUZIL ADHIM
//=================
//SD CARD MODULE
//DHT 11 Sensor
//RTC Module

#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
#define Buzzer 5
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>

RTC_DS1307 rtc;
const int chipSelect = 10;
DHT dht(DHTPIN, DHTTYPE);
unsigned long currentMillis, time;
unsigned long buzzerinterval, readsensorinterval, hometext = 0;
//time Interval
const long readsensortime = 3000;
const long hometexttime = 3000;
const long buzzertime = 3000;

int BuzzerState = LOW;
LiquidCrystal_I2C lcd(0x27, 16, 2);
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

void BuzzerAlert() {
  if (currentMillis - buzzerinterval >= buzzertime) {
    buzzerinterval = currentMillis;
    if (BuzzerState == LOW) {
      BuzzerState = HIGH;
      noTone(Buzzer);
      tone(Buzzer, 500);
    } else {
      BuzzerState = LOW;
      noTone(Buzzer);
      tone(Buzzer, 1000);
    }
  }
}

void checkallstatus(float h, float t, float f) {
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Gagal membaca sensor!!!"));
    lcd.setCursor(0, 1);
    lcd.print("    CEK SENSOR  ");
    BuzzerAlert();
    return;
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("LOG");
  lcd.setCursor(3, 0);
  lcd.print((char)62);
  lcd.setCursor(7, 0);
  lcd.print("RTC");
  lcd.setCursor(10, 0);
  lcd.print((char)62);

  lcd.setCursor(1, 1);
  lcd.print(h);
  lcd.setCursor(10, 1);
  lcd.print(t);
  if (SD.begin(chipSelect)) {
    lcd.setCursor(4, 0);
    lcd.print("OK");
  }
  else {
    lcd.setCursor(4, 0);
    lcd.print("NO");
  }

  if (rtc.begin()) {
    lcd.setCursor(11, 0);
    lcd.print("OK");
  }
  else {
    lcd.setCursor(11, 0);
    lcd.print("NO");
  }

}

void printresult(float h, float t, float f) {
  DateTime now = rtc.now();
  digitalWrite(LED_BUILTIN, HIGH);
  tone(Buzzer, 1000);
  Serial.print("Kelembaban: ");
  Serial.print(h);
  Serial.println("%");

  Serial.print("Temperatur (C): ");
  Serial.print(t);
  Serial.println(" C");

  Serial.print("Temperatur (F): ");
  Serial.print(f);
  Serial.println(" F");
  Serial.println(" ");

  Serial.print("Time :");
  Serial.print(time);
  Serial.println(" S");
  Serial.print("Time RTC: ");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.println(" ");

  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print("Kelembaban: ");
    dataFile.print(h);
    dataFile.println("%");

    dataFile.print("Temperatur (C): ");
    dataFile.print(t);
    dataFile.println(" C");

    dataFile.print("Temperatur (F): ");
    dataFile.print(f);
    dataFile.println(" F");
    dataFile.println(" ");

    dataFile.print("Time :");
    dataFile.print(time);
    dataFile.println(" S");
    dataFile.println(" ");

    dataFile.print("Time RTC:");
    dataFile.print(now.hour(), DEC);  //print the values of the rtc
    dataFile.print(":");
    dataFile.print(now.minute(), DEC);
    dataFile.print(":");
    dataFile.print(now.second(), DEC);
    dataFile.println(" ");

    dataFile.close();
  } else {
    Serial.println("error opening datalog.txt");
  }
  noTone(Buzzer);
  digitalWrite(LED_BUILTIN, LOW);
}

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  dht.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(Buzzer, OUTPUT);

  //SD CARD CHECK
  while (!Serial) {
    ;
  }
  lcd.setCursor(0, 0);
  lcd.print("Initializing SD");
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    lcd.setCursor(0, 1);
    lcd.print("Card failed");
    while (1)
      ;
  }
  Serial.println("card initialized.");
  lcd.setCursor(0, 1);
  lcd.print("card initialized");

  delay(1000);
  lcd.clear();

  //RTC CHECK
#ifndef ESP8266
  while (!Serial)
    ;
#endif

  if (!rtc.begin()) {
    lcd.setCursor(0, 1);
    lcd.print("Couldnt find RTC");
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  lcd.setCursor(0, 0);
  lcd.print("RTC Start");
  lcd.setCursor(0, 1);
  lcd.print("Setting the time");
  Serial.println("Setting the time...");
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  delay(1000);
}


void loop() {
  unsigned long currentMillis = millis();

  //MAIN
  if (currentMillis - readsensorinterval >= readsensortime) {

    time = millis() / 1000;
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);
    checkallstatus(h, t, f);
    float hif = dht.computeHeatIndex(f, h);
    float hic = dht.computeHeatIndex(t, h, false);
    printresult(h, t, f);
    readsensorinterval = currentMillis;
  }

}
GND5VSDASCLSQWRTCDS1307+