#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include <WiFi.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
// Pin configuration
#define RELAY_PIN 4 // Relay connected to digital pin 4
#define LED_PIN 2 // LED pin
#define BUZZER_PIN 17 // Buzzer pin
#define DHT_PIN 5 // DHT sensor pin
#define SERVO_PIN 16 // Servo motor pin
#define TRIG_PIN 19 // Ultrasonic TRIG pin
#define ECHO_PIN 18 // Ultrasonic ECHO pin
// Constants
#define TEMP_THRESHOLD 30 // Temperature threshold to activate AC/fan
#define TEMP_HIGH_THRESHOLD 78 // High temperature threshold to activate buzzer
#define DISTANCE_THRESHOLD 5 // Distance threshold in cm to move servo
#define BUZZER_FREQUENCY 1000 // Frequency for buzzer tone
DHT dht(DHT_PIN, DHT22);
Servo servoMotor;
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD I2C address and size
void setup() {
Serial.begin(115200);
// Initialize I2C communication for the LCD on GPIO 23 (SDA) and GPIO 22 (SCL)
Wire.begin(23, 22);
lcd.begin(20, 4);
lcd.init();
lcd.backlight();
// Initialize other components
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
dht.begin();
servoMotor.attach(SERVO_PIN);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
float distance = getDistance();
// Display temperature and humidity on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C ");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity);
lcd.print("%");
// Control LED and relay based on temperature for AC/Fan
if (temperature >= 30, humidity <= 10) {
digitalWrite(LED_PIN, HIGH); // Turn on fan LED
lcd.setCursor(0, 2);
lcd.print("AC/Kipas Menyala "); // Clear previous message
} else {
digitalWrite(LED_PIN, LOW); // Turn off fan LED
lcd.setCursor(0, 2);
lcd.print("AC/Kipas Mati "); // Clear previous message
}
// Control buzzer based on high temperature
if (temperature >= 78) {
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
tone(BUZZER_PIN, BUZZER_FREQUENCY); // Play sound on buzzer
lcd.setCursor(0, 3);
lcd.print("Bahaya Kebakaran! "); // Clear previous message
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
noTone(BUZZER_PIN); // Stop sound on buzzer
lcd.setCursor(0, 3);
lcd.print(" "); // Clear previous message
}
// Control servo based on distance
if (distance <= 10) {
servoMotor.write(90); // Move servo to 90 degrees
} else {
servoMotor.write(0); // Return servo to 0 degrees
}
delay(1000); // Delay for readability
}
// Function to get distance from ultrasonic sensor
float getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2; // Calculate distance in cm
return distance;
}