7

new to arduino I'm struggling with what sounds like fairly n00b problem... I've wired up a adafruit GPS-board to my Arduino and it is working as it spits out GPS data to the serial port with Serial.print(GPS.latitude, DEC)

I Now want to concat a string which I can process (read: I want to sent it via an ethernet client.) This is what I got already:

......
String vnnt = "$VNNT,";

if (GPS.fix) {
   vnnt += "GPS,";

   //this works:
   vnnt.concat(GPS.fix);

   //but this not:
   vnnt.concat(GPS.latitude);

}else{
   vnnt += "INFO,Acquiring Sats";
}

Serial.println(vnnt);

The error message is: Call of overloaded 'concat(float&)' is ambiguous When I Serial.print(GPS.latitude, DEC) it results in: 4418.5937996050

So it is probably to big or something...

How can I concat the vars and create the long string?

stUrb
  • 351
  • 2
  • 5
  • 10

1 Answers1

7

The concat function does not implement a float version, but some of char, int, unsigneds...

unsigned char String::concat(long unsigned int)
unsigned char String::concat(unsigned int)
unsigned char String::concat(int)
...

so the compiler does not know how to cast (truncating probably the float) to integer, as there are several options.

You have to convert first your float to string, use dtostrf() or sprintf() then concat to your string.

char outstr[25];
sprintf(outstr, "%f", GPS.latitude);

or

dtostrf(GPS.latitude, 6, 2, outstr);  //check docs for 6, 2 values, and set them for your needs

And then:

vnnt.concat(outstr)

Also note that sprintf is very handy for compact creation of a (char) string:

char msg[80];
sprintf(msg, "$VNNT,GPS, %s %f", GPS.fix, GPS.latitude);
drodri
  • 1,434
  • 11
  • 12