#include <WiFi.h>
#include <MQTT.h>
#include <DHT.h>
#include <ESP32Servo.h>
#define DHTPIN 13
#define DHTTYPE DHT22
#define PIR_IN 12
#define PIR_OUT 14
#define SERVO_WINDOW_PIN 2
#define SERVO_FAN_PIN 4
WiFiClient net;
MQTTClient client;
DHT dht(DHTPIN, DHTTYPE);
Servo servoWindow;
Servo servoFan;
const char ssid[] = "Wokwi-GUEST";
const char pass[] = "";
float suhu, kelembapan;
int jumlahOrang = 0;
int fanLevel = 0;
bool windowState; // false = tutup
bool isAutoMode = true;
bool lastPIRin = LOW, lastPIRout = LOW;
char buffer[10];
void connect() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
while (!client.connect("ec93d603")) {
delay(500);
Serial.print("*");
}
Serial.println("\nConnected to MQTT broker!");
client.subscribe("smarthomeIoT/mode");
client.subscribe("smarthomeIoT/kecepatan");
client.subscribe("smarthomeIoT/jendela");
}
void messageReceived(String &topic, String &payload) {
Serial.println("Message: " + topic + " = " + payload);
if (topic == "smarthomeIoT/mode") {
isAutoMode = (payload == "auto");//?
}
if (!isAutoMode) {
if (topic == "smarthomeIoT/kecepatan") {
controlFan(payload.toInt());
}
if (topic == "smarthomeIoT/jendela") {
controlWindow(payload == "true");
}
}
}
void controlFan(int level) {
fanLevel = level;//kontrol manual kipas
switch (level) {
case 1: servoFan.write(45); break;
case 2: servoFan.write(90); break;
case 3: servoFan.write(135); break;
default: servoFan.write(0); break;
}
}
void controlWindow(bool open) {
windowState = open;//kontrol jendela
servoWindow.write(open ? 180 : 0);
}
void handleAutoLogic() {
// logika kecepatan kipas berdasarkan suhu
if (suhu > 70) controlFan(3);
else if (suhu > 60) controlFan(2);
else if (suhu > 50) controlFan(1);
else controlFan(0);
// logika jendela buka/tutup berdasarkan suhu
controlWindow(suhu > 60);
}
void publishAll() {
dtostrf(suhu, 4, 2, buffer);
client.publish("smarthomeIoT/suhu", buffer);
dtostrf(kelembapan, 4, 2, buffer);
client.publish("smarthomeIoT/kelembapan", buffer);
itoa(jumlahOrang, buffer, 10);
client.publish("smarthomeIoT/jumlahOrang", buffer);
}
void publishInput() {
client.publish("smarthomeIoT/jendela", windowState ? "open" : "close");
itoa(fanLevel, buffer, 10);
client.publish("smarthomeIoT/kecepatan", buffer);
}
void setup() {
Serial.begin(115200);
pinMode(PIR_IN, INPUT);
pinMode(PIR_OUT, INPUT);
dht.begin();
servoWindow.attach(SERVO_WINDOW_PIN);
servoFan.attach(SERVO_FAN_PIN);
controlFan(0);
//controlWindow(false);
WiFi.begin(ssid, pass);
client.begin("broker.emqx.io", net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
if (!client.connected()) connect();
suhu = dht.readTemperature();
kelembapan = dht.readHumidity();
if (isnan(suhu) || isnan(kelembapan)) {
Serial.println("Gagal membaca DHT");
return;
}
if (isAutoMode) {
handleAutoLogic();
bool pirIn = digitalRead(PIR_IN);
if (pirIn && !lastPIRin) jumlahOrang++;
lastPIRin = pirIn;
// PIR keluar
bool pirOut = digitalRead(PIR_OUT);
if (pirOut && !lastPIRout && jumlahOrang > 0) jumlahOrang--;
lastPIRout = pirOut;
publishInput();
} else {
}
publishAll();
delay(2000);
}