A9G Receives HTTP Response takes long time

I am using A9G with ESP32, using below code but as you can see the serial monitor, the HTTP response takes long time to load even I have reduced the delay, command sent at 16:24:20 and response received at 16:24:36. Also, is it possible to remove http headers from the response to get actual payload only.



#include <HardwareSerial.h>
#define DEBUG true
#define BAUD_RATE 115200
#include<stdio.h>
#include <ArduinoJson.h>
#define A9G_SIZE_RX  1024    // used in A9G.setRxBufferSize()
#define data_BUFFER_LENGTH   1024
/***********************************/
#define A9G_PON     13  //ESP12 GPIO16 A9/A9G POWON
#define A9G_POFF    12  //ESP12 GPIO15 A9/A9G POWOFF
#define A9G_WAKE    27  //ESP12 GPIO13 A9/A9G WAKE
#define A9G_LOWP    14  //ESP12 GPIO2 A9/A9G ENTER LOW POWER MODULE

int A9GPOWERON();

String response = "";
HardwareSerial A9G(1);

void setup() {

  Serial.begin(115200); /* Define baud rate for A9G communication */
  A9G.begin(9600, SERIAL_8N1, 35, 32);    // RX and TX of ES32
  A9G.setRxBufferSize(A9G_SIZE_RX);

 Serial.println("\nA9G Test Started...");

  for (char ch = ' '; ch <= 'z'; ch++) {
    A9G.write(ch);
  }
  A9G.println("");
  delay(500);
  //    sendData("AT+RST=1", 1000, DEBUG); //Software reset
  //    delay(500);

  /************************************/
  pinMode(A9G_PON, OUTPUT);//LOW LEVEL ACTIVE
  pinMode(A9G_POFF, OUTPUT);//HIGH LEVEL ACTIVE
  pinMode(A9G_LOWP, OUTPUT);//LOW LEVEL ACTIVE

  digitalWrite(A9G_PON, HIGH); // Keep A9G OFF(HIGH) initially
  digitalWrite(A9G_POFF, LOW);
  digitalWrite(A9G_LOWP, HIGH);

  Serial.println("Turning ON A9G...");
  Serial.println();

  if (A9GPOWERON() == 1)
  {
    Serial.println("A9G POWER ON.");
    Serial.println();
  }

  sendData("ATE0", 1000, DEBUG);
  sendData("AT+CGSN", 3000, DEBUG);  //IMEI No.
  sendData("AT+CREG?", 3000, DEBUG);
  sendData("AT+CSQ", 1000, DEBUG);
  sendData("AT+CMGF=1", 1000, DEBUG);
  sendData("AT+CPMS=\"SM\",\"SM\",\"SM\"", 1000, DEBUG);
  //  sendData("AT+CMGDA=\"DEL ALL\"", 1000, DEBUG); //Delete All Messages
  sendData("AT+CGATT=1", 3000, DEBUG);  //Attach network, this command is necessary if the Internet is needed.
  sendData("AT+CGDCONT=1,\"IP\",\"CITMOI\"", 1000, DEBUG);  //Set PDP parameter
  sendData("AT+CGACT=1,1", 3000, DEBUG);  //Activate PDP, can access to Internet when activated properly
  sendData("AT+CIFSR", 1000, DEBUG);
  sendData("AT+GPS=1", 1000, DEBUG);
  sendData("AT+GPNT=1", 500, DEBUG); //Set the status of GPS light
  sendData("AT+LOCATION=2", 500, DEBUG);  //1:Get base address, 2:get gps address
  sendData("AT+CCLK?", 500, DEBUG);
  sendData("AT+CCLK?", 500, DEBUG);
}

void loop() {
  //  while (A9G.available() > 0) {
  //    A9G.write(A9G.read());
  ////    yield();
  //  }
//  while (A9G.available() > 0) {
//    A9G.write(A9G.read());
//    //    yield();
//  }

  //****For Sending Data to MySQL****
  //  String cmdString = "AT+HTTPGET=\"https://apk.vehtechs.com/api/imei-update-info?imei=123456789012345&lat=11.11&lng=22.22&speed=100&wifi_ssid1=ABC5GHz&wifi_ssid2=ABC5GHz&wifi_ssid3=ABC5GHz&wifi_ssid4=ABC5GHz&wifi_connected=1&gyro=1&battery=1&fuel=100&door1=1&door2=1&door3=0&door4=1\"";

  //****For Receivng Data from MySQL****
  String cmdString = "AT+HTTPGET=\"http://apk.vehtechs.com/api/imei?imei=123456789456789\"";
  String input = sendData(cmdString, 500, DEBUG);

  StaticJsonDocument<512> doc;

  DeserializationError error = deserializeJson(doc, input);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    //return;
  }
 else{
  const char* isArm = doc["isArm"]; // "1"
  const char* wifi = doc["wifi"]; // "0"
  const char* wifi_password = doc["wifi_password"]; // nullptr
  const char* wifi_connssid = doc["wifi_connssid"]; // "0"
  int wifi_disconnect = doc["wifi_disconnect"]; // 0
  const char* wifi_disc_name = doc["wifi_disc_name"]; // nullptr
  int engine = doc["engine"]; // 1
  const char* phoneNumber = doc["phoneNumber"]; // "542351685"
  const char* number1 = doc["number1"]; // nullptr
  const char* number2 = doc["number2"]; // nullptr
  const char* number3 = doc["number3"]; // nullptr
  const char* number4 = doc["number4"]; // nullptr
  const char* number5 = doc["number5"]; // nullptr
  const char* gyro = doc["gyro"]; // "0"
  const char* gps = doc["gps"]; // "1"
  const char* powerSupply = doc["powerSupply"]; // "1"
  const char* plateNo = doc["plateNo"]; // "32568"
  
  Serial.print("isArm= ");
  Serial.println(isArm);
  Serial.print("wifi= ");
  Serial.println(wifi);
  Serial.print("wifi_password= ");
  Serial.println(wifi_password);
  Serial.print("wifi_connssid= ");
  Serial.println(wifi_connssid);
  Serial.print("wifi_disconnect= ");
  Serial.println(wifi_disconnect);
  Serial.print("wifi_disc_name= ");
  Serial.println(wifi_disc_name);
  Serial.print("engine= ");
  Serial.println(engine);
  Serial.print("phoneNumber= ");
  Serial.println(phoneNumber);
  Serial.print("number1= ");
  Serial.println(number1);
  Serial.print("number2= ");
  Serial.println(number2);
  Serial.print("number3= ");
  Serial.println(number3);
  Serial.print("number4= ");
  Serial.println(number4);
  Serial.print("number5= ");
  Serial.println(number5);
  Serial.print("gyro= ");
  Serial.println(gyro);
  Serial.print("gps= ");
  Serial.println(gps);
  Serial.print("powerSupply= ");
  Serial.println(powerSupply);
  Serial.print("plateNo= ");
  Serial.println(plateNo);
  Serial.println();
  Serial.println();
  Serial.println();
 }
 delay(500);
}




String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  Serial.print("Command Sent: "); Serial.println(command);
  A9G.println(command);

  unsigned long time = millis();
  while (A9G.available() == 0) {}

  while (A9G.available())
  {
    char c = A9G.read();
    response += c;
    //Serial.print(c);
    delay(10);
  }

  if (debug)
  {
    Serial.print("A9G Response: ");
    Serial.println(response);
  }

  if (response.indexOf('{') > 0) {
    int l = response.indexOf('{');
    String res = response.substring(l);
    Serial.println(res);
    response = res;
  }
  return response;
}


int A9GPOWERON()                 //Send Command to A9G to Turn ON
{
  digitalWrite(A9G_PON, LOW);
  delay(3000);
  digitalWrite(A9G_PON, HIGH);
  delay(5000);
  String msg = String("");
label:
  msg = sendData("AT", 2000, DEBUG);
  if ( msg.indexOf("OK") >= 0 ) {
    Serial.println("GET OK");
    Serial.println();
    return 1;
  }
  else {
    Serial.println("NOT GET OK");
    Serial.println();
    goto label;     //If NOT GET OK GOTO label
    return 0;
  }
}

@ayazurrehman786, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

You wait 10ms after every byte you received. Given the response use about the 500 bytes you reserved in the buffer this sums up to 5 sec in addition to the time this needed for the A9G to resolve the host, connect to the server, send the request and receive the answer.
As you posted the output as a picture (although it's just text) I cannot check the actual size without typing it in again, I don't waste my time.

What timing did you expect?

Helllo Mr ayazurrehman786, i want to know how did you correct this error because i have faced with this same issues too. my email: didoxgusenga@gmail.com

Thank you Sir.

Below should resolve the issue, I have removed HTTP header also and receiving the actual payload only in the HTTP_Response.
Also note that A9G default response time for HTTP command is 5-6 seconds which can't be changed. So that will be the minimum delay you will get.

String sendData2(String command, const int timeout, boolean debug)
{
  char StartMarker = '{';
  char endMarker = '}';
  char c;

  String HTTP_Response = "";
  Serial.print("Command Sent: "); Serial.println(command);
  A9G.println(command);

  while (A9G.available() == 0) {}

  unsigned long time = millis();
  while (millis() - time < timeout)
  {
    delay(100);
    if (A9G.available())
    {
      while (A9G.available() > 0)
      {
        c = A9G.read();
        //        Serial.print(c);

        // Remove HTTP Headers & Recieve response only if { appears
        if ((c == '{') || (HTTP_Response.indexOf('{') == 0)) {
          HTTP_Response += c;
        }
      }
    }
  }
  if (debug)
  {
    Serial.println();
    Serial.print("A9G HTTP: ");
    Serial.println(HTTP_Response);
  }
  return HTTP_Response;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.