 #define SWITCHPIN D6 // Switch for "LOCKED" or "OPEN" status.
 #define LEDPIN D7 // Status LED.
 
+unsigned long lastSync = millis();
+
 rgb_lcd lcd;
 
-class OpenOperation {
-  public:
-
-    OpenOperation() {
-        _timer = new Timer(3000, (void (*)())&OpenOperation::callback, true);
-    }
-
-    void start() {
-        // TODO: Check if box is arelady open
-        // TODO: Delay 2 seconds after 2 consecutive open operations
-        if (!isRunning()) {
-            lcd.setCursor(0,1);
-            lcd.print("               ");
-            lcd.setCursor(0,1);
-            lcd.print("opening....");
-            digitalWrite(RELAYPIN, HIGH);
-            _timer->reset();
-        }
-    }
-
-    void stop() {
-        digitalWrite(RELAYPIN, LOW);
-        _timer->dispose();
-    }
-
-    bool isRunning() {
-        return _timer->isActive();
-    }
-
-  private:
-    Timer* _timer;
-    void callback(void) {
-        digitalWrite(RELAYPIN, LOW);
-    }
-};
-
-volatile int switchState = LOW;
+int switchState = LOW; // HIGH = locked, LOW = open
 volatile bool requestDisplayStatus = true;
 volatile bool requestDisplayTime = false;
 volatile bool requestDisplayConfigurationTime = true;
 volatile bool requestOpeningOperation = false;
 volatile bool isLCDBusy = false;
 
+class OpenOperation {
+  public:
+
+    OpenOperation() {
+        _timer = new Timer(2000, (void (*)())&OpenOperation::callback, true);
+    }
+
+    void start() {
+        // TODO: Check if box is arelady open
+        // TODO: Delay 2 seconds after 2 consecutive open operations
+        if (!isRunning()) {
+            // Only open the box when it is closed
+            if (switchState == HIGH) {
+                lcd.setCursor(0,1);
+                lcd.print("               ");
+                lcd.setCursor(0,1);
+                lcd.print("opening....");
+                digitalWrite(RELAYPIN, HIGH);
+                _timer->reset();
+            }
+        }
+    }
+
+    void stop() {
+        digitalWrite(RELAYPIN, LOW);
+        _timer->dispose();
+    }
+
+    bool isRunning() {
+        return _timer->isActive();
+    }
+
+  private:
+    Timer* _timer;
+    void callback(void) {
+        digitalWrite(RELAYPIN, LOW);
+    }
+};
+
 Timer clockTimer(1000, updateTime); // timer used to display the clock on the lcd display
 
 OpenOperation openOperation;


ConfigurationTime configurationTime;

STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

void setup() {
    // Ouput pins
    pinMode(LEDPIN, OUTPUT);

volatile int second;

int configureTime(String time);
void syncTimeWithCloudIfNeeded();

struct ConfigurationTime {
    uint8_t version;
