Parsing pressure with 2 decimal precision

Hi Everyone,

After having read Decrypting messages for dummies
and played with https://github.com/TheThingsNetwork/workshops/tree/master/The%20Things%20Network

I decided to try on my node sending info from my bme280, so my do_send function look like this

void do_send(osjob_t* j) {
  bme.takeForcedMeasurement();
  delay(10);

  int16_t celciusInt = round(bme.readTemperature() * 100);
  int16_t humInt = round(bme.readHumidity() * 100);
  int16_t pressInt = (bme.readPressure()/100.0F);

  Serial.println(bme.readPressure()/100.0F);

  byte payload[6];
  payload[0] = highByte(celciusInt);
  payload[1] = lowByte(celciusInt);
  payload[2] = highByte(humInt);
  payload[3] = lowByte(humInt);
  payload[4] = highByte(pressInt);
  payload[5] = lowByte(pressInt);

  if (LMIC.opmode & OP_TXRXPEND) {
  } else {
    LMIC_setTxData2(1, payload, sizeof(payload), 0);
  }
}

And my Decoder function , like this:

function Decoder(bytes, port) {
  var decoded = {};

  var celciusInt = (bytes[0] << 24 >> 16) | bytes[1];
  var humidityInt = (bytes[2] << 24 >> 16) | bytes[3];
  var pressureInt = (bytes[4] << 24 >> 16) | bytes[5];

  // Decode integer to float
  decoded.temperature = celciusInt / 100;
  decoded.humidity = humidityInt / 100;
  decoded.pressure = pressureInt;

  return decoded;
}

But despite that in serial, i got something like 981.45 for example, on the ttn website, i only 981, could anyone help me understand what i did wrong either on the arduino code or on the decoder javascript, either both ^^. I really would like to have 2 decimal precision.

I also tried cayenneLpp library on arduino side and i got only 1 decimal precision, which is not enough for me. (using this javascript decoder https://gist.github.com/iPAS/e24970a91463a4a8177f9806d1ef14b8 )

Thanks a lot
Rgds

Leen

You can’t have the pressure with 2 decimals in 16 bits…

Do you really think these sensors have a 2 decimal precision? :thinking:

Either use 3 bytes or offset the pressure by e.g. 800hPa to get it back in a 16 bits range…

Don’t divide it with 100 on your node. Leave it in Pa, and then in TTN console, in decoder divide it with 100. You will need 3 bytes for that because it’s value will be higher than 65536. But value will not be lower than 700hPa (70 000Pa). So you can on node subtract with 70 000 and add that in decoder so you will be able to get that in 2 bytes and with decimal precision. :smiley:

Ignoring external environment factors, have you taken into account the difference between resolution, accuracy and repeat-ability?

If so, you would realize that 2 digits of precision is meaningless for this sensor. You may as well use your favorite numbers instead, it will be just as accurate and just as useful.

3 Likes

Thanks you all for your help, each and every responses were valuable. I managed to get my 2 decimal precision.

Regarding the resolution , i definitely agree that its pointless, just that i had 2 decimal using my ESP32s and it was making smooth and nice graphana dashboard, without 2 decimal, its just simply ugly to look at :wink: