-->
Page 1 of 1

ESP can't respond after TCP connection timeout

PostPosted: Thu Apr 16, 2015 10:49 am
by nate4495
Hello ESP world! I am trying to get my ESP to stay connected to a website using TCP connection, and it works!.... but after a certain time the Arduino is unable to talk with the ESP - and the TCP connection is lost forever, until I re-plug the Arduino into the source. In my code, I establish connection, then reset the ESP every 10 successful connections (pin 12 connected to RST on ESP), and if there is a failure I check if ESP is responding, if not reset again. It works for some time and then eventually just crashes... :?

Here is the code ---
Code: Select all#include <AltSoftSerial.h>
 AltSoftSerial altSerial;

#define SSID "network name"  //name of wireless access point to connect to
#define PASS "pass"  //wifi password
#define DST_IP "23.229.249.101" //site ip

int loops = 1;  //a counter for testing

void setup()  //initialise device & connect to access point in setup
{

  altSerial.begin(9600);    // hardware serial connects to esp8266 module
  Serial.begin(9600); // arduino serial
 
 reset();
}

void loop()
{
 
  altSerial.println("AT");
  delay(1000);
  if(altSerial.find("OK")) {
    Serial.print("Reset OK");
  }
 // Serial.print("loops = ");  //check for successful connections to server
  String cmd = "AT+CIPSTART=\"TCP\",\"";  //make this command: AT+CPISTART="TCP","146.227.57.195",80
  cmd += DST_IP;
  cmd += "\",80";

  altSerial.println(cmd);  //send command to device

  delay(2000);  //wait a little while for 'Linked' response - this makes a difference
 
   if(!altSerial.find("ERROR"))
  {
   Serial.println(" CONNECT GOOD");
  }
  else
  {
    Serial.println("connection problem");  //weak spot! Need to recover elegantly
    reset();
  }

  //cmd =  "GET /~sexton/test.txt HTTP/1.0\r\n";  //construct http GET request
 // cmd += "Host: cse.dmu.ac.uk\r\n\r\n";   
  cmd =  "GET http://harvestboxbeta.com/plants/status/get HTTP/1.0\r\n"; 
  cmd += "Host: www.harvestboxbeta.com\r\n\r\n";        //test file on my web
altSerial.print("AT+CIPSEND=");                //www.cse.dmu.ac.uk/~sexton/test.txt
  altSerial.println(cmd.length());  //esp8266 needs to know message length of incoming message - .length provides this

  if(altSerial.find(">"))    //prompt offered by esp8266
  {
   // Serial.println("found > prompt - issuing GET request");  //a debug message
    altSerial.println(cmd);  //this is our http GET request
  }
  else
  {
    altSerial.println("AT+CIPCLOSE");  //doesn't seem to work here?
    Serial.println("No '>' prompt received after AT+CPISEND");
      if(softwarereset())
      Serial.print("--ready--");
      else
      reset();
  }

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

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

  if(altSerial.find("Unlink"))  //rarely seems to find Unlink? :(
  {
    Serial.println("Connection Closed Ok!");
  }
  else
  {
    Serial.println("connection close failure");
  }
 
  //reset esp
 
  if(loops%10==0)
  reset();
 
 
}
//------------------------------------------------------------------------------------
void reset()
{
 digitalWrite(12,HIGH);
  digitalWrite(13,HIGH);
  delay(2000);
  digitalWrite(12,LOW);
  digitalWrite(13,LOW);
 
    altSerial.begin(9600);
 
  if(!cwmode3()) Serial.println("cwmode3 failed");
  boolean wifi_connected=false;  //not connected yet...
  for(int i=0;i<5;i++)    //attempt 5 times to connect to wifi - this is a good idea
  {
    if(connectWiFi())  //are we connected?
    {
      wifi_connected = true;  //yes
      break;              //get outta here!
    }
  }
}
//------------------------------------------------------------------------------
boolean connectWiFi()
{
 
  String cmd="AT+CWJAP?";
altSerial.println(cmd);
  delay(2000);
  if(altSerial.find(SSID))  //healthy response
  {
    Serial.println("Network recognized!");
    return true;
  }
 
  cmd="AT+CWJAP=\"";  //form eg: AT+CWJAP="dynamode","55555555555555555555555555"
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  altSerial.println(cmd);
  delay(5000); //give it time - my access point can be very slow sometimes
  if(altSerial.find("OK"))  //healthy response
  {
    Serial.println("Connected to WiFi!");
    return true;
  }
  else
  {
    Serial.println("Not connected to WiFi...");
    return false;
  }
}
//-------------------------------------------------------------------------------- 
//ditch this in favour of hardware reset. Done
boolean softwarereset()
{
  altSerial.println("AT+RST");
  if (altSerial.find("ready"))
  {
    return true;
  }
  else
  {
    return false;
  }
}
boolean cwmode3()
// Odd one. CWMODE=3 means configure the device as access point & station. This function can't fail?

{
  altSerial.println("AT+CWMODE=3");
  if (altSerial.find("no change"))  //only works if CWMODE was 3 previously
  {
    return true;
  }
  else
  {
    return false;
  }
}


And here is the output at the point where the ESP stops communicating ---
Code: Select allNetwork recognized!
Reset OK CONNECT GOOD
Conection established = 40
Thu, 16 Apr 2015 15:41:14 GMT
Connection Closed Ok!
Reset OK CONNECT GOOD
Conection established = 41
Thu, 16 Apr 2015 15:41:19 GMT
connection close failure
 CONNECT GOOD
No '>' prompt received after AT+CPISEND
cwmode3 failed
Not connected to WiFi...
Not connected to WiFi...
Not connected to WiFi...
Not connected to WiFi...
Not connected to WiFi...
connection close failure
 CONNECT GOOD
No '>' prompt received after AT+CPISEND
cwmode3 failed
Not connected to WiFi...
Not connected to WiFi...
...repeated


Thank you for any help!! :D

Re: ESP can't respond after TCP connection timeout

PostPosted: Thu Apr 16, 2015 1:16 pm
by AcmeUK
What version of the AT Firmware are you using?

Re: ESP can't respond after TCP connection timeout

PostPosted: Fri May 08, 2015 1:57 am
by EDUGIMENO
Youre doing a printLN, maybe you need to add +1 to the length? Cause "length" holds the actual string but not the termination char