#define BLYNK_TEMPLATE_ID "TMPL6-_pORr5-H"
#define BLYNK_TEMPLATE_NAME "Smart Home Nia"
#define BLYNK_AUTH_TOKEN "1lptFHb4oGhFD6n2kXQA-gPEmmqDOLcP"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";

// DHT sensor
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Potensiometer (simulasi sensor gas dan api)
#define GASPIN 34
#define FLAMEPIN 35

// Relay Pin
#define RELAYPIN 13

// Speaker dan LED
#define SPEAKERPIN 32
#define LED_INDICATOR_PIN 25
#define LED_LAMP1_PIN 26
#define LED_LAMP2_PIN 27

// Blynk virtual pins
#define VPIN_TEMPERATURE V1
#define VPIN_HUMIDITY V2
#define VPIN_GAS V3
#define VPIN_FLAME V4
#define VPIN_RELAY V5
#define VPIN_LAMP1 V6
#define VPIN_LAMP2 V7

void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(RELAYPIN, OUTPUT);
  pinMode(GASPIN, INPUT);
  pinMode(FLAMEPIN, INPUT);
  pinMode(SPEAKERPIN, OUTPUT);
  pinMode(LED_INDICATOR_PIN, OUTPUT);
  pinMode(LED_LAMP1_PIN, OUTPUT);
  pinMode(LED_LAMP2_PIN, OUTPUT);
  digitalWrite(RELAYPIN, LOW);
  digitalWrite(SPEAKERPIN, LOW);
  digitalWrite(LED_INDICATOR_PIN, LOW);
  digitalWrite(LED_LAMP1_PIN, LOW);
  digitalWrite(LED_LAMP2_PIN, LOW);

  lcd.init(); // Initialize the LCD
  lcd.backlight(); // Turn on the LCD backlight

  lcd.setCursor(0, 0);
  lcd.print("Welcome to Smart");
  lcd.setCursor(0, 1);
  lcd.print("Home");
  delay(2000); // Delay to show the full message initially
  for (int i = 0; i < 16; i++) {
    lcd.scrollDisplayLeft(); // Scroll text to the left
    delay(300); // Delay 0.3 second between scroll steps
  }
  lcd.clear();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED){
    lcd.setCursor(0, 0);
    lcd.print("Connecting to");
    lcd.setCursor(0, 1);
    lcd.print("WiFi...");
    for (int i = 0; i < 16; i++) {
      lcd.scrollDisplayLeft(); //scroll text to the left
      delay(300); //Delay 0.3 second between scroll steps
    }
    lcd.clear();
    delay(1000);
    Serial.println("Connecting to WiFi....");
  }

  Serial.println("Connected to WiFi");
  lcd.setCursor(0, 0);
  lcd.print("Connected to WiFi");
  for (int i = 0; i < 16; i++){
    lcd.scrollDisplayLeft(); // scroll text to the left
    delay(300); // Delay 0.3 second between scroll steps
  }
  lcd.clear();

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
}

BLYNK_WRITE(VPIN_LAMP1) {
  int pinValue = param.asInt();
  digitalWrite(LED_LAMP1_PIN, pinValue);
}

BLYNK_WRITE(VPIN_LAMP2) {
  int pinValue = param.asInt();
  digitalWrite(LED_LAMP2_PIN, pinValue);
}

void loop() {
  Blynk.run();
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int gasValue = analogRead(GASPIN);
  int flameValue = analogRead(FLAMEPIN);

  Blynk.virtualWrite(VPIN_TEMPERATURE, temperature);
  Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
  Blynk.virtualWrite(VPIN_GAS, gasValue);
  Blynk.virtualWrite(VPIN_FLAME, flameValue);

  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print("C ");
  lcd.print("Gas: ");
  lcd.print(gasValue);
  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print("% ");
  lcd.print("Flame: ");
  lcd.print(flameValue);
  delay(1000);
  lcd.clear();

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C, Humidity: ");
  Serial.print(humidity);
  Serial.print(" %, Gas: ");
  Serial.println(gasValue);
  Serial.print(", Flame: ");
  Serial.println(flameValue);
  
  if (gasValue > 1000 || flameValue > 1000) {
    digitalWrite(RELAYPIN, HIGH); // Turn on relay
    Blynk.virtualWrite(VPIN_RELAY, 1);
    digitalWrite(SPEAKERPIN, HIGH); // activate speaker
    tone(SPEAKERPIN, 200, 50);
    digitalWrite(LED_INDICATOR_PIN, HIGH); // Turn on indicator LED
    lcd.setCursor(0, 0);
    lcd.print("Alert! Gas/Fire");
  } else {
    digitalWrite(RELAYPIN, LOW); // Turn off relay
    Blynk.virtualWrite(VPIN_RELAY, 0);
    digitalWrite(SPEAKERPIN, LOW); // Deactivate speaker
    noTone(SPEAKERPIN);
    digitalWrite(LED_INDICATOR_PIN, LOW); // turn off indicator LED
  }

  delay(2000);
}
NOCOMNCVCCGNDINLED1PWRRelay Module