 #define POWER_LED D2
 
 // The port to listen for incoming TCP connections
-#define LISTEN_PORT 80
+ESP8266WebServer server(80);
 
 // How often to check for power off state
 unsigned long lastPowerCheck = millis();


// Import required libraries
#include <ESP8266WiFi.h>
#include <aREST.h>
extern "C" {
  #include "user_interface.h"
}

  Serial.println(WiFi.localIP());
}

void getStatus() {
  String preString = "{\"relayOn\":\"" + String(relayOn) + "\",\"computerOn\":\"" + String(computerOn) + "\",\"relayOffWhenPowerDown\":\"" + String(relayOffWhenPowerDown) + "\"}";
  const char * result = preString.c_str();
  server.send(200, "application/json", result);
}

// Powers on the computer by pulling the power button low.
void powerOn() {
  pinMode(POWER_BUTTON, OUTPUT);
  digitalWrite(POWER_BUTTON, LOW);
  delay(1000);
  digitalWrite(POWER_BUTTON, HIGH);
  pinMode(POWER_BUTTON, INPUT);
  server.send(200, "application/json", "{\"status\":\"complete\"}");
}

// Hard power off computer by holding power button 6 seconds.
void hardPowerOff() {
  pinMode(POWER_BUTTON, OUTPUT);
  digitalWrite(POWER_BUTTON, LOW);
  delay(6000);
  digitalWrite(POWER_BUTTON,HIGH);
  pinMode(POWER_BUTTON, INPUT);
  server.send(200, "application/json", "{\"status\":\"complete\"}");
}

// Turn on relay, then press power.
// Parameter 0 = just do that.
// Parameter 1 = also wait until computer shuts down, then automatically flip relay off.
void powerOnGracefully() {
  //Relay on
  digitalWrite(RELAY_PIN, HIGH);
  relayOn = 1;
  delay(5000);
  //Then power computer on as normal
  powerOn();
  computerOn = 1;
  //Setup auto off if requested
  //int autoOff = command.toInt();
  //if (autoOff == 1) {
  if (1) {
    relayOffWhenPowerDown = 1;
  } else {
    relayOffWhenPowerDown = 0;
  }
  server.send(200, "application/json", "{\"status\":\"complete\"}");
}
