#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const int buttonPin = 19;
const int buttonPin2 = 18;
const int trigPin = 14;
const int echoPin = 12;
const uint8_t buttonPins[] = { 19,18,5,17,16 };
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
#define SERVER_IP "http://tugas-ku-3934b4f9796f.herokuapp.com/login-iot"
long duration;
float distanceCm;
String key_pass = "";
LiquidCrystal_I2C lcd(0x27, 16, 2);
int countUp = 0;
const int servoPin = 4;
Servo servo;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
servo.attach(servoPin, 500, 2400);
servo.write(0);
WiFi.begin(ssid);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connecting");
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
lcd.init();
lcd.backlight();
printLcd("SMART DOOR LOCK",0,0);
printLcd("SECURITY SYSTEM",0,1);
delay(3000);
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT);
}
// pinMode(buttonPin, INPUT);
// pinMode(buttonPin2, INPUT);
// pinMode(5, INPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
lcd.clear();
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
if(distanceCm >= 0.0 && distanceCm <= 20){
countUp += 1;
if(countUp == 1){
lcd.backlight();
printLcd("Welcome",0,0);
printLcd("Please scan",0,1);
}
if(countUp >= 20000){
hitApi("Trespassed","adminlock");
countUp = 0;
}
for (uint8_t i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == 1) {
Serial.println(i);
functionMain(i);
countUp = 0;
}
}
}else{
countUp = 0;
lcd.clear();
lcd.noBacklight();
}
}
void printLcd(String note, int x, int y){
lcd.setCursor(x,y);
lcd.print(note);
}
void printTrespassed(){
lcd.setCursor(0,0);
lcd.print("UNAUTHORIZE");
lcd.setCursor(0,1);
lcd.print("ANOMALI DETECTED");
delay(5000);
lcd.setCursor(0,0);
lcd.print("UNAUTHORIZE");
lcd.setCursor(0,1);
lcd.print("PLEASE WAIT 20S");
delay(5000);
lcd.setCursor(0,0);
lcd.print("UNAUTHORIZE");
lcd.setCursor(0,1);
lcd.print("ANOMALI DETECTED");
delay(5000);
lcd.setCursor(0,0);
lcd.print("UNAUTHORIZE");
lcd.setCursor(0,1);
lcd.print("PLEASE WAIT 20S");
delay(5000);
lcd.clear();
}
void openDoor(){
servo.write(90);
}
void closeDoor(){
servo.write(0);
}
void functionMain(int inpt){
lcd.clear();
printLcd("Checking...",0,0);
printLcd("Please wait...",0,1);
if(inpt == 0){
hitApi("father123","father");
}
else if(inpt == 1){
hitApi("mother123","mother");
}
else if(inpt == 2){
hitApi("son123","son123");
}
else if(inpt == 3){
hitApi("son12366","son");
}
else if(inpt == 4){
hitApi("key-used","keyauth");
}
lcd.clear();
}
void hitApi(String username, String password){
HTTPClient http;
WiFiClient client;
String jsonString = "{\"username\":\""+username+"\",\"password\":\""+password+"\"}";
Serial.println(jsonString);
http.begin(client,SERVER_IP); // HTTP
http.addHeader("Content-Type", "application/json");
int Code = http.POST(jsonString);
String response = http.getString();
char json[500];
response.trim();
response.toCharArray(json , 500);
JsonDocument doc;
deserializeJson(doc, json);
const char* data = doc["data"];
// JsonDocument doc(1024);
// JsonDocument error = deserializeJson(doc, response);
// const char* data = doc["data"];
// const char* status = doc["success"];
// Serial.println(json);
// Serial.println(data);
Serial.println(Code);
http.end();
lcd.clear();
if(Code == 401){
printLcd("ID NOT MATCH!",0,0);
printLcd("Wait for 3sec",0,1);
delay(3000);
lcd.clear();
}
else if(username == "Trespassed"){
printTrespassed();
}
else if(Code == 200){
printLcd("SUCCESS!",0,0);
printLcd("WELCOME ",0,1);
printLcd(data,8,1);
openDoor();
delay(3000);
closeDoor();
lcd.clear();
}
else{
printLcd("INET ERROR",0,0);
printLcd("Scan again",0,1);
delay(2000);
lcd.clear();
printLcd("INET ERROR",0,0);
printLcd("Or use key",0,1);
delay(2000);
lcd.clear();
}
}