the-real-vutility / payload-codecs

2 stars 0 forks source link

Some help with Chirpstack v3 #6

Closed jjanderson closed 9 months ago

jjanderson commented 11 months ago

I am using the following modified codec in chirpstack v3 but keep getting the error message

0:"Payload length must be 11 bytes"

could someone help point me in the right direction?

function Decode(fPort, bytes, variables) {
  var result = {
    data: {},
    errors: [],
    warnings: [],
  };
  var rawBytesArray = bytes;
  // Uplink payload must be 11 bytes long.
  if (rawBytesArray.length != 11) {
    result.errors.push("Payload length must be 11 bytes");
    delete result.data;
    return result;
  }

  // Packet ID - 1 byte
  var packetId = rawBytesArray[0];
  if (packetId !== 50) {
    result.errors.push("Payload packet ID is not equal to 50");
    delete result.data;
    return result;
  }

  // Constant factors for formulas
  var capacitorVoltageFactor = 5.0 / 255.0;
  var temperatureCelsiusFactor = 120.0 / 255.0;
  var deciToUnitFactor = 0.1;

  // Amp hour accumulation - 4 bytes
  // 32-bit unsigned integer in network byte order (MSB/BE) reported in deci-ampere-hour (dAh)
  var ampHourAccumulationDeciAmpere =
    ((rawBytesArray[1] << 24) +
      (rawBytesArray[2] << 16) +
      (rawBytesArray[3] << 8) +
      rawBytesArray[4]) >>>
    0;

  // Average amps - 2 bytes
  // 16-bit unsigned integer in network byte order (MSB/BE) reported in deci-ampere (dA),
  // this average represents the entire time since the last transmit (one entire transmit period)
  var averageAmpsDeciAmpere =
    ((rawBytesArray[5] << 8) + rawBytesArray[6]) >>> 0;

  // Max Offset - 1 byte
  // 8-bit unsigned integer representing the percent offset above the Average amps value.
  var maxOffset = rawBytesArray[7];

  // Min Offset - 1 byte
  // 8-bit unsigned integer representing the percent offset below the Average amps value.
  var minOffset = rawBytesArray[8];

  // Capacitor Voltage Scalar - 1 byte
  // 8-bit unsigned integer representing the capacitor voltage.
  // (as if the integer range from 0-255 is scaled to between 0.0V and 5.0V)
  var capacitorVoltageScalar = rawBytesArray[9];

  // Temperature Scalar
  // 8-bit unsigned integer representing the temperature.
  // (as if the integer range from 0-255 is scaled to between -40C and 80C)
  var temperatureScalar = rawBytesArray[10];

  // Calculated fields
  var maximumAmpsDeciAmpere =
    averageAmpsDeciAmpere * ((100 + maxOffset) / 100.0);
  var minimumAmpsDeciAmpere =
    averageAmpsDeciAmpere * ((100 - minOffset) / 100.0);
  var capacitorVoltage = capacitorVoltageFactor * capacitorVoltageScalar;
  var temperatureCelsius = temperatureCelsiusFactor * temperatureScalar - 40;

  if (minimumAmpsDeciAmpere < 0) {
    result.warnings.push("Minimum amps is less than 0.");
  }
  if (capacitorVoltage < 3.4) {
    result.warnings.push("Low capacitor voltage may reduce transmit interval.");
  }

  result.data = {
    ampHourAccumulation: ampHourAccumulationDeciAmpere * deciToUnitFactor,
    averageAmps: averageAmpsDeciAmpere * deciToUnitFactor,
    maximumAmps: maximumAmpsDeciAmpere * deciToUnitFactor,
    minimumAmps: minimumAmpsDeciAmpere * deciToUnitFactor,
    capacitorVoltage: capacitorVoltage,
    temperatureCelsius: temperatureCelsius,
  };

  return decoded;
}
CarterCall1 commented 10 months ago

I need a little more info: