BME280 Pressure, LMIC

I’m really struggling with getting a BME280 pressure reading working with TTN…
I have this code (borrowed from here:JackGruber/ - esp32_ttn_environmental_sensor) generating the payload:

        //Battery Voltage 
        tmp_u8 = (ReadVBat(REF_VBAT, REF_VCCRAW_ESP32) / 10) - 200;
        mydata[0] = tmp_u8;
    
        // Temperature
        /**************************************************************************/
        tmp_u16 = (BME280ReadTemperature() * 10);
        mydata[1] = tmp_u16 >> 8;
        mydata[2] = tmp_u16 & 0xFF;

        // Humidity
        /**************************************************************************/
        tmp_float = BME280ReadHumidity();
        tmp_u8 = tmp_float;
        mydata[3] = tmp_u8;
        // Bit 4 for decimal 1 = 0.5
        if ((tmp_float - tmp_u8) > 0.251 && (tmp_float - tmp_u8) < 0.751)
        {
            mydata[3] |= (1 << 7);
        }
        else if ((tmp_float - tmp_u8) > 0.751)
        {
            mydata[3] = mydata[3] + 1;
        }

        
        // Pressure
        /**************************************************************************/
        tmp_float = BME280ReadPressure();
        tmp_u32 = (tmp_float * 100);
        mydata[4] = (tmp_u32 >> (8 * 0)) & 0xff;
        mydata[5] = (tmp_u32 >> (8 * 1)) & 0xff;
        mydata[6] = (tmp_u32 >> (8 * 2)) & 0xff;


        LMIC_setTxData2(1, mydata, sizeof(mydata) , 0);
   
    }

and for the uplink decoder I have:

function decodeUplink(input) {
  var decoded = {};
  
  decoded.vcc = (input.bytes[0] + 200) / 100;
  
  if (input.bytes[1] != 255 || input.bytes[2] != 255) {
    decoded.temperature = ((input.bytes[1] << 24 >> 16 | input.bytes[2]) / 10);
  }
  
  if(input.bytes[3] != 255) {
    decoded.humidity = input.bytes[3];
    if ((decoded.humidity & (1 << 7)) !== 0) {
      decoded.humidity = decoded.humidity & ~(1 << 7);
      decoded.humidity += 0.5;
    }
  }

  pressure = (input.bytes[4] << (8*0) | input.bytes[5] << (8*1) | input.bytes[6]  << (8*2)) / 100;
  decoded.pressure = pressure

  return {
    data: decoded,
    warnings: [],
    errors: []
  };
}

Decoded Payload:

"decoded_payload": {
        "humidity": 58.5,
        "pressure": 2.19,
        "temperature": 21.2,
        "vcc": 4.3
      }

Voltage, Temperature and Humidty all work perfectly! Pressure however… I just cannot get correct…
tmp_float = BME280ReadPressure(); returns a correct reading of circa 1000 Pa (e.g. 987.82 Pa) but the decoded payload = 2.19

I’ve spent hours trying to figure out whats going wrong testing all sorts of things but I’m stumped… does anyone here have any ideas or see anything obviously wrong with the code?

Missing ;?

Ah yes that’s definitely a typo, thanks for spotting it :+1:.

I must have created it whilst reverting back to the simplest version of the decoder function before posting it here… I’ve added the semi colon back in of course but sadly has no effect on the issue.

1 Like

Are you sure the buffer you are filling is big enough?
The code is not very clear about the unit of pressure (Pascal or bar or hPa/mbar)

I would run cppcheck on the source code and check any warnings it produces.

1 Like

The device code may benefit from some tidy up - sending floats is rarely recommended and I suspect you may have been massaging it to get it to work as multiplying pressure by 100 and then saving only three out of the four bytes of the float is unlikely to end well.

This may help: https://www.thethingsnetwork.org/docs/devices/bytes/

1 Like

I think you could well be right about buffer size!. All the code is something I’ve cobbled together from shared code & past projects and I have a horrible feeling I’ve not increased buffer size to allow for a bigger payload :man_facepalming::man_facepalming::man_facepalming:. I’ll test this first thing this evening! Thank you!

Thank you to those who replied with advice. As bertrik suggested it was indeed a case of too small of a payload buffer / buffer overflow that was causing so much grief :man_facepalming:.

Interestingly (or perhaps not for those of you with a talent for C++) chatgpt (which I’ve been finding helpful lately in finding coding errors & typos etc) never picked up on the buffer being too small… within a few hours of posting here I had my answer which shows there is clearly no substitute for real knowledge!

Having taken care of that and tidying up my code (which, quite rightly, was pointed out as being messy) my node is now all working perfectly.

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.