# Gps tracker payload decoding for gps latitude and longitude

**URL:** <https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554>\
**Category:** General\
**Created:** [22 January 2021 10:30 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554 "2021-01-22T10:30:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)\
**Post date:** [22 January 2021 10:30 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554/1 "2021-01-22T10:30:08Z")

</div>

Hello Everyone ,basically want help in decoding payload to get gps information like latitude and longitude.

> **[LW004-PB%20User%20Manual%20V1.1.pdf](https://www.dropbox.com/s/8xrodwwtm05u5ks/LW004-PB%20User%20Manual%20V1.1.pdf?dl=0)**

payload

```auto
{"applicationID":"1","applicationName":"cloud","data":"YgAlYSgAHbo2AGR8QFocrwASACYD4gAC","devEUI":"f06a5fffff0f549e","deviceName":"lw004pb-1","fCnt":18,"fPort":222,"rxInfo":[{"altitude":0,"latitude":0,"loRaSNR":11.5,"longitude":0,"mac":"24e124fffef0a78e","name":"24e124fffef0a78e","rssi":-63,"time":"2021-01-22T10:28:43.328275Z"}],"time":"2021-01-22T10:28:43.328275Z","txInfo":{"adr":false,"codeRate":"4/5","dataRate":{"bandwidth":125,"modulation":"LORA","spreadFactor":12},"frequency":865985000}}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b33cd100267d46a23ea52ed5dafefdf9d5b4884b.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/0037972896328e00aa8c276ac87bf2d6a72f048b.png)  
I have used buffer parser acc to manual to extract latitude and longitude check if its correct.Now acc to it is in little endian format means least significant byte is stored in lower memory address.after this need some guidance like simply converting hexadecimal to decimal would work.

```auto
    char hex[17];
    int decimal, place;
    int i = 0, val, len;

    decimal = 0;
    place = 1;

    /* Find the length of total number of hex digit */
    len = strlen(hex);
    len--;

    for(i=0; hex[i]!='\0'; i++)
    {
 
        /* Find the decimal representation of hex[i] */
        if(hex[i]>='0' && hex[i]<='9')
        {
            val = hex[i] - 48;
        }
        else if(hex[i]>='a' && hex[i]<='f')
        {
            val = hex[i] - 97 + 10;
        }
        else if(hex[i]>='A' && hex[i]<='F')
        {
            val = hex[i] - 65 + 10;
        }

        decimal += val * pow(16, len);
        len--;
    }

console.log(decimal);

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2021 13:59 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554/2 "2021-01-22T13:59:30Z")

</div>

I believe that node-red-contrib-buffer-parser is designed for exactly these problems.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 January 2021 17:06 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554/3 "2021-01-22T17:06:44Z")

</div>

Pretty sure I've been here before?

Anyhoo...

Using the sample data from the manual...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/12082dd501a263295a5cb046f4160af31e633a2a.png)

Function...

```auto
msg.payload.lat = msg.payload.lat_dec * 90 / 8388607
msg.payload.lon = msg.payload.lon_dec * 180 / 8388607
return msg;

```

Result...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/3/b3b4de4b5116952b32596c39c2dfd8293e0254bd.png)

Demo flow...

```auto
[{"id":"67cdb95c.be7d88","type":"inject","z":"a2d1264d.2189b8","name":"example data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2F0179512B0077665100D9194D750B3300D0006C03A2000E","payloadType":"str","x":1220,"y":780,"wires":[["db29b1f3.c93e6"]]},{"id":"db29b1f3.c93e6","type":"buffer-parser","z":"a2d1264d.2189b8","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint8","name":"battery","offset":0,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint8","name":"alarmStatus","offset":1,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"lat_dec","offset":2,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"int32le","name":"lon_dec","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"nearestBeaconMAC","offset":10,"length":6,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1400,"y":780,"wires":[["1a005c2a.a874f4"]]},{"id":"c2db851a.fd8fd8","type":"debug","z":"a2d1264d.2189b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1430,"y":840,"wires":[]},{"id":"1a005c2a.a874f4","type":"function","z":"a2d1264d.2189b8","name":"calc coords","func":"msg.payload.lat = msg.payload.lat_dec * 90 / 8388607\nmsg.payload.lon = msg.payload.lon_dec * 180 / 8388607\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1250,"y":840,"wires":[["c2db851a.fd8fd8"]]}]

```

---

<div class="post-metadata">

**Author:** ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)\
**Post date:** [22 January 2021 17:42 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554/4 "2021-01-22T17:42:40Z")

</div>

Thanks a lot steve , 😇 😍 without you it will be difficult for me to decode.Good javascripting

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 February 2021 17:42 UTC](https://discourse.nodered.org/t/gps-tracker-payload-decoding-for-gps-latitude-and-longitude/39554/5 "2021-02-05T17:42:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
