# Extracting lat and long coordinates from payload

**URL:** <https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787>\
**Category:** General\
**Created:** [12 November 2020 07:30 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787 "2020-11-12T07:30:56Z")\
**Posts on this page:** 20\
**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:** [12 November 2020 07:30 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/1 "2020-11-12T07:30:56Z")

</div>

Payload  
{"applicationID":"1","applicationName":"cloud"," **data**":[3,72,94,140,0,12,16,236,55,45,223,38,4,245,106,62],"devEUI":"20635f01d1000002","deviceName":"abeeway tracker","fCnt":588,"fPort":18,"rxInfo":[{"altitude":0,"latitude":0,"loRaSNR":9.8,"longitude":0,"mac":"24e124fffef0a78e","name":"24e124fffef0a78e","rssi":-59,"time":"2020-11-12T07:01:22.759152Z"}],"time":"2020-11-12T07:01:22.759152Z","txInfo":{"adr":true,"codeRate":"4/5","dataRate":{"bandwidth":125,"modulation":"LORA","spreadFactor":7},"frequency":865402500}}

```auto
[{"id":"506b83c1.56ee6c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"57373220.09238c","type":"mqtt in","z":"506b83c1.56ee6c","name":"","topic":"bachloo.rahul@gmail.com/test","qos":"0","datatype":"json","broker":"55bef158.86a7c","x":210,"y":80,"wires":[["8606fe2a.c2499"]]},{"id":"d5396d84.2231e","type":"debug","z":"506b83c1.56ee6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":520,"y":140,"wires":[]},{"id":"8606fe2a.c2499","type":"base64","z":"506b83c1.56ee6c","name":"","action":"","property":"payload.data","x":343,"y":167,"wires":[["d5396d84.2231e","449795b6.55bf6c"]]},{"id":"449795b6.55bf6c","type":"buffer-parser","z":"506b83c1.56ee6c","name":"","data":"payload.data","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":1,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":420,"y":260,"wires":[[]]},{"id":"55bef158.86a7c","type":"mqtt-broker","name":"","broker":"maqiatto.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe52a2d63cdc3541b5f249f45ed3a0bb26260ec0.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1ccd23b1bab25d36dc8a4f98ce39d9b97fde4a08.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e18141f7ef7053b3282fbea4e024b3c273a5c74f.png)  
How to use buffer parser node for parsing the data attribute?

---

<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:** [12 November 2020 09:08 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/2 "2020-11-12T09:08:09Z")

</div>

- Add the 2 entries in the buffer parser with offset set to match your data (I assume offset 6 for the Lat & and offset 9 for the Lon).
- Set these two items to Uint16BE or Uint16LE (depending on your data source)
- Set a mask of 0XFFF0 for both of them
- Add a function after the parser to do the MATH

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

---

<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:** [12 November 2020 09:13 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/3 "2020-11-12T09:13:57Z")

</div>

> [@Steve-Mcl](#):
>
> with offset set to match your data (I assume offset 6 for the Lat & and offset 9 for the Lon).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71d03c1d187f449ede6acb9780262060f7ba423d.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 November 2020 09:16 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/4 "2020-11-12T09:16:11Z")

</div>

Ii notice that the spec you provide show 16 bytes - but that you are receiving two packets - one of 16 bytes (OK) and one of 42 bytes... Are the first 16 bytes the same fields or something completely different ? You need to tell them apart before trying to decode them.

---

<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:** [12 November 2020 09:20 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/5 "2020-11-12T09:20:04Z")

</div>

I have this manual i think it should be given in manual [Abeeway\_Tracker\_Reference\_Guide\_FW2\_0.pdf](https://discourse.nodered.org/uploads/short-url/dZYqvUyaCPe11hHjHcvF0mo43aA.pdf) (1.5 MB)

---

<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:** [12 November 2020 09:27 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/6 "2020-11-12T09:27:16Z")

</div>

Close. Set the mask as described and add a debug node to see whats coming out of the parser.

Also, add a function after the parser and do the maths as described & a debug after the function node to see if your math is good.

> [@dceejay](#):
>
> but that you are receiving two packets - one of 16 bytes (OK) and one of 42 bytes

Good spot.

@bachloo.rahul As Dave said, which one of those 2 packets are the one containing your Lat/Lon data? You will need to identify which one of those 2 are providing the data you want otherwise it will fail 50% of the time. Looking at your screenshots, we can assume the 16 byte packet is the correct one.

---

<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:** [12 November 2020 09:56 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/7 "2020-11-12T09:56:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85ce6649db92d703afd562862d8219405f197e38.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24a418f7f1c5e10059e0bf40318d5b62c8fbd60f.png)  
check to see if code is correct in function node?

---

<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:** [12 November 2020 10:14 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/8 "2020-11-12T10:14:02Z")

</div>

> [@bachloo.rahul](#):
>
> check to see if code is correct in function node?

Does it work (It won't!)

As I said earlier in my screenshot, you **don't** need to do the bit shift because you took 4 bytes when asking for UINT32 & so the value was already shifted. I sometimes wonder why I bother to explain stuff.

```auto
Byte 6 7 8 9
Mask F F F 0

```

The value is already shifted to MSB

As for return - you cannot return values like that. This is node-red basics.

Change last part of function

```auto
msg.payload.latittude = lat;
msg.payload.longitude = lon;
return msg;
```

---

<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:** [12 November 2020 10:27 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/9 "2020-11-12T10:27:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/c/ec310fb2173d13ea843111579d94e07b5e09bdb0.png)  
There is some error

---

<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:** [12 November 2020 10:29 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/10 "2020-11-12T10:29:48Z")

</div>

Check your spelling. payload vs paylaod.

---

<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:** [12 November 2020 10:30 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/11 "2020-11-12T10:30:52Z")

</div>

yes 😅 very silly mistake

---

<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:** [12 November 2020 10:35 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/12 "2020-11-12T10:35:01Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c4fc55c9e05ca7154e523b5dba3e50b5c83fb1ee.png)  
it is returning the same values in debug window as both the if and else if conditions does not meet.

---

<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:** [12 November 2020 10:36 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/13 "2020-11-12T10:36:42Z")

</div>

Because your `/ 10000000` is inside the `if` and the value is \< 0x7fffffff

read the screenshot you first posted.

---

<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:** [12 November 2020 10:37 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/14 "2020-11-12T10:37:43Z")

</div>

yes that means the code in function is working perfectly fine as per the screenchot?

---

<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:** [12 November 2020 10:39 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/15 "2020-11-12T10:39:15Z")

</div>

> [@bachloo.rahul](#):
>
> yes that means the code is working perfectly fine?

no, obviously not!

Take the `/ 10000000` out of the `if`

If you posted actual function code instead of screenshots I might feel compelled to give you the fix, as it stands, I am not going to read from a screenshot & re-type your code.

---

<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:** [12 November 2020 10:40 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/16 "2020-11-12T10:40:50Z")

</div>

```auto
var lat=msg.payload.lat;
var lon=msg.payload.lon;

if (lat>0X7FFFFFF){
    lat-=0x100000000;
}
else if (lon>0x7FFFFFF){
    lon-=0x100000000;
}
msg.payload.latitude = lat;
msg.payload.longitude = lon;
return msg;

```

sorry was in a bit hurry ,i have removed both /1000000

---

<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:** [12 November 2020 10:42 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/17 "2020-11-12T10:42:00Z")

</div>

Hi, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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:** [12 November 2020 10:47 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/18 "2020-11-12T10:47:07Z")

</div>

> [@bachloo.rahul](#):
>
> i have removed both /1000000

That is not what I meant. I meant move it **outside** of the `if`

> [@Steve-Mcl](#):
>
> Because your `/ 10000000` is inside the `if` and the value is \< 0x7fffffff

This should be closer...

```auto
var lat = msg.payload.lat;
var lon = msg.payload.lon;

if (lat > 0x7FFFFFFF) { // 0x7FFFFFFF not 0x7FFFFFF
  lat -= 0x100000000;
}
// else << DONT DO IF ELSE HERE!
if (lon > 0x7FFFFFFF) { // 0x7FFFFFFF not 0x7FFFFFF
  lon -= 0x100000000;
}

lat /= 10000000; //outside of the IF
lon /= 10000000; //outside of the IF 

msg.payload.latitude = lat;
msg.payload.longitude = lon;
return msg;

```

---

<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:** [12 November 2020 10:53 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/19 "2020-11-12T10:53:36Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1a6f7a35adc89e986090eca2cecc6ab69a8fa998.png)  
this is the output but coordinates are not correct as i know it should be around 28 and 76.

---

<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:** [12 November 2020 10:55 UTC](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787/20 "2020-11-12T10:55:56Z")

</div>

3 things...

1. Re-read my code & correct yours.
2. Are you 100% certain the data going in to the parser has LAT at byte 6 and LON at byte 9?
3. Are you 100% certain the data going in to the parser is Big Endian (BE). What happens if you use LE types in the buffer parser.

EDIT...  
It might be you actually DO need to shift `<< 8` - give that a go too. Personally, I think your data might be byte swapped.

[Next page](https://discourse.nodered.org/t/extracting-lat-and-long-coordinates-from-payload/35787.md?page=2)
