I'm trying to build a string of characters from several elements given by the TinyGPS++ library with the use of sprintf().
This is my code making the string and printing it in the serial monitor.
char posmsg[100], lat[14+1], lon[14+1];
void sendString(){
//- convert lat/lon
dtostrf(gps.location.lat(), 10,6, lat);
dtostrf(gps.location.lng(), 10,6, lon);
//- sprintf the string
sprintf(posmsg, "$%4d-%02d-%02d %02d:%02d:%02d,%s,%s,%i,%i,%i,%i,%i$",
gps.date.year(),gps.date.month(),gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second(),
lat, lon,
gps.speed.value(), gps.course.value(), gps.altitude.value(),
gps.satellites.value(), gps.hdop.value()
);
Serial.println("--------------------");
Serial.println(posmsg);
Serial.println("--------------------");
Serial.print("Speed:");
Serial.println(gps.speed.value());
Serial.print("Course:");Serial.println(gps.course.value());
Serial.print("Height:");Serial.println(gps.altitude.value());
Serial.print("Sats:");Serial.println(gps.satellites.value());
Serial.print("HDOP:");Serial.println(gps.hdop.value());
Serial.println("--------------------");
}
This is the result and what I expect... (spaces added for readability)
Result:
--------------------
$2014-06-24 15:17:11, 51.xxxxxx, 5.xxxxxx, 2, 0, 4964, 0, 3660$
--------------------
Speed:2
Course:4964
Height:3660
Sats:8
HDOP:110
--------------------
Expected:
--------------------
$2014-06-24 15:17:11, 51.xxxxxx, 5.xxxxxx, 2, 4964, 3660, 8, 110$
--------------------
So in the end of the string the numbers get displaced...
According to the documentation the type of the variables are all integers or unsigned integers, but the sprintf messes things up.
gps.speed.value(); // Raw speed in 100ths of a knot (i32)
gps.course.value(); // Raw course in 100ths of a degree (i32)
gps.altitude.value(); // Raw altitude in centimeters (i32)
gps.satellites.value(); // Number of satellites in use (u32)
gps.hdop.value(); // Horizontal Dim. of Precision (100ths-i32)
What am I doing wrong?