"AT+location=2" in a9g module to get lat and lng. I can't

49 Views Asked by At

I'm trying to get coordinates from board a9g with at+location=2 Tiny library sometimes work sometimes not. The function to get coordinates as string is:

String sendData(String command, const int timeout, boolean debug)
{
  String response = "";
  mySerial.println(command);
  long int time = millis();
  while ((time + timeout) > millis())
  {
    while (mySerial.available()){
      char c = mySerial.read();
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

to retrieve the coordinates

coordinates =sendData("AT+LOCATION=2", 1000, DEBUG);
int index = coordinates.indexOf(',');
lat = coordinates.substring(17, index);
lng = coordinates.substring(index + 1);


printing:

lat = 42.850
lng = 2.8500

but when I send to php

"AT+HTTPGET=\"http://carles.ho.submit_data01.php?data1=" + String(lat) +"&data2=" + String(lng) + "&data3=" + mac + "&data4=" + String(flag) + "\"";

it does'nt work. The documentation for at+location is this:

AT+LOCATION=1(return:,OK) xx.xxxxx, xxx.xxxxxx (fixed to 6 digits after the decimal point) OK Return latitude and longitude directly without conversion

I've tried differents options but I can't. Thanks in advance.

I would like to know what kind of response return and how to solve.

1

There are 1 best solutions below

0
Carles Casademunt On

Improving accuracy in lng:

lat = coordinates.substring(17, index);
lng = coordinates.substring(index + 1, index + 10);

and cleaning results avoiding undesirable characters:

tring sanitizeString(String input) {
String result = "";
for (int i = 0; i < input.length(); i++) {
    char c = input.charAt(i);
    if (isAlphaNumeric(c) || c == '.' || c == ',' || c == '-') {
        result += c;
    }
}
return result;

}