#include <WiFi.h>
// #include <PubSubClient.h> // DITUTUP karena Wokwi tidak mendukung
#define RELAY_PIN 4
#define POT_PIN 34
#define TEMP_PIN 35
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// const char* mqtt_server = "broker.hivemq.com"; // MQTT dinonaktifkan
WiFiClient espClient;
// PubSubClient client(espClient); // KOMEN karena PubSubClient tidak tersedia
void setup_wifi() {
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
int motion = LOW; // Buat jadi variabel global agar tidak reset setiap loop
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
setup_wifi();
Serial.println("Ketik '1' untuk motion HIGH, '0' untuk LOW di Serial Monitor.");
}
void loop() {
// Baca input manual motion dari Serial Monitor
if (Serial.available()) {
char input = Serial.read();
if (input == '1') {
motion = HIGH;
} else if (input == '0') {
motion = LOW;
}
}
int potValue = analogRead(POT_PIN);
int tempValue = analogRead(TEMP_PIN);
float voltage = tempValue * (3.3 / 4095.0); // ADC ESP32 12-bit
float temperatureC = voltage * 100;
// Threshold potensiometer (misalnya > 2000)
int threshold = 2000;
// LED hanya menyala jika motion HIGH dan potValue > threshold
if (motion == HIGH && potValue < threshold) {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
// Tampilkan di Serial Monitor
Serial.print("Temp: ");
Serial.print(temperatureC);
Serial.print(" C, Pot: ");
Serial.print(potValue);
Serial.print(", Motion: ");
Serial.print(motion == HIGH ? "Yes" : "No");
Serial.print(", LED: ");
Serial.println((motion == HIGH && potValue < threshold) ? "ON" : "OFF");
delay(1000);
}