Pages: [1]   Go Down
Author Topic: TinyGPS + dtostrf() Weirdness  (Read 902 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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: ");
    Serial.println(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.println(flonBuffer);
    Serial.print("flatBuffer: ");
    Serial.println(flatBuffer);
    newString += ",";

  }


And here is the response I receive



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

AT+SSTRSEND=1,", 41.887939,24,35558504718154199,0,1,100,0"

Any help would be greatly appreciated.

Thanks,
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's your error:

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

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

This has to be:

Code:
dtostrf(flat, 10, 6, flatBuffer);

Change that in every call to dtostrf().
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49236
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: