Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add altitude calculation #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 31 additions & 11 deletions bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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