   }
 }
 
+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 (getClientRequests(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();        
+          getRemote(s);
+        }
+        else{
+          handleMessage(request);
+        }
+      }
+    }
+  }
+
+  delay(50);
+}
+
+
+/** I2C: Send data to slave node */
 void sendI2cMessage(String message){
   static char buf[I2C_BUF_SIZE];
   message.toCharArray(buf, message.length()+1);

  }
}

/** Store WLAN credentials to EEPROM */
void saveCredentials() {
  char ok[2+1] = "OK";
  EEPROM.begin(EEPROM_LEN);

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Wire.h>


/**
 * Interrupts caused by SoftwareSerial disturbs the WS2812 update cycle on the Arduino.
