sendATcommand response -1 ESP32-SIM7600X 4G HAT

I have connected SIM7600X 4G HAT to ESP32.

I am using Adafruit_FONA.h library.

I am using HardwareSerial *fonaSerial = &Serial2;

fona.begin(*fonaSerial) detects correctly the SIM7600X 4G HAT.

But later when I send AT commans something wrong.

Por instance:

sendATcommand("AT+CREG?","OK",2000);

Not response.

If I print the serial reading inside the sendATcommand function like this:

Serial.println(ATcommand); Serial.println(fonaSerial->available()); Serial.println(fonaSerial->read());

The responses is like this:

AT+CREG? 0 -1 AT+CGSOCKCONT=1,"IP","NXT17C.NET" 0 -1 AT+CSOCKSETPN=1 0 -1 AT+CGSOCKCONT? 0 -1 AT+CSOCKAUTH=1,0 0 -1 AT+CIPMODE=0 0 -1 AT+NETOPEN 0 -1

I don't understand what is wrong.

I need some help.

Upload the complete sketch, or at least the sendATcommand function. Do you send the AT command to the device?

This is the code.

#include <Adafruit_FONA.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char apn[]  = "NXT17C.NET";
const char gprsUser[] = "";
const char gprsPass[] = "";

#define FONA_RST 4
#define FONA_RX 16
#define FONA_TX 17
#define GSM_PWR 5



float Lat = 0;
float Log = 0;

// this is a large buffer for replies
char replybuffer[255];

int respuesta;
char aux_str[100];



HardwareSerial *fonaSerial = &Serial2;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

uint8_t type;
String payload_pattern ="{'id':'MyESP','Latitud':$Latitud$,'Longitud':$Longitud$,'Cisterna':$Cisterna$}";

void setup() {

  while (!Serial);

  Serial.begin(115200);
  Serial.println(F("FONA basic test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));
  delay(1000);

  fonaSerial->begin(115200);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    ESP.restart();
    while (1);
  }
  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  switch (type) {
    case FONA800L:
      Serial.println(F("FONA 800L")); break;
    case FONA800H:
      Serial.println(F("FONA 800H")); break;
    case FONA808_V1:
      Serial.println(F("FONA 808 (v1)")); break;
    case FONA808_V2:
      Serial.println(F("FONA 808 (v2)")); break;
    case FONA3G_A:
      Serial.println(F("FONA 3G (American)")); break;
    case FONA3G_E:
      Serial.println(F("FONA 3G (European)")); break;
    default:
      Serial.println(F("???")); break;
  }
   

   Connect4G();


}

void loop()
{

  PeticionHttp();
  delay(5000);

}


uint8_t sendATcommand(const char* ATcommand, const char* expected_answer, unsigned int timeout)
{
  uint8_t x = 0, answer = 0;
  char response[100];
  unsigned long previous;

  memset(response, '\0', 100); // Initialize the string

  delay(100);

  while (fonaSerial->available() > 0) { // Clean the input buffer
    fonaSerial->read();
  }
 
  fonaSerial->println(ATcommand);    // Send the AT command

  Serial.println(ATcommand);
  Serial.println(fonaSerial->available()); 
  Serial.println(fonaSerial->read()); 

  x = 0;
  previous = millis();

  // This loop waits for the answer
  do {
    if (fonaSerial->available() != 0) {
      // if there are data in the UART input buffer, reads it and checks for the answer
      response[x] = fonaSerial->read();
      Serial.print(response[x]);
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer) != NULL) {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));

  // fonaSerial->print("\n");
  return answer;
}



void PeticionHttp()
{

   float Latitud = 23.456;
   float Longitud = 80.21;
   int Cisterna = 2;
   String payload = payload_pattern;
   payload.replace("$Latitud$",String(Latitud));
   payload.replace("$Longitud$",String(Longitud));
   payload.replace("$Cisterna$",String(Cisterna));

   StaticJsonDocument<100> root;
   //JsonObject root = doc.to<JsonObject>();
   root = payload;
   char json_str[256];
   serializeJson(root,json_str);

   Serial.println(json_str); 
   
   HTTPClient http;   
   http.begin("http://api.cultilogic.com/testing.php");  //Specify destination for HTTP request
   http.addHeader("Content-Type", "application/json");             //Specify content-type header
   int httpResponseCode = http.POST(json_str);
  
   if(httpResponseCode>0)
   {
 
    String response = http.getString();                       //Get the response to the request
 
    Serial.println(httpResponseCode);   //Print return code
    Serial.println(response);           //Print request answer
 
   }else
   {
 
    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
 
   }
 
   http.end();  //Free resources

   /*
   Serial.println("Enviem HHTTP...");
   sendATcommand("AT+CHTTPSSTART","OK",1000);
   sendATcommand("AT+CHTTPACT=api.cultilogic.com,80","+CHTTPACT: REQUEST",6000);
   sendATcommand("GET /testing.php HTTP/1.1 Host: api.cultilogic.com Content-Length: 0","OK",1000);
   aux_str[0] = 0x1A;
   aux_str[1] = 0x00;
   respuesta = sendATcommand(aux_str, "+CHTTPACT: DATA,", 30000);

   sendATcommand("AT+CHTTPSCLSE","OK",1000);
   sendATcommand("AT+CHTTPSSTOP","OK",1000); 
   */
}


void Connect4G()
{
   Serial.println("Connectant 4G...");
   sendATcommand("AT+CREG?","OK",2000);
   Serial.println("Configurant APN...");
   sendATcommand("AT+CGSOCKCONT=1,\"IP\",\"NXT17C.NET\"","OK",2000);
   sendATcommand("AT+CSOCKSETPN=1","OK",2000);
   sendATcommand("AT+CGSOCKCONT?","OK",2000);
   sendATcommand("AT+CSOCKAUTH=1,0","OK",2000);
   sendATcommand("AT+CIPMODE=0","OK",2000);
   sendATcommand("AT+NETOPEN","OK",2000);
   Serial.println("IP...");
   sendATcommand("AT+IPADDR","OK",2000);

}

Maybe try

fonaSerial->print(ATCommand);
fonaSerial->print('\r');

According to the SIM7600 command manual, it expects a after each command. Println sends \r\n, maybe the newline character confuses the device?