Decodeng and converting payload


(Fmeylink) #1

Hi,

My aplication send as payload temp and moisture as hex.

the actual payload now is: 16 1C
16 is the temp 22 degrees 1C is the moisture 28%

How do i decode this 16 1C to te 22degrees and 28% ?
I read the decoding payload for dummys topic, that did not help much :frowning:


(Darren Millett) #2

Let javascript do the work for you:

decoded.Temperature = bytes[0];
decoded.Humidity = bytes[1];

Remember, a number is ultimately just a binary number stored inside the computer. It’s how that number is displayed that matters.
Payload data is displayed in Hex bytes, but if you decode it using Payload Functions, it gets stored (and displayed) as a json object, so usually numbers will be printed as decimal.

N.B. Negative numbers tend to cause problems (they are stored as 2’s complement), so you might want to modify you code to add and substract 100 to the temperature.
i.e. in the node: temp = temp + 100;
And in the payload decoding: decoded.Temperature = bytes[0] - 100;
That will give you a range from -100c to +155c, which is pretty safe for air temperature.


(Fmeylink) #3

i made this:

decoded.Temperature = bytes[0];
decoded.Humidity = bytes[1];

got this answer:

Internal error: Decoder threw error: ReferenceError("‘decoded’ is not defined")

something went wrong …


(Ud Lo Ra) #4

You must learn at least a minimum of programming and Javascript to develop an application… declare the variable first.


(Fmeylink) #5

ok ok
I saw the youtube about this and now it works.
Added another var that works but not al i like.

the value must be rounded to two decimals, how do i do that in java ?
expamples failed…

i use: math.round (bat/1000)
the result is a figure with no decimals.
the result = 4 in stead of 4.145
i want as a result 4.15


(Fmeylink) #6

got it :slight_smile:

bat: Math.round ((bat/1000.0) * 100.0) / 100.0 did the trick


(Jac Kersing) #7

How about:
Math.round(bat/10.0)/100.0
One less multiplication required.

BTW. Javascript is not Java. They share a couple of characters in the name, that is all they have in common.