[SOLVED] Cant get GPS coordinates in a HTTPpoststring

Hello,

Im not that familiar whit HTTP request and sending GPS coordinates.
Im using an arduino whit GSM shield V2 (and a GPS shield)
Im trying to send GPS coordinates to ubidots.
I want to make http request.
I can send the longitude and latidtude indivudiualy to ubidots but I cant get them togheter in 1 request.
Based of another users post I found out that &context=lat=%s,lng=%s has to be included.
Now im trying to use that sentence in my payload. I cant seem to get it to work.
Does anyone know a solution?
Here is a part of my code to get an idea what im trying to do.

// Important: Avoid to send a very long char as it is very memory space costly, send small char arrays

  sprintf(payload, "{\"");
  sprintf(payload, "&context=lat=%s,lng=%s", payload);
  sprintf(payload, "%s%s\":%", payload, VARIABLE_LABEL_1,str_val_1,str_val_2);
  sprintf(payload, "%s}", payload);
  Serial.println(payload);
  SendToUbidots(payload);

After the payload has been constucted im using this code to send it:

PayloadLenght = strlen(payload);  //Determin the payload lenght (Needed to send data to a server)
 /* Builds the request POST - Please reference this link to know all the request's structures https://ubidots.com/docs/api/ */
  sprintf(HTTPPostString, "POST /api/v1.6/devices/%s/?force=true HTTP/1.1\r\n", DEVICE_LABEL);
  sprintf(HTTPPostString, "%sHost: things.ubidots.com\r\n", HTTPPostString);
  sprintf(HTTPPostString, "%sUser-Agent: %s/%s\r\n", HTTPPostString, USER_AGENT, VERSION);
  sprintf(HTTPPostString, "%sX-Auth-Token: %s\r\n", HTTPPostString, TOKEN);
  sprintf(HTTPPostString, "%sConnection: close\r\n", HTTPPostString);
  sprintf(HTTPPostString, "%sContent-Type: application/json\r\n", HTTPPostString);
  sprintf(HTTPPostString, "%sContent-Length: %d\r\n\r\n", HTTPPostString, PayloadLenght);
  sprintf(HTTPPostString, "%s%s\r\n", HTTPPostString, payload);

Greetings,

Robert

Hello,

I found out how to do it.
On the following website I have found my solution:

I have made this code to consturct the value (if somone else is struggeling whit the same issue as I have):

sprintf(payload, "%s", "");                                                                                         //Cleans the content of the payload
    sprintf(payload, "{\"");
    sprintf(payload, "%s%s\":", payload, GPS_VARIABLE_LABEL);
    sprintf(payload, "%s%s\":%s", payload, "{\"value", Time_Value);
    sprintf(payload, "%s,\"%s\":%s", payload, "context\":{\"lat" , Latitude_Value);
    sprintf(payload,  "%s,\"%s\":%s", payload, "lng", Longitude_Value);
    sprintf(payload, "%s}}}", payload);
    Serial.println(payload);                                                                                          //After payload has been constructed call the post to ubidots void
    SendToUbidots(payload);

NOTE: DO NOT make the sprintf code longer, If you do this and ‘‘overload’’ the command the arduino may crash and restart!
Example:

sprintf(payload, “{"”);
sprintf(payload, “%s%s":%s,"%s":%s,"%s":%s%s”, payload, VARIABLE_LABEL_1, str_val_1, “context":{"lat” , str_val_2, “lng”, str_val_3);
//sprintf(payload, “%s,"%s":%s "%s":%s”, payload, “context":{"lat” , str_val_2, “lng”, str_val_3);
sprintf(payload, “%s}}”, payload);
Serial.println(payload);

This code will generate the same payload but it may crash te Arduino

Now it is working fine :smiley:

Greetings,

Robert

Hello @R_Bouw,

Thanks for share it with the community! :smiley:

Regards,
Maria C.

Thanks, this way we can make the community better :smiley:

kind regards,

Robert