#define BLYNK_TEMPLATE_ID "TMPL6IiJn-6d-"
#define BLYNK_TEMPLATE_NAME "SISTEM PEMANTAUAN"
#define BLYNK_AUTH_TOKEN "-KeUnO9HpPEaQQj8MFXXrzt7k-n6guoB"

#include <WiFi.h>
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
#include "DHT.h"
#include <RTClib.h>
#include <BlynkSimpleEsp32.h>

RTC_DS1307 rtc;

#define DHT_PIN 15
#define DHTTYPE DHT22
#define LED_PIN 33
#define LDR_PIN 35  // Pin untuk sensor cahaya
#define CO2_PIN 34  // Pin untuk sensor CO2 (analog)

DHT dht(DHT_PIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
    Serial.begin(9600);
    Serial.flush();
    
    pinMode(LDR_PIN, INPUT);
    pinMode(DHT_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);
    
    Wire.begin();
    if (!rtc.begin()) {
        Serial.println("RTC Tidak Terhubung");
        lcd.setCursor(0, 0);
        lcd.print("RTC Tidak Konek");
        while (1);
    }

    Serial.println("Sistem Pemantauan Gudang Penyimpanan Bahan Makanan");
    dht.begin();
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Final Project");
    lcd.setCursor(0, 1);
    lcd.print("IoT Bootcamp Edspert");
    lcd.setCursor(0, 2);
    lcd.print("Gudang Penyimpanan");
    lcd.setCursor(0, 3);
    lcd.print("Pemantauan");
    delay(2000);
    lcd.clear();

    // Koneksi Blynk
    Blynk.begin(BLYNK_AUTH_TOKEN, "Your_SSID", "Your_PASSWORD"); // Ganti dengan SSID dan password WiFi Anda
}

void loop() {
    Blynk.run(); // Menjalankan Blynk

    // RTC serial print untuk menampilkan waktu
    DateTime now = rtc.now();
    Serial.println("Tanggal             : " + String() + now.day() + "/" + now.month() + "/" + (now.year() % 100));
    Serial.println("Waktu               : " + String() + now.hour() + ":" + now.minute() + ":" + now.second());

    // Data sensor DHT22
    float hum = dht.readHumidity();
    float temp = dht.readTemperature();
    Serial.println("Suhu                : " + String(temp, 2) + " °C");
    Serial.println("Kelembaban          : " + String(hum, 1) + " %");

    // Data sensor LDR
    int ldrValue = analogRead(LDR_PIN);
    float lux = map(ldrValue, 4095, 0, 1024, 0);
    Serial.println("Kecerahan           : " + String(lux) + " Lux");

    // Data sensor CO2
    int co2Value = analogRead(CO2_PIN);
    Serial.println("Kadar CO2           : " + String(co2Value)); // Output raw CO2 value

    // Mengirim data ke Blynk
    Blynk.virtualWrite(V0, temp);     // Mengirim suhu ke V0
    Blynk.virtualWrite(V1, hum);      // Mengirim kelembaban ke V1
    Blynk.virtualWrite(V2, lux);      // Mengirim intensitas cahaya ke V2
    Blynk.virtualWrite(V3, co2Value); // Mengirim kadar CO2 ke V3

    // Output LCD untuk monitoring kondisi gudang
    lcd.setCursor(0, 0);
    lcd.print("Suhu       : " + String(temp, 1) + "C");
    
    lcd.setCursor(0, 1);
    lcd.print("Kelembaban : " + String(hum, 1) + "%");
    
    lcd.setCursor(0, 2);
    lcd.print("Kecerahan  : " + String(lux) + " Lux");
    
    lcd.setCursor(0, 3);
    lcd.print("Kadar CO2   : " + String(co2Value));
    
    Serial.println("--------------");
    delay(2000);
    lcd.clear();
}
NOCOMNCVCCGNDINLED1PWRRelay Module
GND5VSDASCLSQWRTCDS1307+