String, cha , long.

Hi

I need to generate a message in the required *Char Format so that I can send it by SMS
The message shall concatenate some text and some long data retrieved from the GPS device.
Unfortunately I am lost in String, Char *Char formats and their use.

The final Char message shall be sth like this :

"The coordinates of DC1234 are: Latitude=49.355N Longuitude=6.400E"

Please focus on: void GenerateSMS ()

String strFirstText = "The coordinates of " ; // this text is constant
String strCarID ; // This text is variable
String strSecondText = " are : Latitude= " ; // this text is constant
String strThirdText = "N Longuitude= " ; // this text is constant
String strForthText = "E" ; // this text is constant

long lonLatCoordinates  ; // in the final code this data comes from a GPS device
long lonLongCoordinates  ; // in the final code this data comes from a GPS device

String strSMSmessage ;
char chaSMSmessage ; 

void setup() 
  {
  Serial.begin (9600); 
  }

void loop() 
  {
  strCarID = "DC1234";
  ReadGPS ();
  GenerateSMS () ; 
  SendSMS (); 
  }

void SendSMS ()
  {
  // Will be included in the final code and send the message in the required Char format.
  }

void ReadGPS () // Will be included in the final code and retrieve the Coordinates from GPS in long
  {
    lonLatCoordinates = 49.355 ;
    lonLongCoordinates = 6.400 ;
  }

void GenerateSMS ()
  {    
  strSMSmessage = strFirstText + strCarID + strSecondText + lonLatCoordinates + strThirdText + lonLongCoordinates + strForthText ; 
  chaSMSmessage = cha(strSMSmessage);

  Serial.println (strSMSmessage) ; 
  Serial.println (chaSMSmessage) ; 
  }

Unless you have a library that requires them, forget about the String class, and even then only use them when you absolutely have to.

  • you defined your lat and long as "long" variable types. they need to be float

sprintf() doesn't support the "%f" format.

   int latInt   = lonLatCoordinates;
    int latFrac  = (lonLatCoordinates - latInt) * 1000;

    int longInt   = lonLongCoordinates;
    int longFrac  = (lonLongCoordinates - longInt) * 1000;

    char s [80];
    sprintf (s, " Lat %3d.%03d, Long %3d.%03d",
        latInt, latFrac, longInt, longFrac);
    Serial.println (s) ;