Ubidots Community

[SOLVED] TheThingsNetwork GPS

I’m trying to implement an GPS from TTN and “Map” is not catching the Lat-Long value.
I’m using this decoder from TTN:

function Decoder(b, port) {
var lat = (b[0] | b[1]<<8 | b[2]<<16 | (b[2] & 0x80 ? 0xFF<<24 : 0)) / 10000 ;
var lng = (b[3] | b[4]<<8 | b[5]<<16 | (b[5] & 0x80 ? 0xFF<<24 : 0)) / 10000;

return {

location: {
lat: lat,
lng: lng
}
}
}

And decoded value is:

{
“location”: {
“lat”: 63.3968,
“lng”: 10.4017
}
}

But it’s shown in two different devices and not caught as a GPS coordinate.

Greetings, your device coordinates must be sent inside a variable’s context as specified at our REST API. Please reference an example below (numerical value is mandatory):

"location" : {
  "value": 1,
  "context": { "lat": 63.3968, "lng": 10.4017 }
}

All the best

I’m connecting the Tabs GPS Tracker to Ubidots and I’m facing issues with the GPS map.

The payload format I used:

function bits(value, lsb, msb) {
  var len = msb - lsb + 1;
  var mask = (1<<len) - 1;
  return value>>lsb & mask;
}

function Decoder(bytes, port) {
  var status = bits(bytes[0], 0, 7);
  var voltage = (25 + bits(bytes[1], 0, 3)) / 10;
  var bat_percentage = 100 * ((bits(bytes[1], 4, 7)) / 15);
  var temp = bits(bytes[2], 0, 7) - 32;
  var lat = ((((bytes[6] << 24) | bytes[5] << 16) | bytes[4] << 8) | bytes[3])/1000000;
  var lon = ((((bytes[10] << 24) | bytes[9] << 16) | bytes[8] << 8) | bytes[7])/1000000;
  var position = {"lat": lat, "lng": lon};

  var json={
    voltage:voltage,
    bat_percentage:bat_percentage,
    temp:temp,
    position:position,
    lat: lat,
    lon: lon
  }
  
    return json;
  }

This gives the following output in the console:

The output in Ubidots:

Somehow, the Latitude and Longitude are not recognized.

Any ideas?

Hey @laurens :wave:t2:,

I’m so happy to see you around the community, welcome. :smiley:

The payload being sent does not manage the correct format. If you refer to the Ubidots API documentation, you will notice that the payload must contain the following format:

{"position": {"value" : 1, "context":{"lat":-6.2, "lng":75.4}}}

In your case, the payload should be:

  var json={
    voltage:voltage,
    bat_percentage:bat_percentage,
    temp:temp,
    position:{"value":1, "context": position},
    lat: lat,
    lon: lon
  }

Please try again, and let me know if everything is working as it should.

Cheers,
Maria H.

1 Like

Thanks Maria, that works like a charm.

1 Like

You’re always welcome :smiley: