# BME280 Pressure, LMIC

**URL:** <https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530>\
**Category:** LMIC\
**Created:** [April 8, 2024, 8:08pm UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530 "2024-04-08T20:08:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paule99](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@paule99](https://www.thethingsnetwork.org/forum/u/paule99)\
**Post date:** [April 8, 2024, 8:08pm UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/1 "2024-04-08T20:08:52Z")

</div>

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](https://github.com/JackGruber/esp32_ttn_environmental_sensor)) generating the payload:

```auto
        //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:

```auto
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:

```auto
"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?

---

<div class="post-metadata">

**Author:** ![stevencellist](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/stevencellist/32/46434_2.png) [@stevencellist](https://www.thethingsnetwork.org/forum/u/stevencellist)\
**Post date:** [April 8, 2024, 9:21pm UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/2 "2024-04-08T21:21:35Z")

</div>

> [@paule99](#):
>
> `decoded.pressure = pressure`

Missing `;`?

---

<div class="post-metadata">

**Author:** ![paule99](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@paule99](https://www.thethingsnetwork.org/forum/u/paule99)\
**Post date:** [April 8, 2024, 9:49pm UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/3 "2024-04-08T21:49:36Z")

</div>

Ah yes that’s definitely a typo, thanks for spotting it 👍.

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.

---

<div class="post-metadata">

**Author:** ![bertrik](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/bertrik/32/35697_2.png) [@bertrik](https://www.thethingsnetwork.org/forum/u/bertrik)\
**Post date:** [April 9, 2024, 8:56am UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/4 "2024-04-09T08:56:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [April 9, 2024, 9:16am UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/5 "2024-04-09T09:16:04Z")

</div>

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/](https://www.thethingsnetwork.org/docs/devices/bytes/)

---

<div class="post-metadata">

**Author:** ![paule99](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@paule99](https://www.thethingsnetwork.org/forum/u/paule99)\
**Post date:** [April 9, 2024, 12:48pm UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/6 "2024-04-09T12:48:13Z")

</div>

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 🤦‍♂️🤦‍♂️🤦‍♂️. I’ll test this first thing this evening! Thank you!

---

<div class="post-metadata">

**Author:** ![paule99](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@paule99](https://www.thethingsnetwork.org/forum/u/paule99)\
**Post date:** [April 10, 2024, 8:56am UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/7 "2024-04-10T08:56:41Z")

</div>

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 🤦‍♂️.

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.

---

<div class="post-metadata">

**Author:** ![system](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/6/2/6250ea44fd2b0d6259489e1a92748df681e1f970.png) [@system](https://www.thethingsnetwork.org/forum/u/system)\
**Post date:** [April 11, 2024, 8:57am UTC](https://www.thethingsnetwork.org/forum/t/bme280-pressure-lmic/67530/8 "2024-04-11T08:57:09Z")

</div>

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