   }
 }
 
+void onLockStateChange() {
+  long now = millis();
+  int state = digitalRead(SWITCH);
+  if (state != lastState && now - lastStateTimestamp > 50) {
+    lastStateTimestamp = millis();
+    lastState = state;
+    if (lastState == LOW) {
+      sendLock();
+    } else {
+      sendUnlock();
+    }
+  }
+}
 
 bool sendLock() {
-  if (!sendATConnectServer()) return false;
-  if (!sendATPrepareLockDoor()) return false;
-  if (!sendATLockDoor()) return false;
-  if (!sendATCipClose()) return false;
+  
+  digitalWrite(RED, HIGH);
+  digitalWrite(YELLOW, LOW);
+  digitalWrite(GREEN, LOW);
+  
+  if (espActive) {
+    if (!sendATConnectServer()) return false;
+    if (!sendATPrepareLockDoor()) return false;
+    if (!sendATLockDoor()) return false;
+    if (!sendATCipClose()) return false;
+  }
   return true;
 }
 
 bool sendUnlock() {
-  if (!sendATConnectServer()) return false;
-  if (!sendATPrepareUnlockDoor()) return false;
-  if (!sendATUnlockDoor()) return false;
-  if (!sendATCipClose()) return false;
+
+  digitalWrite(RED, LOW);
+  digitalWrite(YELLOW, HIGH);
+  digitalWrite(GREEN, LOW);
+  
+  if (espActive) {
+    if (!sendATConnectServer()) return false;
+    if (!sendATPrepareUnlockDoor()) return false;
+    if (!sendATUnlockDoor()) return false;
+    if (!sendATCipClose()) return false;
+  }
   return true;
 }
 

String commandUnlock = "GET /log_visit_state/1 HTTP/1.0\r\n\r\n";

int count;
int switchState;

void setup()
{
  pinMode(ESP_RESET, INPUT);
  //pinMode(ESP_RESET, OUTPUT);
  //digitalWrite(ESP_RESET, HIGH);

#include "Arduino.h"

#define ESP_RESET 9

#define RED 10
#define YELLOW 11
#define GREEN 12
#define LED 13
#define SWITCH 3

#define BLINK_COUNT 1
#define BLINK_DURATION 500
