TinyGPS + dtostrf() Weirdness

Hi All,

I have an Arduino Uno connected to a GPS module via UART and am using TinyGPS to read in tasty NMEA sentences. Everything works beautifully with the TinyGPS examples. I’m trying to take the latitude and longitude from TinyGPS, and use dtostrf() to convert them into String objects to pass along to a GSM radio.

The portion of my code relating to this is as follows:

Serial.println("Listening to GPS");
  gpsPort.listen(); //Listen to the GPS module instead of the GSM radio.
  //For two seconds, read GPS and grab key values

  bool newData = false;
  for (unsigned long start = millis(); millis() - start < 2000;)
    while (gpsPort.available())
      char c = gpsPort.read();
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
  wdt_reset(); //Watchdog timer is enabled, pet it.
  Serial.println("Done with GPS");
  cell.listen(); //Listen to the GSM radio instead of the GPS.
  //If valid GPS data was gathered go ahead and build the string
  if (newData)
    float flat, flon;
    char flatBuffer[20]; //buffer for dtostrf() to use
    char flonBuffer[20]; //buffer for dtostrf() to use
    unsigned long age;

  // Load Prefix : "AT+SSTRSEND=1,"
  // This buffer was setup previously as buffer[90]
  strcpy_P(buffer, (char*)pgm_read_word(&(at_table[15]))); 
  //Throw the buffer into a new string object so we can use operands
    String newString = buffer;

    gps.f_get_position(&flat, &flon, &age); 

    dtostrf(flat, 10, 6, &flatBuffer[20]); //Convert Latitude to a char array
    newString += flatBuffer; //Append the newString object with latitude char array
    Serial.print("Lat: ");
    Serial.println(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6); // this line is used by the TinyGPS example
    Serial.print("flatBuffer: ");
    newString += ",";

    dtostrf(flon, 10, 6, &flonBuffer[20]);
    newString += flonBuffer;
    Serial.print("Lon: ");
    Serial.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print("flonBuffer: ");
    Serial.print("flatBuffer: ");
    newString += ",";


And here is the response I receive

Listening to GPS
Done with GPS
Lat: 41.887939
Lon: -87.667449
flonBuffer: 41.887939

AT+SSTRSEND=1,“, 41.887939,24,35558504718154199,0,1,100,0”

Any help would be greatly appreciated.


Here's your error:

dtostrf(flat, 10, 6, &flatBuffer[20]);

Here you're pointing to the byte after the buffer, which is obviously wrong.

This has to be:

dtostrf(flat, 10, 6, flatBuffer);

Change that in every call to dtostrf().

While you're at it, get rid of the String class usage. It will bite you in the derriere until the (well documented) bug gets fixed. NOTHING that the String class does can not be done using char arrays.