 "</html>";
 
 
-const char INDEX_HTML[] =
-"<!DOCTYPE HTML>"
-"<html>"
-"<head>"
-"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"
-"<title>ESP8266 Web Form Demo</title>"
-"<style>"
-"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""
-"</style>"
-"</head>"
-"<body>"
-"<h1>ESP8266 Web Form Demo</h1>"
-"<FORM action=\"/\" method=\"post\">"
-"<P>"
-"Sensor name : "
-"<INPUT type=\"text\" name=\"SENSOR_NAME\" <BR>"
-"WIFI access point :"
-"<INPUT type=\"text\" name=\"WIFI_NAME\" <BR>"
-"WIFI password :"
-"<INPUT type=\"text\" name=\"WIFI_PWD\" <BR>"
-"MQTT broker IP address : "
-"<INPUT type=\"text\" name=\"MQTT_IP\" <BR>"
-"MQTT port : "
-"<INPUT type=\"text\" name=\"MQTT_PORT\" <BR>"
-"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"
-"</P>"
-"</FORM>"
-"</body>"
-"</html>";
-
-
 void returnOK()
 {
   server.sendHeader("Connection", "close");

  }
  else {
    server.send(200, "text/html", INDEX_RELAY_HTML);
  }
}



void handleRoot()
{
   if (server.args() > 1){
      Serial.println("Field update requested");
      sensorName = server.arg("SENSOR_NAME");
      String wifiName   = server.arg("WIFI_NAME");
      String wifiPwd    = server.arg("WIFI_PWD");
      String mqttIp     = server.arg("MQTT_IP");
      String mqttPort   = server.arg("MQTT_PORT");

      Serial.print("sensorName :");
      Serial.println(sensorName);
      Serial.print("wifiName :");
      Serial.println(wifiName);
      Serial.print("wifiPwd :");
      Serial.println(wifiPwd);
      Serial.print("mqttIp :");
      Serial.println(mqttIp);
      Serial.print("mqttPort :");
      Serial.println(mqttPort);

      strcpy(&eepromArray[0][0], sensorName.c_str());
      strcpy(&eepromArray[1][0], wifiName.c_str());
      strcpy(&eepromArray[2][0], wifiPwd.c_str());
      strcpy(&eepromArray[3][0], mqttIp.c_str());
      strcpy(&eepromArray[4][0], mqttPort.c_str());

      for (int addr = 0; addr < sizeof(eepromArray); addr++){
          EEPROM.write(addr, ((char*)eepromArray)[addr]);
      }
      EEPROM.commit();
      Serial.println("EEPROM updated");
      returnOK();

  }
  else {
    Serial.println("no  arg");
    server.send(200, "text/html", INDEX_HTML);
  }

}




void handleSubmit()
{
  String LEDvalue;

  if (!server.hasArg("LED")) return returnFail("BAD ARGS");
  LEDvalue = server.arg("LED");
  if (LEDvalue == "1") {

    server.send(200, "text/html", INDEX_HTML);
  }
  else if (LEDvalue == "0") {

    server.send(200, "text/html", INDEX_HTML);
  }
  else {
    returnFail("Bad LED value");
  }
}


void toggleRelayState(){
    if(currentRelayState == RELAY_STATE_OFF)
    {
        currentRelayState= RELAY_STATE_ON;

        digitalWrite(LED, HIGH);
        digitalWrite(RELAY, HIGH);
        if(bootMode == BOOT_MODE_MQTT){
            char tmpChar = RELAY_STATE_ON;
            myMqtt->publishValue("state", &tmpChar);
        }
    }
    else
    {
        currentRelayState = RELAY_STATE_OFF;

        digitalWrite(LED, LOW);
        digitalWrite(RELAY, LOW);
        if(bootMode == BOOT_MODE_MQTT){
            char tmpChar = RELAY_STATE_OFF;
            myMqtt->publishValue("state", &tmpChar);
        }
    }
}


void loop() {
    // put your main code here, to run repeatedly:
    if(bootMode == BOOT_MODE_CONFIG){
        myConfigServer->handleClient();
        blinkLedConfigMode();
    }
    else
    {
        if(bootMode == BOOT_MODE_MQTT){
            if (!myMqtt->connected()) {
                myMqtt->reconnect();
                statusGiven = false;
            }
            if (myMqtt->connected() & !statusGiven) {
                deviceMqttReadyBlink();
                statusGiven = true;
            }
            myMqtt->loop();
        }
        else{
            server.handleClient();
        }

        //In all case monitor HW button
        if(digitalRead(BUTTON) == 0){
            pressCount +=1;
            //Only consider button pressed after 2 loops
            if(pressCount == 2){
                Serial.println("Toggling state");
                toggleRelayState();
            }
        }
        else{
            pressCount = 0;
        }
    }

delay(100);

}
