Stop send measurements arduino esp8266

I have made sketch with arduino uno and esp8266 in which i send measures of HC-SR04 to a website. Firstly my code was:

#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);  



boolean DEBUG=true;

int echoPin = 12; 
int trigPin = 13; 

long duration, distance; 
int alarm;

String alarms;

String data;
String server = "www.example.com"; // Send data to this site

String uri = "/add2.php";

int error = 0;
int cancel = 0;

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }
                   
}

boolean websiteWrite(long value1, int value2){ 
  if (value2 == 0){
    alarms = "OFF";
  }else{
    alarms = "ON";
  }
  
  data = "distance=" + String(value1) + "&alarm=" + alarms;
  
  espSerial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
  
  if( espSerial.find("OK")) {
  
  Serial.println("TCP connection ready"); cancel = 0;
  
  }
  else{
    cancel = cancel + 1;  Serial.println("cancel="); Serial.println(cancel);
  }
  delay(1000);
  
  String postRequest =
  
  "POST " + uri + " HTTP/1.0\r\n" +
  
  "Host: " + server + "\r\n" +
  
  "Accept: *" + "/" + "*\r\n" +
  
  "Content-Length: " + data.length() + "\r\n" +
  
  "Content-Type: application/x-www-form-urlencoded\r\n" +
  
  "\r\n" + data;
  
  String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  
  espSerial.print(sendCmd);
  
  espSerial.println(postRequest.length() );
  
  delay(500);
  
  if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
  
  if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
  
  while (espSerial.available()) {
  
  String tmpResp = espSerial.readString();
  
  Serial.println(tmpResp);
  
  }
  
  // close the connection
  
  espSerial.println("AT+CIPCLOSE");
  
  }
    error = 0;
  }else{
    error = error + 1;
    Serial.println("error=");
    Serial.println(error);
    
  }
  
  if(cancel >=15 || error >= 15){software_Reset() ;}
  
}

void setup() {
  delay(150000); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  DEBUG=true;           
  Serial.begin(9600); 
  
  espSerial.begin(9600);  
  
  //espSerial.println("AT+RST");         
  //showResponse(1000);
  
  //espSerial.println("AT+UART_DEF=9600,8,1,0,0");    
  //showResponse(1000);
  
  espSerial.println("AT+CWMODE=1");   
  showResponse(1000);

  espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  
  showResponse(5000);

  if (DEBUG)  Serial.println("Setup completed");  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  
  if (distance<=8){
    alarm=1;
  }else{
    alarm=0;
  }
  
      if (isnan(distance)) {
        if (DEBUG) Serial.println("Failed of calculation the distance");
      }
      else {
          if (DEBUG)  Serial.println("Distance="+String(distance)+" cm");
           websiteWrite(distance,alarm);                                      // Write values to website
      }
  
    
  //Needs 60 sec delay between updates    
  delay(60000);    
}

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
}

But after a few days stop send measurements to website. I followed someone’s comments here in the forum and convert Strings to char arrays so there are no strings they cause heap fragmentation. My code is:

void(* software_Reset) (void) = 0;

#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);  

boolean DEBUG=true;

int echoPin = 12; 
int trigPin = 13; 

long duration, distance; 
int alarm;

const int distancesSize = 20;
char distances[distancesSize+1];

char* alarms;

const int dataSize = 200; 
char data[dataSize+1];

const char* server = "www.example.com"; // Send data to this site

const char* uri = "/add5.php";


int cancel = 0;
int error = 0;

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }                 
}

boolean websiteWrite(long value1, int value2){   
  if (value1 > 500){
   strncpy(distances, distancesSize, ">500");
  }else{
    snprintf(distances,
      distancesSize,
      ("%d"),
      value1);
  }
 if (value2 == 0){
    alarms = "OFF";
  }else{
    alarms = "ON";
  }
  
  snprintf(data,
      dataSize,
      ("distance=%s &alarm=%s"),
      distances,
      alarms);
  
  espSerial.println("AT+CIPSTART=\"TCP\",\"" "www.example.com" "\",80");//start a TCP connection.
  
  if( espSerial.find("OK")) {
  
  Serial.println("TCP connection ready"); error = 0;
  
  }
  else{
    error = error + 1;  Serial.println("error="); Serial.println(error);
  }
  delay(1000);
  
  const int postRequestSize = 500; 
  char postRequest[postRequestSize+1];

   snprintf(postRequest,
      postRequestSize,
      "POST %s HTTP/1.0\r\nHost: %s\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s",
      uri,
      server,
      strlen(data),
      data);
  
  char* sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  
  espSerial.print(sendCmd);
  
  espSerial.println(strlen(postRequest));
  
  delay(500);
  
  if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
      
  if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
  
  while (espSerial.available()) {
  
  Serial.println(espSerial.readString());
  
  }
  
  // close the connection
  
  espSerial.println("AT+CIPCLOSE");
  
  }
    cancel = 0;
  }else{
    cancel = cancel + 1;
    Serial.println("cancel=");
    Serial.println(cancel);
    
  }
  
  if(error >=15 || cancel >= 15){software_Reset() ;}
  
 
}


void setup() {
  delay(150000); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  DEBUG=true;           
  Serial.begin(9600); 
  
  espSerial.begin(9600);  
  
  espSerial.println("AT+CWMODE=1");   
  showResponse(1000);

  espSerial.println("AT+CWJAP=\"ssid\",\"password\""); 
  
  showResponse(5000);
  
   if (DEBUG)  Serial.println("Setup completed");  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  
  if (distance<=8){
    alarm=1;
  }else{
    alarm=0;
  }
      if (isnan(distance)) {
        if (DEBUG) Serial.println("Failed of calculation the distance");
      }
      else {
          if (DEBUG)  Serial.println("Distance="+String(distance)+" cm");
           websiteWrite(distance,alarm);                                      // Write values to website
      }
  
    
  //Needs 60 sec delay between updates    
  delay(60000);    
}

Τhe problem remains and don’t send measurements to website after few days. Do I have a bug in the code? What can I do to solve it?

Do I have a bug in the code?

Your code STILL uses Strings.

  Serial.println(espSerial.readString());

Stop using that function.

Turn up the level of compiler warnings in preferences; it reveals a number of problems.

If i stop using the function readString(),then there is a problem sending data, when it sends and when not. How can I stop using this function and run the code properly? Can I convert this function to read()?

If i stop using the function readString(),then there is a problem sending data

Then the problem is with whatever code you wrote to replace readString(). Post your attempt.

How can I stop using this function and run the code properly?

I presume you mean write, not run. That's simple. Use a text editor and your brain.

Can I convert this function to read()?

Of course you can. After all, readString() calls read().

My code is the previous without the lines:

while (espSerial.available()) {
  
  Serial.println(espSerial.readString());
  
  }

My code is the previous without the lines

The previous what?

What did you replace those line with?

boolean websiteWrite(long value1, int value2){   
  if (value1 > 500){
   strncpy(distances, distancesSize, ">500");
  }else{
    snprintf(distances,
      distancesSize,
      ("%d"),
      value1);

%d is not the correct format specifier for a long.

I deleted these lines of code. My new code is:

  while (espSerial.available()) {

  Serial.println(espSerial.readString());

  }

These lines?

Yes these lines I deleted and it shows me the problem. My code is

void(* software_Reset) (void) = 0;

#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);  

boolean DEBUG=true;

int echoPin = 12; 
int trigPin = 13; 

long duration, distance; 
int alarm;

const int distancesSize = 20;
char distances[distancesSize+1];

char* alarms;

const int dataSize = 200; 
char data[dataSize+1];

const char* server = "www.example.com"; // Send data to this site

const char* uri = "/add5.php";


int cancel = 0;
int error = 0;

void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }                 
}

boolean websiteWrite(long value1, int value2){   
  if (value1 > 500){
   strncpy(distances, distancesSize, ">500");
  }else{
    snprintf(distances,
      distancesSize,
      ("%d"),
      value1);
  }
 if (value2 == 0){
    alarms = "OFF";
  }else{
    alarms = "ON";
  }
  
  snprintf(data,
      dataSize,
      ("distance=%s &alarm=%s"),
      distances,
      alarms);
  
  espSerial.println("AT+CIPSTART=\"TCP\",\"" "www.example.com" "\",80");//start a TCP connection.
  
  if( espSerial.find("OK")) {
  
  Serial.println("TCP connection ready"); error = 0;
  
  }
  else{
    error = error + 1;  Serial.println("error="); Serial.println(error);
  }
  delay(1000);
  
  const int postRequestSize = 500; 
  char postRequest[postRequestSize+1];

   snprintf(postRequest,
      postRequestSize,
      "POST %s HTTP/1.0\r\nHost: %s\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s",
      uri,
      server,
      strlen(data),
      data);
  
  char* sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  
  espSerial.print(sendCmd);
  
  espSerial.println(strlen(postRequest));
  
  delay(500);
  
  if(espSerial.find(">")) { Serial.println("Sending.."); espSerial.print(postRequest);
      
  if( espSerial.find("SEND OK")) { Serial.println("Packet sent");
  
  
  
  // close the connection
  
  espSerial.println("AT+CIPCLOSE");
  
  }
    cancel = 0;
  }else{
    cancel = cancel + 1;
    Serial.println("cancel=");
    Serial.println(cancel);
    
  }
  
  if(error >=15 || cancel >= 15){software_Reset() ;}
  
 
}


void setup() {
  delay(150000); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  DEBUG=true;           
  Serial.begin(9600); 
  
  espSerial.begin(9600);  
  
  espSerial.println("AT+CWMODE=1");   
  showResponse(1000);

  espSerial.println("AT+CWJAP=\"ssid\",\"password\""); 
  
  showResponse(5000);
  
   if (DEBUG)  Serial.println("Setup completed");  
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
  
  if (distance<=8){
    alarm=1;
  }else{
    alarm=0;
  }
      if (isnan(distance)) {
        if (DEBUG) Serial.println("Failed of calculation the distance");
      }
      else {
          if (DEBUG)  Serial.println("Distance="+String(distance)+" cm");
           websiteWrite(distance,alarm);                                      // Write values to website
      }
  
    
  //Needs 60 sec delay between updates    
  delay(60000);    
}

Yes these lines I deleted and it shows me the problem.

You need to explain this. What shows you a problem? What is the problem that it, whatever it is, shows you?

You were supposed to replace the line in the body with code that uses read() in a loop, to read all the data.

PaulS: You were supposed to replace the line in the body with code that uses read() in a loop, to read all the data.

Tell him Paul ! oh i you use just a single loop, make sure you get the whole response, 1200 us per byte.