 #include <ESP8266WiFi.h>
 #include <ESP8266HTTPClient.h>
 #include <Wire.h>
+
 
 /**
  * Interrupts caused by SoftwareSerial disturbs the WS2812 update cycle on the Arduino.

  return s;
}
 

void handleMessage(String msg){

  int index = msg.indexOf("ssid=");
  if (index != -1){
    String newSsid = msg.substring(index+5);
    newSsid.toCharArray(ssid, newSsid.length()+1);
    SerialPrint("New SSID: ");
    SerialPrintln(ssid);
  }
  
  index = msg.indexOf("pass=");
  if (index != -1){
    String newPass = msg.substring(index+5);
    newPass.toCharArray(password, newPass.length()+1);
    SerialPrintln("New password set: ********** ");
  }

  index = msg.indexOf("loadcfg");
  if (index != -1){
    if (loadCredentials()){
      SerialPrintln("Loaded credentials from EEPROM");
    }
    else{
      SerialPrintln("FAILED to load credentials");
    }
  }

  index = msg.indexOf("savecfg");
  if (index != -1){
    saveCredentials();
    SerialPrintln("Saved credentials to EEPROM");
  }

  index = msg.indexOf("clearcfg");
  if (index != -1){
    clearCredentials();
    SerialPrintln("Cleared credentials in EEPROM");
  }

  index = msg.indexOf("reconnect");
  if (index != -1){
    disconnectWifi();
    sendI2cMessage("wifi=0");

    if (connectWifi()){
      sendI2cMessage("wifi=1");
    }
  }

  index = msg.indexOf("ping");
  if (index != -1){
    delay(100);
    SerialPrintln("pong");
    sendI2cMessage("pong");
  }

  index = msg.indexOf("print");
  if (index != -1){
    SerialPrint("SSID: ");
    SerialPrintln(ssid);
    SerialPrint("Password: ");
    SerialPrintln(strlen(password)>0 ? "********" : "<no password>");
  }
}


void loop() {
  
  /** Check for serial communication */
  if(SerialAvailable()){
    String msg = SerialReadStringUntil('\r');
    handleMessage(msg);
  }

  /** Check for I2C messages */
  String msg = requestI2cMessage();
  if (msg.length() > 0){
    sendI2cMessage("s: " +msg.substring(0,10));
  }
   
  /** Read request from wifi */
  String request;
  if (checkWifiRequest(request)){
    if (request.indexOf("favicon.ico") == -1){

      int i = request.indexOf("/");
      int j = request.indexOf("HTTP/");
      if (i != -1 && j != -1){
        request = request.substring(i+1,j-1);
        if (request.startsWith("h=")){
          //sendI2cMessage(request);

        
          String s    = "http://192.168.1.120/h=";
          s          += request.substring(2).toInt();        

          HTTPClient http;
          http.begin(s);
          int httpCode = http.GET();
          if (httpCode > 0) {
            String payload = http.getString();
            Serial.println(payload);
          }
          else {
            Serial.println("payload size is zero");
          }
          http.end();   //Close connection        
        }
        else{
          handleMessage(request);
        }
      }
    }
  }

  delay(50);
}

/** Check if Wifi is up and running
 *  Check if a client has connected
 *  Wait for client to sends some data 
 *  
 *  returns true if all is good and request is populated
 */
bool checkWifiRequest(String &request){

  if (WiFi.status() != WL_CONNECTED){
    if (!connectWifi()){

  Wire.endTransmission();
}

/** I2C: Request data from slave node */
String requestI2cMessage(){

  String s = "";  
