ESP8266 server response is not stable


hi guys, have esp8266 functioning server , gets requests android application. problem android application not receiving response times. misses response, 1/3 of time receives response json object. how can sure esp8266 returns response? it's important. code shown below:

 
code: [select]
string httpresponse;
  string httpheader;
  string content;
  content = f("{\"r\":\"");
  content += response;
  content += f("\"}");
  httpheader = f("http/1.1 200 ok\r\ncontent-type: application/json; charset=utf-8\r\n");
  httpheader += f("content-length: ");
  httpheader += content.length();
  httpheader += f("\r\n");
  httpheader += f("connection: close\r\n\r\n");
  httpresponse = httpheader + content;
  sendcipdata(connectionid, httpresponse);


void sendcipdata(int connectionid, string data) {
  string cipsend = f("at+cipsend=");
  cipsend += connectionid;
  cipsend += f(",");
  cipsend += data.length();
  cipsend += f("\r\n");
  sendcommand(cipsend, 2000, debug);
  senddata(data, 1000, debug);
  delay(1000);
}


/*
  name: sendcommand
  description: function used send data esp8266.
  params: command - data/command send; timeout - time wait response; debug - print serial window?
  returns: response esp8266 (if there reponse)
*/
string sendcommand(string command, const int timeout, boolean debug) {
  string response = "";
  string commandsuffix = f("\r\n");
  command += commandsuffix;

  unsigned long starttime = millis();
  unsigned long lastchartime = starttime;
//  serial.println(f(""));
//  serial.println(f("======================================================"));
//  serial.print(f("executing command: "));
//  serial.println(command);

  esp8266.print(command); // send read character esp8266

  long int time = millis();

  while ( (time + timeout) > millis())
  {
    while (esp8266.available())
    {
      // esp has data display output serial window
      char c = esp8266.read(); // read next character.
      //serial.print(c);
      response += c;
      lastchartime = millis();
    }
  }
//  serial.print(f("freememoryprint()="));
//  serial.println(freememory());



  if (debug)
  {
//    serial.print(f("received in "));
//    serial.print(lastchartime - starttime);
//    serial.println(f(" milliseconds."));
//    serial.print(response);
  }

  return response;
}

/*
  name: senddata
  description: function used send data esp8266.
  params: command - data/command send; timeout - time wait response; debug - print serial window?(true = yes, false = no)
  returns: response esp8266 (if there reponse)
*/
string senddata(string command, const int timeout, boolean debug) {
  string response = "";
  int datasize = command.length();
  char data[datasize];
  command.tochararray(data, datasize);
  esp8266.write(data, datasize); // send read character esp8266
  if (debug)
  {
//    serial.println(f("\r\n====== http response arduino ======"));
//    serial.write(data, datasize);
//    serial.println(f("\r\n========================================"));
  }

  long int time = millis();

  while ( (time + timeout) > millis())
  {
    while (esp8266.available())
    {
      // esp has data display output serial window
      char c = esp8266.read(); // read next character.
      response += c;
    }
  }

  if (debug)
  {
   // serial.print(response);
  }

  return response;
}

quote
the code shown below:
some of it, anyway. so, of answer is:

you need ed...


Arduino Forum > Using Arduino > Programming Questions > ESP8266 server response is not stable


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor