5 Seconds delay on ESP8266 in Server Request

Hi, guys. I have a concern on my Internet of Things project. I have 5 seconds delay on the components of ESP8266.

Short explanation of my project, is RFID based door security system using magnetic door lock.
Each time a person taps a card to the MFRC522 reader module, it will send the UID for then being verified through website using PHP based. The data will be parsed and the UID will be verified through the website as well. And then if the UID is true, it will run the relay to then power off the magnetic door lock so the door can be accessed.

The problem is, the fact that 5 seconds delay occurred in the step of verification through website using HTTPRequest ( i guess ) . Is it the problem with the ESP8266 or did I miss in my code?

#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#define RST_PIN 0
#define SS_PIN 2
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

MFRC522 mfrc522(SS_PIN, RST_PIN);

#define BELUM_TERDAFTAR 1
#define SUDAH_TERDAFTAR 2
#define MODE_ENROLL_SUDAH 3
#define MODE_ENROLL_BELUM 4
#define ID_BLOCKED 5

LiquidCrystal_I2C lcd(0x27, 16, 2);

String ID;
int STATUS_RFID;
bool STATUS_PINTU_WEB;

//Wifi & Server
WiFiClient client;
int8_t WIFI_STATUS = WL_IDLE_STATUS;
char ssid[] = "AiPhone";
char password[] = "30000000";
char server[] = "172.20.10.4";
char *path = "/web/device-communication.php?";
//char *path = "/website/komunikasi-device.php?";

#define pinBuzzer 4
#define pinRelay 10
#define pinTombol 8
#define pinSensor 9
#define pinLED 15
//#define pinRELAY 5
const int pinRELAY = 5;

#define SENSOR digitalRead(pinSensor)
#define TOMBOL_DALAM digitalRead(pinTombol)

String kondisi = "TidakTerdaftar";
int sensor, tombol, lastsensor = 0, counterS = 0, lasttombol = 0, counterT = 0;
bool pintu = false;
bool bukadalam = false;
bool dobrak = false;
bool Terdaftar = false;
bool tmblDalam = false;
bool oneCycle = false;

String Respon = "";
bool serverConnected = false;

String url;
unsigned long timeout;

String inStr, dataIn, Data[10];
char inChar;
boolean parsing = false;
String Nama, Nim, Prodi, Kelas;

void (*resetArduino)(void) = 0;




void setup(){
  Serial.begin(9600);
  SPI.begin();
  lcd.begin();
  lcd.backlight();

  pinMode(pinLED, OUTPUT);
  pinMode(pinRELAY, OUTPUT);
  digitalWrite(pinRELAY, LOW);

  WiFi.hostname("NodeMCU");
  WiFi.begin(ssid, password);

//  lcd.setCursor(0,0);
//  lcd.print("Silakan tap kartu!");
 

//  if(WiFi.status() == WL_NO_SHIELD){
//    Serial.println("WiFi module tidak ada");
//    while(1);
//  }
//  while (WIFI_STATUS != WL_CONNECTED){
//    Serial.print("Mencoba terhubung ke WiFi dengan SSID: ");
//    Serial.println(ssid);
//    WIFI_STATUS = WiFi.begin(ssid, password);
//  }

  while(WiFi.status() != WL_CONNECTED)
  {
    //progress sedang mencari WiFi
    delay(500);
    Serial.print(".");
  }
  printWifiStatus();

  Serial.println("RFID Version");
  mfrc522.PCD_Init();
  mfrc522.PCD_DumpVersionToSerial();
}

int delay_check = 30000;
unsigned long last_check = 0;


void loop(){
//  SENSOR_PINTU();
//    

  TOMBOL();

  //  Serial.print((String) "SENSOR: " + SENSOR);
  //  Serial.println((String) "  BUTTON: " + TOMBOL_DALAM);

  if (Terdaftar == true && bukadalam == false && counterS == 0) {
    //digitalWrite(2, LOW);
    digitalWrite(pinRELAY, HIGH);
    digitalWrite(pinLED, HIGH);
    
    lcd.setCursor(0,0);
    lcd.print("Selamat Datang!");
    lcd.setCursor(0,1);
    lcd.print("Have a nice day!");
    digitalWrite(pinRelay, HIGH);
    digitalWrite(pinBuzzer, LOW);
    delay(2000);
    digitalWrite(pinLED, LOW);
    digitalWrite(pinRELAY, LOW);
//    lcd.clear();
    Terdaftar = false;
  }
  if (pintu == true) {
    //digitalWrite(2, LOW);
    //digitalWrite(3, LOW);
    digitalWrite(pinRelay, LOW);
    kondisi = "TidakTerdaftar";
  }
  if (bukadalam == true && Terdaftar == false) {
    digitalWrite(pinRelay, HIGH);
    tmblDalam = true;
  }
  if (tmblDalam == true && Terdaftar == false && counterS == 1) {
    digitalWrite(pinRelay, LOW);
  }
  if (tmblDalam == false && Terdaftar == false && counterS == 1) {
    // Send alert to server
    if (!oneCycle) {
      serverConnected = httpRequest(server, path, 80, "alarm", "on");
      if (serverConnected) {
        oneCycle = true;
        if (!client.connected()) {
          client.stop();
          serverConnected = false;
        }
      }
    }
    Serial.println();
    Serial.print("Message : ");
    Serial.println("Peringatan, ada yang memasuki ruangan tanpa akses kartu");
    digitalWrite(pinBuzzer, HIGH);
    } 
    else {
    digitalWrite(pinBuzzer, LOW);
    oneCycle = false;
    //    if (oneCycle) {
    //      serverConnected = httpRequest("alarm", "off");
    //      if (serverConnected) {
    //        oneCycle = false;
    //        if (!client.connected()) {
    //          client.stop();
    //          serverConnected = false;
    //        }
    //      }
    //    }
  }


  if (tmblDalam == false && Terdaftar == false && counterS == 0) {
  ID = GET_RFID();
  if(ID != ""){
    Serial.println("\r\nUID     : " + ID);
    serverConnected = httpRequest(server, path, 80, "rfid", ID);
    handle_respon();

    if (STATUS_RFID == BELUM_TERDAFTAR) {
//        digitalWrite(pinBuzzer, HIGH);
        //digitalWrite(3, HIGH);
        delay(100);
        //digitalWrite(3, LOW);
        delay(300);
        Serial.println(F("Tidak Terdaftar"));

        //STATUS_RFID = 0;
      } 
    else if (STATUS_RFID == SUDAH_TERDAFTAR) {
        Terdaftar = true;
//        digitalWrite(pinBuzzer, HIGH);
        //digitalWrite(2, HIGH);
        delay(100);
        Serial.print(F("Nama       : ")); Serial.println(Nama);
        Serial.print(F("NIM        : ")); Serial.println(Nim);
        Serial.print(F("Prodi      : ")); Serial.println(Prodi);
        Serial.print(F("Kelas      : ")); Serial.println(Kelas);

        //STATUS_RFID = 0;
      }
     else if (STATUS_RFID == MODE_ENROLL_BELUM) {
//        digitalWrite(pinBuzzer, HIGH);
        delay(50);
        Serial.print(F("Mode enroll anggota [Belum Terdaftar]\r\n"));

        //STATUS_RFID = 0;
      }
     else if (STATUS_RFID == MODE_ENROLL_SUDAH) {
        Terdaftar = true;
//        digitalWrite(pinBuzzer, HIGH);
        delay(50);
        Serial.print(F("Mode enroll anggota [Sudah Terdaftar]\r\n"));
        Serial.print(F("Nama       : ")); Serial.println(Nama);
        Serial.print(F("NIM        : ")); Serial.println(Nim);
        Serial.print(F("Prodi      : ")); Serial.println(Prodi);
        Serial.print(F("Kelas      : ")); Serial.println(Kelas);

        //STATUS_RFID = 0;
      }
     else if (STATUS_RFID == ID_BLOCKED) {
//        digitalWrite(pinBuzzer, HIGH);
        delay(200);
        Serial.print(F("Kartu di blokir!\r\n"));
        Serial.print(F("Nama       : ")); Serial.println(Nama);
        Serial.print(F("NIM        : ")); Serial.println(Nim);
        Serial.print(F("Prodi      : ")); Serial.println(Prodi);
        Serial.print(F("Kelas      : ")); Serial.println(Kelas);
      }
     else {
        //Terdaftar = true;
//        digitalWrite(pinBuzzer, HIGH);
        delay(50);
        Serial.println(F("Coba lagi"));
        //resetArduino();
      }
      digitalWrite(pinBuzzer, LOW);
      delay(50);
    }
  }

    // check from database (for door control from website)
  if ((millis() > last_check + delay_check)) {
    serverConnected = httpRequest(server, path, 80, "control", "-");
    handle_respon();
    last_check = millis();
  }

  if (STATUS_PINTU_WEB) {
    while (1) {
      Terdaftar = true;
      Serial.print(F("Buka pintu!\r\n"));
      digitalWrite(pinRelay, HIGH);
      digitalWrite(pinLED, HIGH);
      serverConnected = httpRequest(server, path, 80, "control", "ok");
      // if (serverConnected) STATUS_PINTU_WEB = false;
      if (serverConnected) {
        STATUS_PINTU_WEB = false;
        break;
      }
    }
  }
}


String GET_RFID() {
  String CARD_ID;
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    long ID = 0;
    CARD_ID = " ";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      ID = ((ID + mfrc522.uid.uidByte[i]) * 10); // DECIMAL
      //CARD_ID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
      //CARD_ID.concat(String(mfrc522.uid.uidByte[i], HEX)); // HEXADECIMAL
    }
    CARD_ID = ID;
    // CARD_ID.toUpperCase();
    // Serial.print("UID tag :"); Serial.println(CARD_ID);
    mfrc522.PICC_HaltA();
    mfrc522.PCD_StopCrypto1();
    return CARD_ID;
  }
}
// #END READING RFID -------------------------------------------------//


//--------------------------------------------------------//
// COMMUNICATION WITH SERVER
//--------------------------------------------------------//
// Http request ke server --------------------//
bool httpRequest(char* server, String path, int port, String variabel, String value) {
  //if (!client.connected()) {
  client.stop();
  //}
  if (client.connect(server, port)) {
    client.print("GET ");
    client.print(path);
    // parameter 1
    client.print("variabel="); client.print(variabel);
    // parameter 2
    client.print("&value="); client.print(value);

    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    char endOfHeaders[] = "\r\n\r\n";
    client.find(endOfHeaders);
    return true;
  } 
  else {
    return false;
  }
}


// Handle respone from server --------------------//
void handle_respon() {
  // penanganan data yang diterima dari server
  if (serverConnected) {
    // Terima data untuk di parsing ------
    String getData;
    getData = client.readString();
    getData.trim();

     Serial.println(getData);

     //parsing data
    if (getData != "") {
      for (int i = 0; i <= getData.length(); i++) {
        inStr = getData.substring(i, (i + 1));
        dataIn += inStr;
        if (inStr == "#") parsing = true;
      }
      if (parsing) {
        parsingData();
        parsing = false;
        dataIn = "";
      }
    }

    // Serial.println(Data[1]);
    // Serial.println(Data[2]);
    // Serial.println(Data[3]);
    // Serial.println(Data[4]);
    // Serial.println(Data[5]);
    // Serial.println(Data[6]);

//          Data[1] == "rfid";
          
    if (Data[1] == "rfid") {
      if (Data[2] == "idbelum") STATUS_RFID = BELUM_TERDAFTAR;
      else if (Data[2] == "idsudah") STATUS_RFID = SUDAH_TERDAFTAR;
      else if (Data[2] == "enrollsudah") STATUS_RFID = MODE_ENROLL_SUDAH;
      else if (Data[2] == "enrollbelum") STATUS_RFID =  MODE_ENROLL_BELUM;
      else if (Data[2] == "idblocked") STATUS_RFID =  ID_BLOCKED;
      
      Nama = Data[3];
      Nim = Data[4];
      Prodi = Data[5];
      Kelas = Data[6];
    } 
    else if (Data[1] == "control" && Data[2] == "close") STATUS_PINTU_WEB = true;

    if (!client.connected()) {
      client.stop();
      serverConnected = false;
    }
    serverConnected = false;
    clear_respon_data();
  }
}// ---------------------------------------------------//



// Kosongkan variabel data yang diterima ----//
void clear_respon_data() {
  for (int i = 0; i <= 10; i++) {
    Data[i] = "";
  }
}
// -----------------------------------------//



// Proses parsing data ----------------------//
void parsingData() {
  int index = 0;
  //inisialisasi variabel, (reset isi variabel)
  Data[index] = "";
  //proses parsing data
  for (int i = 0; i <= dataIn.length(); i++) {
    //pengecekan tiap karakter dengan karakter (#) dan (|)
    if ((dataIn[i] == '#') || (dataIn[i] == '|')) {
      //increment variabel index, digunakan untuk merubah index array penampung
      index++;
      Data[index] = "";     //inisialisasi variabel array Data[index]
    } else {
      Data[index] += dataIn[i]; //proses tampung data saat pengecekan karakter selesai.
    }
  }
}// -----------------------------------//
// #END COMMUNICATION WITH SERVER --------------------------------//




//---------------------------------//
// Print status WiFi
//---------------------------------//
void printWifiStatus() {
  Serial.println("");
  Serial.println("WiFi Connected!");
//  digitalWrite(10, HIGH);
//  digitalWrite(pinBuzzer, HIGH);
  delay(100);
//  digitalWrite(pinBuzzer, LOW);
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  delay(2000);
}//---------------------------------//


void SENSOR_PINTU() {
  //  sensor = digitalRead(pinsensor);
  sensor = SENSOR;
  if (sensor != lastsensor) {
    if (sensor == LOW) {
      delay(500);
      counterS++;
    }
  }
  if (counterS == 1) {
    pintu = true;
    dobrak = true;
  }
  if (counterS >= 2) {
    counterS = 0;
    pintu = false;
    Terdaftar = false;
    tmblDalam = false;
  }
  lastsensor = sensor;
}



void TOMBOL() {
  // tombol = digitalRead(pintombol);
  tombol = TOMBOL_DALAM;
  if (pintu == false) {
    if (tombol != lasttombol) {
      if (sensor == HIGH) {
        counterT++;
      }
    }
    lasttombol = tombol;
    if (tombol == 1) {
      bukadalam = true;
    }
  }
  else if (pintu == true) {
    bukadalam = false;
    counterT = 0;
  }
}

Your code is pretty good documented through self-explaining names. Though not in english which makes it hard to understand.

For analysing I would add serial debug-output and activate the timestamp-function in the serial monitor to see where the 5-seconds delay occurs.

I mean this as a real open question: How much do you care about security?
Sending an UDP-message will be faster than a http-request but is easier to immitate.
The fastest method to transmit data over WiFi is to use the ESP-NOW-protocol.
You can transmit 250 bytes per message in 10 milliseconds.
This would work this way:
a second ESP8266 or ESP32 receives the ESP-NOW-message and then forwards the received data with a serial-connection to your computer.

I'm not very familiar with http TCP/IP Http get Http post etc.

There is a technique called asynchronious server.
Me personal I'm using the ESP-DASH-library for webinterfaces to control IoT which uses an asynchronious server. With this the reaction is almost instantly
and it is updated instantly even for multiple clients.

best regards Stefan