 bool isKeyCodeSet = false;
 long lastKeyCode = 0;
 
+char serialinputbuffer[SERIAL_INPUT_BUFFER_LENGTH];
+int serialinputptr;
+
 unsigned long lastUserAction = 0;
+unsigned long lastPingSent = 0;
 
 void setup() {
   Serial.begin(9600);
+
   resetPinCode();
   wg.begin();
+  resetSerialInputBuffer();
+  
+  pinMode(KEY_OK_PIN, OUTPUT);
+  digitalWrite(KEY_OK_PIN, HIGH);
+
+  Serial.println("{ \"status\": \"running\" }");
+}
+
+void resetSerialInputBuffer() {
+  for (int i=0; i<SERIAL_INPUT_BUFFER_LENGTH; i++) {
+    serialinputbuffer[i] = 0;
+  }
+  
+  serialinputptr = 0;
 }
 
 void resetPinCode() {

    isKeyCodeSet = false;
}

void loop() {
  if (wg.available()) {
    lastUserAction = millis();
    
    if (wg.getWiegandType() == 4) {
      int code = wg.getCode();
      if (code == 27) {
        resetPinCode();
      } else if (code == 13) {
        isPinCodeSet = true;
      } else {
        pincode[pincodeptr % PIN_LENGTH] = wg.getCode();
        pincodeptr++;
      }
    } else {
      lastKeyCode = wg.getCode();
      isKeyCodeSet = true;
    }

    if (isPinCodeSet && isKeyCodeSet) {
      printAndResetAccessCodes();
    }
  }

  if (Serial.available()) {
    char c = Serial.read();
    Serial.print(c);
  }

  if (lastUserAction + MAX_USER_ACTION_DELAY_MS < millis()) {
    lastUserAction = millis();
    resetPinCode();
    isKeyCodeSet = false;
  }
}

#include <Wiegand.h>
#define PIN_LENGTH 10
#define MAX_USER_ACTION_DELAY_MS 10000
#define SERIAL_INPUT_BUFFER_LENGTH 100
#define PING_DELAY_MS 10000

#define KEY_OK_STRING "ok"
#define PING_STRING "ping"

#define KEY_OK_PIN 4

WIEGAND wg;
bool isPinCodeSet = false;
