 WiFiClient        g_wifiClient;
 PubSubClient      g_mqttClient(g_wifiClient);
 
+// Other event handling
+const uint8_t     DEBOUNCE_DELAY    = 200; // Delay in MS
+long              DEBOUNCE_TIME     = 0;
 
 //############################################################################
 //


// Track current status of the devices
boolean           g_relay_status                 = false;
boolean           g_bypass_activated             = false;
boolean           g_bypass_hold                  = false;
boolean           g_bypass_type                  = true; // True = switch; False = button



  // Listen for OTA messages
  ArduinoOTA.handle();

  // Prevent bouncing when performing actions following a read.
  if (millis() - DEBOUNCE_TIME >= DEBOUNCE_DELAY) {
    if(g_bypass_activated != digitalRead(BYPASS_PIN)) {
      //Read in the new state change.
      g_bypass_activated = digitalRead(BYPASS_PIN);
      
      // Perform the requested action
      if (g_relay_status == false && g_bypass_activated == HIGH) {
        #ifdef DEBUG
          Serial.println("INFO:  Bypass state switched from OFF to ON.");
        #endif
        g_relay_status = true;
        // Hold state if switch; prevents MQTT from overriding physical user.
        if(g_bypass_type) {
          g_bypass_hold = true;
        }
        setRelayStatus();
        publishRelayStatus();
      } else if (g_relay_status == true && g_bypass_activated == LOW) {
        #ifdef DEBUG
          Serial.println("INFO:  Bypass state switched from ON to OFF");
        #endif
        g_relay_status = false;
        // Release state if switch; prevents MQTT from overriding physical user.
        if(g_bypass_type) {
          g_bypass_hold = false;
        }
        setRelayStatus();
        publishRelayStatus();
      } else {
        #ifdef DEBUG
          Serial.println("INFO:  Requested bypass state matches current state.");
        #endif
        // Publish status anyway to prevent client hangs.
        publishRelayStatus();
      }
    }
    // Set the last time of button press.
    DEBOUNCE_TIME = millis();
  }
}
