I am trying to build a decoder for Cayenne LPP but I am not successful. The longitude conversion is not correct, someone can help me where my mistake is.
Of course, Kersing is always right. But just FYI: for (possibly) negative numbers, you need to make 32 bits available to JavaScript, using sign extension.
So, untested, and I did not peek into the LPP documentation, I’d assume:
// To support negative values, sign extend to 32 bits by shifting the MSB
// 8 bits too far to the left, followed by sign-propagating right-shift:
decoded.latitude = (bytes[i++]<<24>>8 | bytes[i++]<<8 | bytes[i++])/10000;
decoded.longitude = (bytes[i++]<<24>>8 | bytes[i++]<<8 | bytes[i++])/10000;
Same goes for anything that can be negative, such as temperatures.
And if Cayenne has any 32 bits type that is not signed (unsigned, which cannot be negative) then you actually need to ensure JavaScript does not handle it as a “two’s complement number”. See uint32 in Abeeway Microtracker Decoder for one option to do that.
Also, one of the major selling points of LPP is its support for dynamic length payloads. So, you cannot use fixed array indexes, like bytes[6], in your while(i < bytes.length) loop. Instead, use the postfix-increment in bytes[i++]. (Even for fixed-position payloads, using i++ often makes the code less error prone anyway.)
Another selling point is that it allows for multiple readings of sensors of the same type, so you cannot use decoded.someName = ..., but need to add the “channel” number to the names, like, e.g., decoded['altitude_' + s_ch] = ....
But of course, you don’t need to worry about that when using TTN’s decoder.
Well, there are situations where you want to have control over what is happening or when you need some flexibility, or higher complexity, etc. I like the idea.
In those situations I would use/define a more flexible format and stay away from the limited set of data types offered by Cayenne LPP. I like the platform for fast prototyping but regularly struggle when it comes to finding a suitable data type for measurements.