+#include <ESP8266WiFi.h>
+
+//how many clients should be able to telnet to this ESP8266
+#define MAX_SRV_CLIENTS 1
+#define PORT 8081
+
+const char* ssid     = "***************";
+const char* password = "***************";
+
+// how much serial data we expect before a newline
+const unsigned int MAX_INPUT = 1024;
+
+WiFiServer server(PORT);
+WiFiClient serverClients[MAX_SRV_CLIENTS];
+
 void setup() {
-  pinMode(0, OUTPUT);  // Red led on the ESP, might be used later for debugging
+  // Initializing the used pins
+  // pinMode(0, OUTPUT);  // Red led on the ESP, might be used later for debugging
   pinMode(12, OUTPUT);
   pinMode(13, OUTPUT);
   pinMode(14, OUTPUT);
   pinMode(15, OUTPUT);
+
+  Serial.begin(115200);
+  delay(100);
+
+  // We start by connecting to a WiFi network
+  Serial.println("===========================");
+  Serial.print("Connecting to ");
+  Serial.println(ssid);
+
+  WiFi.begin(ssid, password);
+
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
+    Serial.print(".");
+  }
+
+  Serial.println("");
+  Serial.println("WiFi connected");
+  Serial.println("IP address: ");
+  Serial.println(WiFi.localIP());
+
+  // Start the server
+  server.begin();
+  server.setNoDelay(true);
+
+  Serial.print("Ready! Use 'telnet ");
+  Serial.print(WiFi.localIP()); Serial.print(" "); Serial.print(PORT);
+  Serial.println("' to connect");
 }
 
 void loop() {
-  digitalWrite(0, HIGH);
+  uint8_t i;
+  //check if there are any new clients
+  if (server.hasClient()) {
+    for (i = 0; i < MAX_SRV_CLIENTS; i++) {
+      //find free/disconnected spot
+      if (!serverClients[i] || !serverClients[i].connected()) {
+        if (serverClients[i]) serverClients[i].stop();
+        serverClients[i] = server.available();
+        Serial.print("New client: "); Serial.print(i);
+        continue;
+      }
+    }
+    //no free/disconnected spot so reject
+    WiFiClient serverClient = server.available();
+    serverClient.stop();
+  }
+
+  //check clients for data
+  for (i = 0; i < MAX_SRV_CLIENTS; i++) {
+    if (serverClients[i] && serverClients[i].connected()) {
+      if (serverClients[i].available()) {
+        //get data from the telnet client and push it to the UART
+        while (serverClients[i].available()) {
+          processIncomingByte (serverClients[i].read ());
+        }
+      }
+    }
+  }
+}
+
+void processIncomingByte (const byte inByte)
+{
+  static char input_line [MAX_INPUT];
+  static unsigned int input_pos = 0;
+
+  switch (inByte)
+  {
+    case '\n':   // end of text
+      input_line [input_pos] = 0;  // terminating null byte
+
+      // terminator reached! process input_line here ...
+      process_data (input_line);
+
+      // reset buffer for next time
+      input_pos = 0;
+      break;
+    case '\r':   // discard carriage return
+      break;
+    default:
+      // keep adding if not full ... allow for terminating null byte
+      if (input_pos < (MAX_INPUT - 1))
+        input_line [input_pos++] = inByte;
+      break;
+  }
+}
+
+// here to process incoming serial data after a terminator received
+void process_data (const char * data)
+{
+  Serial.println (data);
+  blink();
+}
+
+void blink() {
   digitalWrite(12, HIGH);
   delay(50);
   digitalWrite(12, LOW);

  digitalWrite(13, HIGH);
  delay(50);
  digitalWrite(13, LOW);
  digitalWrite(0, LOW);
  delay(50);
  digitalWrite(14, HIGH);
  delay(50);

  digitalWrite(15, LOW);
  delay(50);
}

