Payload decoding function Returns "json: unsupported value: NaN"

Hi, can someone help with this? I’m quite new on lorawan…

I have an NPK sensor Node which sends the payload to my Datacake webhook to get data displayed on a dashboard. But it returns this error “json: unsupported value: NaN”.

I’m using this function provided from the manufacturer/seller

function Decoder(port, bytes, variables) {

var paramLength = bytes[2]/2;

var params = new Object();

params[“N”] = bytes[23]*256+bytes[24];

params[“P”] = bytes[25]*256+bytes[26];

params[“K”] = bytes[27]*256+bytes[28];

params[“BAT”] = bytes[29]*256+bytes[30];

return params;

}

this Payload provided with the instructions

01031C00000000000000000000000000000000000000000017C020304FA0DDAC146

should return

01 7C N data 380 mg/kg
02 03 P data 515 mg/kg
04 FA K data 1274mg/kg
0D DA Battery Voltage 3.546V

Thanks in advance!

Hi Welcom,

So count the bytes and tell me what byte - 2, 23, 24, 25, 26, 27, 28, 29 and 30 is?

And you know the code is HEX you can manually decode.

This is more of an integration / javascript issue than a LoRaWAN issue I think.

NaN generally means “not a number”, this can be the result of a failed calculation, such as trying to divide by zero, taking the log of a negative number, etc. But perhaps also the case (depending on the programming language, I don’t know, not familiar with javascript) if you try to get a non-existing item from a map or dictionary. Perhaps the key is case-sensitive, so “N” is not the same as “n”.

Your example string looks like hexadecimal coded data, but its length is not a multiple of two, so something is wrong there. Normally you have two hex digits per byte.

I would double-check with the decoder provided by the manufacturer, get some known-good example data and try to send a simulated uplink. You can find that in the console under
Application / / End Devices / under Messaging

1 Like

I’d ask your manufacturer to fix it, particularly as

wasn’t valid for v2 and isn’t valid for v3 but as JS is too relaxed for it’s own good, will sort of work.

The creation of an object is a bit much as well. Plenty of examples in the docs to look at!

1 Like

Hi Johan,
this is the full list

Sensor address; 01

function code; 03

Data length; 1C

00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank 00 00 blank

01 7C N data 16mg/kg

02 03 P data160mg/kg

04 FA K data 1mg/kg

0D DA battery voltage

C1 46 verify

I suspect @Johan_Scheepers will be setting the next step for you to do rather than doing it for you, so you could turn the hex values in to decimal yourself.

But as mentioned by @bertrik, you’ve got a digit missing as the payload you quoted is 67 characters long

So the Battery Voltage could be A0DD

And the payload function needs updating.

And we don’t know where you saw this error message so it may be working OK but having a problem on Datacake.

Thank you all,

The first character was extra, now it works by usining this payload function

value=bytes[23]<<8 | bytes[24];
var N=value;	//Unit:mg/kg

value=bytes[25]<<8 | bytes[26];
var P=value;	//Unit:mg/kg

value=bytes[27]<<8 | bytes[28];
var K=value;	//Unit:mg/kg


var value=bytes[29]<<8 | bytes[30];
var batV=value/1000;//Battery,units:V

Which would make the data length 192 characters?

Hello Manuelstona, I’m new to this IOT and I’m having problems like you had. could you share your uplink and downlink code for your npk sensor? I don’t know how to program at all. thanks in advance. Nathan

The suppliers web site or their support channel will most probably be the best source of information.