   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(GREEN, HIGH);

  if (!ESP_ACTIVE)
    return; // bail to loop
  
  // setup serial with esp-01
  digitalWrite(LED, HIGH);
  Serial.begin(115200);
  Serial.setTimeout(10000);


void onLockStateChange() {
  long now = millis();
  int state = digitalRead(SWITCH);
 
  if (state != gLastState && now - gLastStateTimestamp > 50) {
    gLastStateTimestamp = millis();
    gStateChanged = true;
    gLastState = state;
  }
}

