   return 1;
 }
 
-// Custom function accessible by the API
+// Powers on the computer by pulling the power button low.
 int powerOn(String command) {
-  pinMode(D4, OUTPUT);
-  digitalWrite(D4,LOW);
-  delay(1500);
-  digitalWrite(D4,HIGH);
-  pinMode(D4, INPUT);
+  pinMode(POWER_BUTTON, OUTPUT);
+  digitalWrite(POWER_BUTTON, LOW);
+  delay(1000);
+  digitalWrite(POWER_BUTTON, HIGH);
+  pinMode(POWER_BUTTON, INPUT);
   return 1;
 }
 
-// Custom function accessible by the API
-int hardReset(String command) {
-  pinMode(D4, OUTPUT);
-  digitalWrite(D4,LOW);
+// Hard power off computer by holding power button 6 seconds.
+int hardPowerOff(String command) {
+  pinMode(POWER_BUTTON, OUTPUT);
+  digitalWrite(POWER_BUTTON, LOW);
   delay(6000);
-  digitalWrite(D4,HIGH);
-  pinMode(D4, INPUT);
+  digitalWrite(POWER_BUTTON,HIGH);
+  pinMode(POWER_BUTTON, INPUT);
   return 1;
 }
+
+// Turn on relay, then press power.
+// Parameter 0 = just do that.
+// Parameter 1 = also wait until computer shuts down, then automatically flip relay off.
+int powerOnGracefully(String command) {
+  //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) {
+    relayOffWhenPowerDown = 1;
+  } else {
+    relayOffWhenPowerDown = 0;
+  }
+  return 1;
+}

  #include "user_interface.h"
}

// Create aREST instance
aREST rest = aREST();

// WiFi parameters
const char* ssid = "joshua";
const char* password = "";

// The port to listen for incoming TCP connections
#define LISTEN_PORT           80

// Create an instance of the server
WiFiServer server(LISTEN_PORT);

// Variables to be exposed to the API
int relayOn;

// Declare functions to be exposed to the API
int powerOn(String command);
int flashLed(String command);
int hardReset(String command);

void setup(void)
{
  // Start Serial
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.hostname("ESP_EIDOLON_IPMI");
  wifi_station_set_hostname("ESP_EIDOLON_IPMI");
  delay(100);

  // Init variables and expose them to REST API
  relayOn = 0;
  rest.variable("relayOn",&relayOn);

  // Function to be exposed
  rest.function("power",powerOn);
  rest.function("led",flashLed);
  rest.function("reset",hardReset);

  // Give name & ID to the device (ID should be 6 characters long)
  rest.set_id("1");
  rest.set_name("ESP_EIDOLON_IPMI");

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {


  // Print the IP address
  Serial.println(WiFi.localIP());
}

// Custom function accessible by the API
