thingSpeak and SIM800L

Hi everyone,

I am trying to send the data of sensor SFR05 to thingspeak. I’ve followed the steps mentioned in this WEBSITE.

The code returned some error and I fixed it. I have following questions

1- The data shown in thingspeak is not complete meaning that some data is missed. how can I fixed it?
2- When I open the serial monitor some times one of or both of the following AT command return ERROR.

AT+SAPBR=1,1
AT+HTTPINIT

for example in this case that I put the output of serial monitor “AT+SAPBR=1,1” returned ERROR.
The code is the same as before so why does it happen SOMETIMES?

3-I have searched in the web to do this project and have seen two different method of data transferring to thingspeak. One the same as mine and another one explained in this Video, in which different code is utilized. Which is better and also more reliable?

4-most of the time the following code is not completely printed in the serial monitor as shown bellow:

String s = "AT+HTTPPARA=URL,http://api.thingspeak.com/update?api_key=C3YP3X3MWPRH6YAJ&field1=" + String(distance) + "&field2=" + String(duration);
gsm.print(s);
gsm.println((char)26);

My code is as follows:

//-----Electronics-project-hub>com------//
#include <SoftwareSerial.h>

SoftwareSerial gsm(11, 10); // RX, TX
const unsigned int TRIG_PIN=13;
const unsigned int ECHO_PIN=12;
long duration=0;
static float distance=0;
void setup()
{
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.begin(9600);
  gsm.begin(9600);
  modem_init();
  data_init();
  internet_init();
}
void loop()
{
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  

   duration= pulseIn(ECHO_PIN, HIGH);
   distance= duration/29.0/2.0;
   if(duration==0){
   Serial.println("Warning: no pulse from sensor");
   }
   else{
      Serial.print("distance to nearest object:");
      Serial.print(distance);
      Serial.println(" cm");
      Serial.println(duration);
   }
   delay(100);
 
  Send_data();
}

void modem_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  updateSerial();
  delay(1000);
  gsm.println("AT+CMGF=1");
  updateSerial();
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0");
  updateSerial();
  delay(1000);
}
void data_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  delay(1000); delay(1000);
  gsm.println("AT+CPIN?");
  updateSerial();
  delay(1000); delay(1000);
  gsm.println("AT+SAPBR =3,1,CONTYPE,GPRS");
  updateSerial();
  gsm.println("AT+SAPBR =3,1,APN,mtnirancell");
  updateSerial();
  delay(1000);
  gsm.print("AT+SAPBR=1,1");
  gsm.write(0x0d);
  gsm.write(0x0a);
  updateSerial();
  delay(3000);
}
void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    gsm.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(gsm.available()) 
  {
    Serial.write(gsm.read());//Forward what Software Serial received to Serial Port
  }
}
void internet_init()
{
  Serial.println("Please wait.....");
  delay(1000);
  gsm.println("AT+HTTPINIT");
  updateSerial();
  delay(1000); delay(1000);
  gsm.print("AT+HTTPPARA=CID,1");
  updateSerial();
  delay(1000);
}
void Send_data()
{
  //-----------------------Your API Key Here----------------------//
  //Replace xxxxxxxxxxx with your write API key.
  String s = "AT+HTTPPARA=URL,http://api.thingspeak.com/update?api_key=C3YP3X3MWPRH6YAJ&field1=" + String(distance) + "&field2=" + String(duration);
  gsm.print(s);
  gsm.println((char)26);
  //---------------------------------------------------------------//
  updateSerial();
  delay(1000);
  gsm.println("AT+HTTPACTION=0");
  updateSerial();
  delay(1000);
}
//-----Electronics-project-hub>com------//

A part of the Serial Monitor output is attached:

Many thanks for any help.
Mahdiyar

Is there any suggestion to fix this problem?

I am still waiting for any help.
Thanks