Help me understand please AT response

Hello I use the following code to send AT command for reading GPSinfo.

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define PWR_Pin 6

SoftwareSerial SWserial(rxPin, txPin);

void setup()
{
  pinMode(PWR_Pin, OUTPUT);
  digitalWrite(PWR_Pin, HIGH);
  delay(500);
  digitalWrite(PWR_Pin, LOW);               
  delay(1500);

  Serial.begin(115200);
  Serial.println("4G Test");
  SWserial.begin(9600);
  delay(1000);
  while (SWserial.available()) {
    Serial.write(SWserial.read());
  }

void loop(){ 
SWserial.println("AT+CGPSPWR=1");
SWserial.println("AT+CGNSINF");
delay(10000);
SWserial.println("AT+CGPSPWR=0");
}

When i use this code response is:

+CGNSINF: 1,1,20220902134248.000,51.650288,3.999361

Which is fine if i only want coordinates.

When i use this code:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define PWR_Pin 6

SoftwareSerial SWserial(rxPin, txPin);

void setup()
{
  pinMode(PWR_Pin, OUTPUT);
  digitalWrite(PWR_Pin, HIGH);
  delay(500);
  digitalWrite(PWR_Pin, LOW);               
  delay(1500);

  Serial.begin(115200);
  Serial.println("4G Test");
  SWserial.begin(9600);
  delay(1000);
  while (SWserial.available()) {
    Serial.write(SWserial.read());
  }





void loop(){ 
SWserial.println("AT+CGPSPWR=1");
gsm_send_seria("AT+CGNSINF");
delay(10000);
SWserial.println("AT+CGPSPWR=0");
}


void gsm_send_serial(String command) {
  Serial.println("Send ->: " + command);
  SWserial.println(command);
  long wtimer = millis();
  while (wtimer + 3000 > millis()) {
    while (SWserial.available()) {
      Serial.write(SWserial.read());
    }
  }
  Serial.println();
}

The response is:

+CGNSINF: 1,1,20220902134248.000,51.650288,3.999361,1.500,0.00,189.4,1,,0.7,1.0,0.7,,12,8,,,38,,

i can not find out why this is?

Could someone explain?

Greetings.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Table 2-3: AT+CGNSINF return Parameters
Index Parameter Unit Range Length
1 GPS run status -- 0-1 1
2 Fix status -- 0-1 1
3 UTC date & Time yyyyMMddhh
mmss.sss
yyyy: [1980,2039]
MM : [1,12]
dd: [1,31]
hh: [0,23]
mm: [0,59]
ss.sss:[0.000,60.999]
18
4 Latitude ±dd.dddddd [-90.000000,90.000000] 10
5 Longitude ±ddd.dddddd [-180.000000,180.000000] 11
6 MSL Altitude meters 8
7 Speed Over Ground Km/hour [0,999.99] 6
8 Course Over Ground degrees [0,360.00] 6
9 Fix Mode -- 0,1,2[1] 1
10 Reserved1 0
11 HDOP -- [0,99.9] 4
12 PDOP -- [0,99.9] 4
13 VDOP -- [0,99.9] 4
14 Reserved2 0
15 GPS Satellites in View -- [0,99] 2
16 GNSS Satellites Used -- [0,99] 2
17 GLONASS Satellites in View -- [0,99] 2
18 Reserved3 0
19 C/N0 max dBHz [0,55] 2
20 HPA[2] meters [0,9999.9] 6 

Thank you for your response.

But i would like to know why i get differemt responses.

Using the normal SWserial and gsm_serial.

I dont understand the differance

Looks like they are the same for the first 51 characters.

+CGNSINF: 1,1,20220902134248.000,51.650288,3.999361
+CGNSINF: 1,1,20220902134248.000,51.650288,3.999361,1.500,0.00,189.4,1,,0.7,1.0,0.7,,12,8,,,38,,

The first sketch is incomplete, won't compile, and looks like it wouldn't produce any output.

The second version produces 96 characters but has similar coding problems.

My guess is that your input buffer is overflowing so characters get dropped. You need to read input earlier and more often.

1 Like

Yeah, you're waiting too long; the funny dropouts at the end are because your starting to extract the leading edge of the string while the trailing end is still coming in; you remove a char, the port adds one, drops the next N until you remove another, adds one, etc. etc. until done.

Drop that delay, and also figure out how to

  • write non-blocking receive code
  • process a buffer
  • watch for a flag character that says you've come to the end of a line(usually carriage return, line feed , or a combo of those).
    You might also make your receive buffer much longer, but that's going to promote laziness, instead of learning.

What line in the code of the first sketch produces this output? You do not write anything to the monitor after issuing the commands?

Both sketches have basic syntax errors and do not compile, so I suspect what you have posted is not what you have actually run.

In the 2nd sketch, this is not a great way to use millis() as it will not handle overflow correctly.

  while (wtimer + 3000 > millis()) 

better to use...
while(millis() - wtimer < 3000)

It looks like that is the response from whatever is connected to SWserial after sending a command

But they never check for a response...

void loop(){ 
SWserial.println("AT+CGPSPWR=1");
SWserial.println("AT+CGNSINF");
delay(10000);
SWserial.println("AT+CGPSPWR=0");
}

This is my code.. i know i it is not correct coding but i try for hobby

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3
#define PWR_Pin 6

SoftwareSerial SWserial(rxPin, txPin);

String apn = "public";
String apn_u = "";
String apn_p = "";
String Bat = ""; 
String Ip = "http://xxxxx: :xxx/?id=";
String GpsData = "";
String GpsData1= "";
String Long = "";
String Lat = "";
String id = "12345";
String Tijd = "";
String UTC = "";
String Snelheid = "";
String Hooghte = "";
String Sat = "";
String Richting = "";
//String postdata = "";
String Jaren = "20";
String Jaar = "";
String Maand = "";
String Dag = "";
String Uur = "";
String Min = "";
String Sec = "";
String TijdTotaal = "";
String MaandTotaal = "";
String ZendTijd = "";



void setup()
{
  pinMode(PWR_Pin, OUTPUT);
  digitalWrite(PWR_Pin, HIGH);
  delay(500);
  digitalWrite(PWR_Pin, LOW);               
  delay(1500);

  Serial.begin(115200);
  Serial.println("4G Test ");
  SWserial.begin(9600);
  delay(1000);
  while (SWserial.available()) {
    Serial.write(SWserial.read());
  }
 
  
  
}
  
 

void loop(){ 
gsm_send_serial("AT+CGPSPWR=1");
delay(2000);  
GpsTijd();
delay(1500);
GpsLong();
delay(2500);


String url = Ip + id + ZendTijd + "&lat=" + Long + "&lon=" + Lat;
String postdata = "";

 gsm_send_serial("AT+SAPBR=1,1");

  gsm_send_serial("AT+SAPBR=2,1");

 gsm_send_serial("AT+HTTPINIT");

  gsm_send_serial("AT+HTTPPARA=URL," + url);

  gsm_send_serial("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded");

  gsm_send_serial("AT+HTTPDATA=192,5000");

  gsm_send_serial(postdata);

  gsm_send_serial("AT+HTTPACTION=1");

  gsm_send_serial("AT+HTTPREAD");

  gsm_send_serial("AT+HTTPTERM");

  gsm_send_serial("AT+SAPBR=0,1");
//
//
gsm_send_serial("AT+CGPSPWR=0");
//Serial.println(url);
delay(10000);
  

}

void gsm_send_serial(String command) {
  Serial.println("Send ->: " + command);
  SWserial.println(command);
  long wtimer = millis();
  while (wtimer + 3000 > millis()) {
    while (SWserial.available()) {
      Serial.write(SWserial.read());
    }
  }
  Serial.println();
}


void GpsTijd() 
  { 
  SWserial.listen();
  String mystr = "";
  int i = 0;
  SWserial.println("AT+CCLK?");
  delay(500);
  while (SWserial.available() > 0) {
    mystr += char(SWserial.read());
  }
  Tijd = mystr;
  Jaar = Tijd.substring(19,27); //---juist
  Jaar.replace("/", "-");
  Jaar = "&timestamp=20" + Jaar;
//
  TijdTotaal = Tijd.substring(28,36); //---juist 
  //= TijdTotaal.substring(28,30);
  ZendTijd = Jaar + "T"  + TijdTotaal + "Z";    
  //Serial.println(TijdTotaal);
  delay(500);
}

void GpsLong()
{
  SWserial.listen();
  String mystr1 = "";
  int i = 0;
  gsm_send_serial("AT+CGNSINF");
  delay(500);
  while (SWserial.available() > 0) {
    mystr1 += char(SWserial.read());
    
  }
   GpsData = mystr1;
  //Serial.println(GpsData);
   Long = GpsData ;
   Long = Long.substring(46,53);
   Lat = GpsData.substring(56,62);
   //Snelheid = mystr1.substring(63,68);
   
   //Serial.println("long= "  + Long);
}

That utterly pointless delay() is the problem. The serial input buffer is probably overflowing.

delay() should be used ONLY where required, and those situations are rare.

That was a example. In full code below you see its different. And i use that to send to server and some delay that my database aint filling to much.

Please don't post misleading examples.

Avoid using Strings objects, especially on AVR-based Arduinos, as they cause memory problems and program crashes.

What you mean by string objects?

Building a string or??

Don't use...
String myString;

Do use...
char myString[30];

Ok thank you all for your awnsers.

I will study and learn more about what you all mentioned

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