#include <SPI.h>
#include <MFRC522.h>
#include <ESP32Servo.h>
// =====================================================
// PIN CONFIGURATION
// =====================================================
// LDR
const int LDR_PIN = 34;
// Ultrasonic
const int TRIG_PIN = 5;
const int ECHO_PIN = 13;
// Outputs
const int LED_LAMP = 26;
const int LED_PUMP = 27;
const int BUZZER_PIN = 25;
// Gas MQ135
const int GAS_PIN = 35;
// Servo Door Lock
const int SERVO_PIN = 4;
// RFID RC522
#define SS_PIN 21
#define RST_PIN 22
// =====================================================
// OBJECTS
// =====================================================
MFRC522 rfid(SS_PIN, RST_PIN);
Servo doorServo;
// =====================================================
// THRESHOLD
// =====================================================
const int LDR_THRESHOLD = 2000;
const int WATER_THRESHOLD = 20;
const int FULL_THRESHOLD = 5;
const int GAS_THRESHOLD = 2000;
// UID RFID WOKWI
String allowedUID = "C0 FF EE 99";
// =====================================================
// VARIABLES
// =====================================================
bool gasAlarm = false;
bool waterAlarm = false;
// =====================================================
// SETUP
// =====================================================
void setup() {
Serial.begin(115200);
// INPUT
pinMode(LDR_PIN, INPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(GAS_PIN, INPUT);
// OUTPUT
pinMode(TRIG_PIN, OUTPUT);
pinMode(LED_LAMP, OUTPUT);
pinMode(LED_PUMP, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// RFID SPI
SPI.begin(
18, // SCK
19, // MISO
23, // MOSI
21 // SS
);
rfid.PCD_Init();
// Servo
doorServo.attach(
SERVO_PIN,
500,
2400
);
lockDoor();
Serial.println();
Serial.println("===== SMART HOME STARTED =====");
}
// =====================================================
// LOOP
// =====================================================
void loop() {
cekRFID();
kontrolLampu();
kontrolAir();
kontrolGas();
updateBuzzer();
delay(200);
}
// =====================================================
// RFID
// =====================================================
void cekRFID() {
// cek kartu baru
if (!rfid.PICC_IsNewCardPresent()) {
return;
}
// baca kartu
if (!rfid.PICC_ReadCardSerial()) {
return;
}
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
if (i > 0) {
uid += " ";
}
String part = String(
rfid.uid.uidByte[i],
HEX
);
// tambahkan 0 jika 1 digit
if (part.length() == 1) {
part = "0" + part;
}
part.toUpperCase();
uid += part;
}
Serial.print("RFID UID: ");
Serial.println(uid);
// cocokkan UID
if (uid == allowedUID) {
Serial.println("ACCESS GRANTED");
openDoor();
delay(3000);
lockDoor();
} else {
Serial.println("ACCESS DENIED");
// bunyi buzzer 3x
for (int i = 0; i < 3; i++) {
tone(BUZZER_PIN, 1500);
delay(150);
noTone(BUZZER_PIN);
delay(150);
}
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
// =====================================================
// SERVO
// =====================================================
void openDoor() {
doorServo.write(90);
Serial.println("Door Open");
}
void lockDoor() {
doorServo.write(0);
Serial.println("Door Locked");
}
// =====================================================
// LIGHT CONTROL
// =====================================================
void kontrolLampu() {
int ldrValue = analogRead(LDR_PIN);
Serial.print("LDR: ");
Serial.println(ldrValue);
// gelap -> lampu ON
if (ldrValue > LDR_THRESHOLD) {
digitalWrite(LED_LAMP, HIGH);
} else {
digitalWrite(LED_LAMP, LOW);
}
}
// =====================================================
// WATER CONTROL
// =====================================================
void kontrolAir() {
long distance = readDistance();
Serial.print("Water Distance: ");
Serial.print(distance);
Serial.println(" cm");
// air rendah -> pompa nyala
if (distance > WATER_THRESHOLD) {
digitalWrite(LED_PUMP, HIGH);
} else {
digitalWrite(LED_PUMP, LOW);
}
// tank penuh -> alarm
waterAlarm = (distance <= FULL_THRESHOLD);
}
// =====================================================
// GAS CONTROL
// =====================================================
void kontrolGas() {
int gasValue = analogRead(GAS_PIN);
Serial.print("Gas: ");
Serial.println(gasValue);
gasAlarm = (gasValue > GAS_THRESHOLD);
}
// =====================================================
// BUZZER MANAGER
// =====================================================
void updateBuzzer() {
// alarm gas prioritas utama
if (gasAlarm) {
tone(BUZZER_PIN, 2000);
return;
}
// alarm air penuh
if (waterAlarm) {
tone(BUZZER_PIN, 1000);
return;
}
noTone(BUZZER_PIN);
}
// =====================================================
// ULTRASONIC
// =====================================================
long readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(
ECHO_PIN,
HIGH,
30000
);
// timeout
if (duration == 0) {
return 999;
}
long distance =
duration * 0.034 / 2;
return distance;
}