From 1de19b208a419425af6129970211863c20ce6295 Mon Sep 17 00:00:00 2001 From: ztimson Date: Wed, 30 Oct 2024 11:03:30 -0400 Subject: [PATCH] Temp fix --- src/bme.js | 21 --------------------- src/bme280.js | 12 +++++++++++- 2 files changed, 11 insertions(+), 22 deletions(-) delete mode 100644 src/bme.js diff --git a/src/bme.js b/src/bme.js deleted file mode 100644 index d22fc6a..0000000 --- a/src/bme.js +++ /dev/null @@ -1,21 +0,0 @@ -import i2c from 'i2c-bus'; - -export async function bme(address = 0x76) { - const i2cBus = await i2c.openPromisified(1); - const data = await Promise.all([ - i2cBus.readByte(address, 0xFA), - i2cBus.readByte(address, 0xFB), - i2cBus.readByte(address, 0xFC), - i2cBus.readByte(address, 0xF7), - i2cBus.readByte(address, 0xF8), - i2cBus.readByte(address, 0xF9), - i2cBus.readByte(address, 0xFD), - i2cBus.readByte(address, 0xFE), - ]); - return { - temperature: (((data[0] << 12) | (data[1] << 4) | (data[2] >> 4)) / 16384.0 - 5120.0) / 100, - pressure: ((data[3] << 12) | (data[4] << 4) | (data[5] >> 4)) / 25600, - humidity: ((data[6] << 8) | data[7]) / 1024.0, - }; -} -console.log(await bme()); diff --git a/src/bme280.js b/src/bme280.js index dc680aa..d71df90 100644 --- a/src/bme280.js +++ b/src/bme280.js @@ -12,8 +12,18 @@ export async function bme(address = 0x76) { i2cBus.readByte(address, 0xFD), i2cBus.readByte(address, 0xFE), ]); + + const T1 = 27504; + const T2 = 26435; + const T3 = -1000; + + const rawTemp = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); + let var1 = (((rawTemp >> 3) - (T1 << 1)) * T2) / 2048; + let var2 = (((((rawTemp >> 4) - T1) * ((rawTemp >> 4) - T1)) >> 12) * T3) / 16384; + let temp = (var1 + var2) / 5120.0; + return { - temperature: (((data[0] << 12) | (data[1] << 4) | (data[2] >> 4)) / 16384.0 - 5120.0) / 100, + temperature: temp, pressure: ((data[3] << 12) | (data[4] << 4) | (data[5] >> 4)) / 25600, humidity: ((data[6] << 8) | data[7]) / 1024.0, };