From ef311c63e30899d10b8d218bc34a9c591c75c30a Mon Sep 17 00:00:00 2001 From: Chris Kuethe Date: Fri, 8 Sep 2017 21:14:02 -0700 Subject: [PATCH 1/4] make the i2c object be the first argument ...because it's required. The mode and address have reasonable defaults. --- README.md | 2 +- bme280.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5257b8d..a205581 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ import machine import bme280 i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) -bme = bme280.BME280(i2c=i2c) +bme = bme280.BME280(i2c) print(bme.values) ``` diff --git a/bme280.py b/bme280.py index c8d67ba..007e34d 100644 --- a/bme280.py +++ b/bme280.py @@ -53,9 +53,9 @@ class BME280: def __init__(self, + i2c=None, mode=BME280_OSAMPLE_1, address=BME280_I2CADDR, - i2c=None, **kwargs): # Check that mode is valid. if mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4, From 15bfb957cac3a1a250c2bb80c33580a84471db9b Mon Sep 17 00:00:00 2001 From: Chris Kuethe Date: Fri, 8 Sep 2017 21:27:13 -0700 Subject: [PATCH 2/4] regular math is easier to read --- bme280.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bme280.py b/bme280.py index 007e34d..0ee4c3a 100644 --- a/bme280.py +++ b/bme280.py @@ -196,11 +196,10 @@ def values(self): t, p, h = self.read_compensated_data() - p = p // 256 - pi = p // 100 - pd = p - pi * 100 - - hi = h // 1024 - hd = h * 100 // 1024 - hi * 100 - return ("{}C".format(t / 100), "{}.{:02d}hPa".format(pi, pd), - "{}.{:02d}%".format(hi, hd)) + t /= 100 + p /= 256 + h /= 1024 + + return ("{}C".format(t), + "{:.02f}hPa".format(p), + "{:.02f}%".format(h)) From 3f87e8f5508f5dda8a699e6f011f7563e3298ba6 Mon Sep 17 00:00:00 2001 From: Chris Kuethe Date: Fri, 8 Sep 2017 22:05:11 -0700 Subject: [PATCH 3/4] add altitude calculation --- README.md | 5 ++++- bme280.py | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a205581..a4fc6ba 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,15 @@ import bme280 i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) bme = bme280.BME280(i2c) +# optional, check your local METAR for the current value +bme.sea_level_millibars = 1013.10 + print(bme.values) ``` #### Detailed usage #### -The `values` property is a convenience function that provides a tuple of human-readable string values to quickly check that the sensor is working. In practice, the method to use is `read_compensated_data()` which returns a `(temperature, pressure, humidity)`-tuple: +The `values` property is a convenience function that provides a tuple of human-readable string values to quickly check that the sensor is working. In addition to the formatting the sensor values, it also computes pressure altitude. In practice, the method to use is `read_compensated_data()` which returns a `(temperature, pressure, humidity)`-tuple: * `temperature`: the temperature in hundredths of a degree celsius. For example, the value 2534 indicates a temperature of 25.34 degrees. * `pressure`: the atmospheric pressure. This 32-bit value consists of 24 bits indicating the integer value, and 8 bits indicating the fractional value. To get a value in Pascals, divide the return value by 256. For example, a value of 24674867 indicates 96386.2Pa, or 963.862hPa. diff --git a/bme280.py b/bme280.py index 0ee4c3a..c1bf318 100644 --- a/bme280.py +++ b/bme280.py @@ -96,6 +96,10 @@ def __init__(self, self._l8_barray = bytearray(8) self._l3_resultarray = array("i", [0, 0, 0]) + # sea level pressure in millibar, because it's + # easy to get from your local airport METAR + self._slp = 1013.25 + def read_raw_data(self, result): """ Reads the raw (uncompensated) data from the sensor. @@ -199,7 +203,21 @@ def values(self): t /= 100 p /= 256 h /= 1024 + sl = 100 * self._slp + a = 44330 * (1 - (p/sl)**0.190295) return ("{}C".format(t), - "{:.02f}hPa".format(p), - "{:.02f}%".format(h)) + "{:.02f}Pa".format(p), + "{:.02f}%".format(h), + "{:.02f}m".format(a), + ) + + @property + def sea_level_millibars(self): + return self._slp + + @sea_level_millibars.setter + def sea_level_millibars(self, value): + if value <= 0: + raise ValueError("sea_level_pressure must be greater than 0mb") + self._slp = value From 50f128f354e2131c090a0a041688dc91707cdd68 Mon Sep 17 00:00:00 2001 From: Chris Kuethe Date: Mon, 11 Sep 2017 21:15:37 -0700 Subject: [PATCH 4/4] split out the scaling from prettyprinting if you just want numerical values, use `.scaledvalues`, if you want formatted output with units continue to use `.values` --- bme280.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bme280.py b/bme280.py index c1bf318..f106a00 100644 --- a/bme280.py +++ b/bme280.py @@ -195,19 +195,22 @@ def read_compensated_data(self, result=None): return array("i", (temp, pressure, humidity)) @property - def values(self): - """ human readable values """ - + def scaledvalues(self): + """Scale raw sensor values to real world units""" t, p, h = self.read_compensated_data() t /= 100 - p /= 256 + p /= 25600 h /= 1024 - sl = 100 * self._slp - a = 44330 * (1 - (p/sl)**0.190295) + a = 44330 * (1 - (p/self._slp)**0.190295) + return t, p, h, a + @property + def values(self): + """Print human readable values""" + t, p, h, a = self.scaledvalues return ("{}C".format(t), - "{:.02f}Pa".format(p), + "{:.02f}hPa".format(p), "{:.02f}%".format(h), "{:.02f}m".format(a), )