#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int DHT_PIN = 13;
const int MQ2_PIN = 34;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Hello Guys!");
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(MQ2_PIN, INPUT);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int gasValue = analogRead(MQ2_PIN);
String gasStatus = (gasValue > 2000) ? "GAS TINGGI!" : "Gas Aman";
Serial.println("Suhu: " + String(data.temperature, 2) + "C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Level Gas: " + String(gasValue) + " - " + gasStatus);
Serial.println("---");
static unsigned long lastChange = 0;
static bool showGas = false;
if (millis() - lastChange > 2000) {
showGas = !showGas;
lastChange = millis();
lcd.clear();
}
if (showGas) {
lcd.setCursor(0,0);
lcd.print("Level Gas:" + String(gasValue));
lcd.setCursor(0,1);
lcd.print(String(gasStatus));
} else {
lcd.setCursor(0,0);
lcd.print("Suhu: " + String(data.temperature, 2) + " C");
lcd.setCursor(0,1);
lcd.print("Humidity: " + String(data.humidity, 1) + "%");
}
delay(500);
}