[SOLVED] Sending Lat and Long decimal issue


#1

I have been trying to figure out what I am doing wrong here. Below is my code and the issue is that in the Ubidots account the context for lat and lng is formatted wrong. The decimal is in the wrong place and the longitude seems to be missing the “-”. Any help or advice is appreciated I have have searched google for anything and tried various things with no luck.

Result in Ubidots context:

{“lat”:“4838.94433594”,“lng”:“11719.44531250”}

the result should be {“lat”:“48.3894433594”,“lng”:"-117.1944531250"}

My code:

double latitude = 0;
double longitude = 0;




AssetTracker t = AssetTracker();


void setup() {
    
    
    // Sets up all the necessary AssetTracker bits
    t.begin();
    
    // Enable the GPS module. Defaults to off to save power. 
    t.gpsOn();
 
}

void loop() {
    
    
    t.updateGPS();
    
    float lat = t.readLat();
    latitude = (double)lat;
    
    float lon = t.readLon();
    longitude = (double)lon;
    
    

    
    
  float speed = 168;

  /* Harcoded Coordinates */
  //float latitude = latitudeA;
  //float longitude = longitudeA;

  /* Reserves memory to store context keys values, add as much as you need */
  char* str_lat = (char *) malloc(sizeof(char) * 20);
  char* str_lng = (char *) malloc(sizeof(char) * 20);

  /* Saves as char the coordinates */
  sprintf(str_lat, "%f", latitude);
  sprintf(str_lng, "%f", longitude);

  /* Reserves memory to store context array */
  char* context = (char *) malloc(sizeof(char) * 30);

  /* Adds context key-value pairs */
  ubidots.addContext("lat", str_lat);
  ubidots.addContext("lng", str_lng);

  /* Builds the context with GPS coordinates to send to Ubidots */
  ubidots.getContext(context);

  /* Sends the position */
  ubidots.add("position", speed, context); // Change for your variable name
  
  bool bufferSent = false;
  bufferSent = ubidots.send(); // Will send data to a device label that matches the device Id

  if(bufferSent){
    // Do something if values were sent properly
    //Serial.println("Values sent by the device");
  }

  /* frees memory */
  free(str_lat);
  free(str_lng);
  free(context);
 
  delay(5000);
}

#2

So I figured out most of my issue. After looking through the asset tracker library some more I found other commands for getting lat and lon.

I was using:

float AssetTracker::readLat() {
    return gps.latitude;
}

float AssetTracker::readLon() {
    return gps.longitude;
}

And when using these I actually get the correct formatting: (Except for one small thing)

float AssetTracker::readLatDeg() {
    return gps.latitudeDegrees;
}

float AssetTracker::readLonDeg() {
    return gps.longitudeDegrees;
}

Using the readLatDeg() does put the decimals in the right spot however my longitude should be in the negative range however its not reporting as that.

Any ideas?


#3

Solved my own issue :slight_smile:
So the issue as you can see I solved in stages however the final issue with not getting the “-” in my longitude was just caused by not formatting the output properly. the following code fixed it.

/* Saves as char the coordinates */
sprintf(str_lat, “%.6f”, latitude);
sprintf(str_lng, “%.6f”, longitude);

adding “%.6f” solved the formatting problem.


#4

Thanks for sharing your solution, have a nice rest of your day