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?