diff --git a/README.md b/README.md index 5257b8d..a4fc6ba 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,17 @@ import machine import bme280 i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) -bme = bme280.BME280(i2c=i2c) +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 c8d67ba..f106a00 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, @@ -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. @@ -191,16 +195,32 @@ 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() - p = p // 256 - pi = p // 100 - pd = p - pi * 100 + t /= 100 + p /= 25600 + h /= 1024 + a = 44330 * (1 - (p/self._slp)**0.190295) + return t, p, h, a - hi = h // 1024 - hd = h * 100 // 1024 - hi * 100 - return ("{}C".format(t / 100), "{}.{:02d}hPa".format(pi, pd), - "{}.{:02d}%".format(hi, hd)) + @property + def values(self): + """Print human readable values""" + t, p, h, a = self.scaledvalues + return ("{}C".format(t), + "{:.02f}hPa".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