Arduino UNO and ESP8266 module GET using AT command sometimes FAIL.

Hello!

I am using an ESP8266 to put some data into a mysql database, using a php script on my server.
All is going well, except that not all the time the connection is ended with success.

Yo will see in the picture below, the serial output of the Arduino. Here you can observe that every now and then, AT+CIPSEND=0 and AT+CIPCLOSE=0, ends with fail response, and in this case, the transmission is not working. Why this happens ?

Thank you!

Here is the code:

#include <SoftwareSerial.h>
float var1, var2, var3, var4, var5;

#define RX 10
#define TX 11

String AP = "******";       // CHANGE ME
String PASS = "*******"; // CHANGE ME
String HOST = "home.adrianc.ro";
String PORT = "80";


int countTrueCommand;
int countTimeCommand; 
boolean found = false; 

SoftwareSerial esp8266(RX,TX);

void setup() {
         Serial.begin(9600);
         esp8266.begin(115200);
         sendCommand("AT",5,"OK");
         sendCommand("AT+CWMODE=1",5,"OK");
         sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
} 
void loop() {


var1 = 20.65;
var2 = 55.6;
var3 = 21.4;
var4 = 41.5;
var5 = 20.4;


String getData = "GET /test/add_data.php?";
getData += "var1=";
getData += var1;
getData += "&";
getData += "var2=";
getData += var2;
getData += "&";
getData += "var3=";
getData += var3;
getData += "&";
getData += "var4=";
getData += var4;
getData += "&";
getData += "var5=";
getData += var5;

sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,16,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");

 esp8266.println(getData);
 delay(4000);
 countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}


void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
 
    countTimeCommand++;
  }
 
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
 
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
 
  found = false;
 }

you could use a library which properly handles the AT commands for you.
for AT version 1.7 you can use my WiFiEspAT library, for older version of AT fiirmware use the old WiFiEsp library

Note that the SoftwareSerial library doesn't work reliably at 115200 baud. I recommend configuring the AT firmware and the SoftwareSerial library to communicate at 9600 to be certain of reliable operation.

Juraj:
you could use a library which properly handles the AT commands for you.
for AT version 1.7 you can use my WiFiEspAT library, for older version of AT fiirmware use the old WiFiEsp library

I tryed also the library you indicated, with no success. Maybe I make a mistake somewhere in the code, or the firmware of my ESP is old.

pert:
Note that the SoftwareSerial library doesn't work reliably at 115200 baud. I recommend configuring the AT firmware and the SoftwareSerial library to communicate at 9600 to be certain of reliable operation.

Seems like the baudrate was the problem. I set the ESP8266 baud rate to 9600, and now seems to work all the time. 0 failed transmission. Thank you very much!

You're welcome. I'm glad to hear it's working now. Enjoy!
Per