   if (!sendATJoinNetwork()) blinkLEDForever();
   blink(BLINK_COUNT, BLINK_DURATION, 1, 1, 1);
 }
+
 void loop() {
+  bool success = true;
+  if (gStateChanged) {
+    if (gLastState == LOW) {
+      success = sendLock();
+    } else {
+      success = sendUnlock();
+    }
+    gStateChanged = false;
+  }
+  
   if (gLastState == LOW) {
     gCount++;
   } else if (gCount > 0) { 

      digitalWrite(YELLOW, LOW);
      digitalWrite(GREEN, HIGH);
  }
  
  delay(500);
}


#define BLINK_COUNT 1
#define BLINK_DURATION 500

#define ESP_ACTIVE true

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

int gCount;
int gLastState;
bool gStateChanged = false;
int gCourtesyDelay = 6;
long gLastStateTimestamp;

