#include <WiFi.h>
#include <HTTPClient.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int ledPin = 2;
const int buttonPin = 4;
const int servoPin = 14;
const int sdaPin = 25;
const int sclPin = 26;
const int buzzerPin = 12;
bool doorLocked = true;
void updateLcd(const char* message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Door Status:");
lcd.setCursor(0, 1);
lcd.print(doorLocked ? "Unlocked" : "Locked");
lcd.setCursor(0, 2);
lcd.print("Validation:");
lcd.setCursor(0, 3);
lcd.print(message);
}
void sendPostRequest() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://10.200.225.34:3000/api/door-status"); // Ganti <YOUR_PC_IP> dengan IP komputer Anda
http.addHeader("Content-Type", "application/json");
String payload = "{\"id\":\"001\",\"kode\":\"A1\"}"; // Ganti dengan ID dan Kode yang sesuai
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
updateLcd(response.c_str());
} else {
Serial.println("{\"id\":\"001\",\"kode\":\"A1\"}");
// Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}
void toggleDoor() {
doorLocked = !doorLocked;
digitalWrite(buzzerPin, HIGH);
delay(50);
digitalWrite(buzzerPin, LOW);
if (doorLocked) {
digitalWrite(ledPin, HIGH);
myservo.write(0);
} else {
digitalWrite(ledPin, LOW);
myservo.write(90);
}
updateLcd("Connected");
sendPostRequest();
}
void setup(void) {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
myservo.attach(servoPin);
Serial.begin(115200);
Wire.begin(sdaPin, sclPin);
lcd.begin(20, 4);
lcd.backlight();
updateLcd("Connecting...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
updateLcd("Connected");
}
void loop(void) {
if (digitalRead(buttonPin) == LOW) {
delay(5);
if (digitalRead(buttonPin) == LOW) {
toggleDoor();
while (digitalRead(buttonPin) == LOW);
delay(5);
}
}
}