Decoder with LMIC and Chirpstack

145 Views Asked by At

I'm having a problem with the decoder I'm using. Using my ESP32 I'm encoding my value with the following code:

float Energia;
int sensorValue = Energia *100;
Serial.println("Reading is:" );
Serial.println(sensorValue);
// int -> byte array
byte payload[2];
// Sensor
payload[0] = lowByte(sensorValue);
payload[1] = highByte(sensorValue); 

The code I'm using in the decoder in Chirpstack is the following:

function Decode(fPort, bytes, variables) {
  var decoded = {};
  // Check this is a message with a payload (fPort is not 0, which is used for status messages)
  if (fPort !== 0) {
    // Calculate the original moisture value from the two-byte payload
    decoded.Energia2 = bytes[0] + bytes[1] * 256;
    decoded.Energia2 = decoded.Energia2 /100.0;
  }
  return decoded;
}

This way to encode and decode works fine with small values, but when I try to send a big value, for example 1234567.89 in the variable Energia, the decoder returns a value that doesn't makes sense.

Thanks in advance for any suggestion!

1

There are 1 best solutions below

0
dda On

You're sending a two-byte value, which means the values you are sending have the following restrictions:

  • unsigned integer
  • max range 0 to 65535 or -32768 to 32767

So a value like 1234567.89 won't work because (a) it's not an integer and (b) it's bigger than 65535.