 
 #define LED 13
 
-SoftwareSerial dbgSerial(11, 10); //Rx, Tx
-
-void setup(){
-	//Open serial communication
+SoftwareSerial ESPComm(11, 10); //Rx, Tx
+
+void setup(){ 
+	//Open ESPComm communication
+	ESPComm.begin(9600);
+	ESPComm.setTimeout(5000);
+	
 	Serial.begin(9600);
-	Serial.setTimeout(5000);
-	
-	dbgSerial.begin(9600);
-	dbgSerial.println("\n\r[ESP8266 Interface Test]");
+	Serial.println("\n\r[ESP8266 Interface Test]");
 	
 	pinMode(LED, OUTPUT);
 	digitalWrite(LED, LOW);
 	
 	//Test if the module is ready
-	Serial.print("AT\015\012");
+	ESPComm.print("AT\015\012");
 	delay(2000);
-	if(Serial.available())
+	if(ESPComm.available())
 	{
-		dbgSerial.println("Module is ready");
+		Serial.println("Module is ready");
 	}
 	else
 	{
-		dbgSerial.println("The module has not responded.");
+		Serial.println("The module has not responded.");
 		while(1);
 	}
 	
-	if(Serial.find("OK")){
-		dbgSerial.println("[Module Responded OK]");
+	if(ESPComm.find("OK")){
+		Serial.println("[Module Responded OK]");
 	}
 	/* else {
 		testFailed();

		}
	}
	if(!connected){
		dbgSerial.println("[### Failed to connect to WiFi ###]");
		testFailed();
	}
	
	digitalWrite(LED, HIGH);
	delay(1000);	
	
	dbgSerial.println("[Set single connection mode]");
	Serial.print("AT+CIPMUX=0\015\012");
	
	if(Serial.find("OK")){
		dbgSerial.println("[Module Responded OK]");
	}
	
	dbgSerial.println("[Requesting IP]");
	Serial.print("AT+CIFSR\015\012");
	for(int i = 0; i < 500; ++i){
		if(Serial.available()){
			dbgSerial.print((char) Serial.read());
		}
		delay(1);
	}

int failCount = 0;

void loop(){
	Serial.print("[Using External Code] [loop count: ");
	Serial.print(loops);
	Serial.print("] [failCount: "); 
	Serial.print(failCount);
	Serial.println("]");
//	reset();  //only CERTAIN way I've found of keeping it going
//  delay(5000);  //esp takes a while to restart
 // ESPComm.print("loops = ");  //check for successful connections to server
  //ESPComm.println(loops); 
  loops++;
  String cmd = "AT+CIPSTART=\"TCP\",\"";  //make this command: AT+CPISTART="TCP","146.227.57.195",80
  cmd += DST_IP;
  cmd += "\",80";
  cmd += "\015\012";
  
  ESPComm.print(cmd);  //send command to device

  delay(2000);  //wait a little while for 'Linked' response - this makes a difference
  if(ESPComm.find("Linked"))  //message returned when connection established WEAK SPOT!! DOESN'T ALWAYS CONNECT
  {
   Serial.print("Connected to server at ");  //debug message
   Serial.println(DST_IP);
  }
  else
  {
    Serial.println("'Linked' response not received");  //weak spot! Need to recover elegantly
	++failCount;
  }

  cmd =  "GET /~sexton/test.txt HTTP/1.0\r\n";  //construct http GET request
  cmd += "Host: cse.dmu.ac.uk\r\n\r\n";        //test file on my web
  ESPComm.print("AT+CIPSEND=");                //www.cse.dmu.ac.uk/~sexton/test.txt
  ESPComm.print(cmd.length());  //esp8266 needs to know message length of incoming message - .length provides this
  ESPComm.print("\015\012");
  
  if(ESPComm.find(">"))    //prompt offered by esp8266
  {
    Serial.println("found > prompt - issuing GET request");  //a debug message
    ESPComm.print(cmd);  //this is our http GET request
	ESPComm.print("\015\012");
  }
  else
  {
    ESPComm.print("AT+CIPCLOSE");  //doesn't seem to work here?
	ESPComm.print("\015\012");
    Serial.println("No '>' prompt received after AT+CPISEND");
  }

  //Parse the returned header & web page. Looking for 'Date' line in header

  
  if (ESPComm.find("Date: ")) //get the date line from the http header (for example)
  {
    for (int i=0;i<31;i++)  //this should capture the 'Date: ' line from the header
    {
      if (ESPComm.available())  //new cahracters received?
      {
        char c=ESPComm.read();  //print to console
        Serial.write(c);
      }
      else i--;  //if not, keep going round loop until we've got all the characters
    }
  }

  ESPComm.print("AT+CIPCLOSE");  
  ESPComm.print("\015\012");

  if(ESPComm.find("Unlink"))  //rarely seems to find Unlink? :(
  {
    Serial.println("Connection Closed Ok...");
  }
  else
  {
    //ESPComm.println("connection close failure");
  }
	
	
