# Encoding/decoding problem with GPS coordinates

**URL:** <https://www.thethingsnetwork.org/forum/t/encoding-decoding-problem-with-gps-coordinates/56965>\
**Category:** Application Development\
**Created:** [May 11, 2022, 7:41am UTC](https://www.thethingsnetwork.org/forum/t/encoding-decoding-problem-with-gps-coordinates/56965 "2022-05-11T07:41:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mific78](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/mific78/32/35697_2.png) [@Mific78](https://www.thethingsnetwork.org/forum/u/Mific78)\
**Post date:** [May 11, 2022, 7:41am UTC](https://www.thethingsnetwork.org/forum/t/encoding-decoding-problem-with-gps-coordinates/56965/1 "2022-05-11T07:41:47Z")

</div>

Hello,

I want to send to TTN the GPS coordinates provided by an Arduino Mega with the Dragino GPS/LORA shield.

In order to encode the GPS coordinates, I use the [GitHub - thesolarnomad/lora-serialization: LoraWAN serialization/deserialization library for The Things Network](https://github.com/thesolarnomad/lora-serialization) library =\>

```auto
[...]
byte buffer[8];
LoraEncoder encoder(buffer);
[...]
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
}
else
{
if (checkGpsFix()) {
// Prepare upstream data transmission at the next possible time.
buildPacket(txBuffer);
encoder.writeLatLng(gps.location.lat(), gps.location.lng());
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);
Serial.println(F("Packet queued"));
[...]

```

The code of `encoder.writeLatLng` is the following (I took a look at the code of the library) =\>

```auto
void LoraEncoder::writeLatLng(double latitude, double longitude) {
    int32_t lat = latitude * 1e6;
    int32_t lng = longitude * 1e6;

    _intToBytes(_buffer, lat, 4);
    _intToBytes(_buffer + 4, lng, 4);
    _buffer += 8;
}

```

The problem is as follows :

On the Arduino side, the Lat, Long (as provided by the TinyGPS++ library - functions gps.location.lat() and gps.location.lng()) are =\>

```auto
Lat=48.576763 Long=1.941792

```

On the TTN side, the Lat, Long are =\>

```auto
"decoded_payload": {
  "decoded": [
    48.576628,
    1.941793
   ]

```

The decoder is as follows =\>

```auto
// js code copied from https://github.com/thesolarnomad/lora-serialization/blob/master/src/decoder.js

function decodeUplink(input) {
var decoded = {};
decoded = latLng(input.bytes.slice(0, 8))

return {
  data: {decoded},
  warnings: [],
  errors: []
  }
}

```

So why such a difference between the latitude from the Arduino side and from the TTN side ? The serialization/deserialization of the coordinates (latitude/longitude) is however realized with a precision of 6 decimals (see code above) ?
