87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |  |
95 |
96 |
97 |
98 | $projectname
99 | $projectnumber
101 |
102 |
103 | $projectbrief
104 | |
105 |
106 |
107 |
108 |
109 | $projectbrief
110 | |
111 |
112 |
113 |
114 |
115 | $searchbox |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/docs/images/bme280-gh-banner-py.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sparkfun/qwiic_bme280_py/c4576cc1450ffecff64535b7948d509ac690127d/docs/images/bme280-gh-banner-py.png
--------------------------------------------------------------------------------
/docs/images/sfe_flame.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sparkfun/qwiic_bme280_py/c4576cc1450ffecff64535b7948d509ac690127d/docs/images/sfe_flame.png
--------------------------------------------------------------------------------
/docs/requirements.txt:
--------------------------------------------------------------------------------
1 | m2r
2 | sparkfun-qwiic-i2c
3 |
--------------------------------------------------------------------------------
/examples/README.md:
--------------------------------------------------------------------------------
1 | # Sparkfun BME280 Examples Reference
2 | Below is a brief summary of each of the example programs included in this repository. To report a bug in any of these examples or to request a new feature or example [submit an issue in our GitHub issues.](https://github.com/sparkfun/qwiic_bme280_py/issues).
3 |
4 | NOTE: The abnormal numbering of examples is to retain consistency with the Arduino library from which this was ported.
5 |
6 | ## Example 1: Basic Readings
7 | This example demonstrates basic bringup of the BME280 to extract humidity, pressure, altitude, and temperature.
8 |
9 | The key methods showcased by this example are:
10 |
11 | - [read_humidity()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a601d62c86ef837f61c3e452a3f390b9d)
12 | - [read_pressure()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#af799265b51181bd65cae6983c245c513)
13 | - [get_altitude_feet()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#af5b0a1f1c2355f72d2182ed0c036ef8e)
14 | - [get_temperature_fahrenheit()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a31603e97db007558964618800b055a61)
15 |
16 | These methods are invoked via reading their corresponding properties([humidity](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#ace5c02deec66d7096a322f68bd19e35d), [pressure](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a169132f58174ba65b2ef1148858a93f7), [altitude_feet](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#aa6b842360817379f6c9026767b4c51cb), and [temperature_fahrenheit](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a42eb4ab7f10b8188d9174384feb2e328))
17 |
18 | ## Example 4: Settings
19 | This example shows how to change the various filter and oversample settings of the BME280. It sets several filtering and oversampling settings and then performs the same readings as in Example 1.
20 |
21 | The key methods showcased by this example are:
22 |
23 | - [set_filter()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a5f32790df30c3c23c82b2aa2f43fc378)
24 | - [set_standby_time()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#ac92bbaf17893b45805678d05af28cff8)
25 | - [set_tempature_oversample()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a4cb331d973e88bb6e85c03ead63b042f)
26 | - [set_humidity_oversample()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a510a98a4a15cdf4d634aaacfbde6139c)
27 | - [set_mode()](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a4cb378ab043d8dfa7c3c6c48e19bab84)
28 |
29 | These methods are invoked via reading their corresponding properties ([filter](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#abd7049904b69e17438de6c1bfad899b4), [standby_time](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a16991516b62efcb3400ce1a6ead3f8a3), [tempature_oversample](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#aaa2c92f61e6c0360c2d480074ed73b3a), [humidity_oversample](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a7b7bf88a522fced77db96301b5fced51), [mode](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#aec55ad7625fa470e5f7f4d0e415ec96e))
30 |
31 | ## Example 5: Read All Registers
32 | This example configures the BME280 to read all measurements. It also displays the BME280's physical memory and what the driver perceives the calibration words to be.
33 |
34 | This method showcases direct I2C reads as well as the use of the [calibration property](https://docs.sparkfun.com/qwiic_bme280_py/classqwiic__bme280_1_1_qwiic_bme280.html#a61ff5a498e96023a6c82cb99ca6a093b)
--------------------------------------------------------------------------------
/examples/qwiic_bme280_ex1.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | #-----------------------------------------------------------------------------
3 | # qwiic_env_bme280_ex1.py
4 | #
5 | # Simple Example for the Qwiic BME280 Device
6 | #------------------------------------------------------------------------
7 | #
8 | # Written by SparkFun Electronics, May 2019
9 | #
10 | # This python library supports the SparkFun Electroncis qwiic
11 | # qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12 | # board computers.
13 | #
14 | # More information on qwiic is at https://www.sparkfun.com/qwiic
15 | #
16 | # Do you like this library? Help support SparkFun. Buy a board!
17 | #
18 | #==================================================================================
19 | # Copyright (c) 2019 SparkFun Electronics
20 | #
21 | # Permission is hereby granted, free of charge, to any person obtaining a copy
22 | # of this software and associated documentation files (the "Software"), to deal
23 | # in the Software without restriction, including without limitation the rights
24 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 | # copies of the Software, and to permit persons to whom the Software is
26 | # furnished to do so, subject to the following conditions:
27 | #
28 | # The above copyright notice and this permission notice shall be included in all
29 | # copies or substantial portions of the Software.
30 | #
31 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 | # SOFTWARE.
38 | #==================================================================================
39 | # Example 1
40 | #
41 |
42 | import qwiic_bme280
43 | import time
44 | import sys
45 |
46 | def runExample():
47 |
48 | print("\nSparkFun BME280 Sensor Example 1\n")
49 | mySensor = qwiic_bme280.QwiicBme280()
50 |
51 | if mySensor.connected == False:
52 | print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \
53 | file=sys.stderr)
54 | return
55 |
56 | mySensor.begin()
57 |
58 | while True:
59 | print("Humidity:\t%.3f" % mySensor.humidity)
60 |
61 | print("Pressure:\t%.3f" % mySensor.pressure)
62 |
63 | print("Altitude:\t%.3f" % mySensor.altitude_feet)
64 |
65 | print("Temperature:\t%.2f" % mySensor.temperature_fahrenheit)
66 |
67 | print("")
68 |
69 | time.sleep(1)
70 |
71 |
72 | if __name__ == '__main__':
73 | try:
74 | runExample()
75 | except (KeyboardInterrupt, SystemExit) as exErr:
76 | print("\nEnding Example 1")
77 | sys.exit(0)
78 |
79 |
80 |
--------------------------------------------------------------------------------
/examples/qwiic_bme280_ex4.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | #-----------------------------------------------------------------------------
3 | # qwiic_env_bme280_ex4.py
4 | #
5 | # Simple Example for the Qwiic BME280 Device
6 | #------------------------------------------------------------------------
7 | #
8 | # Written by SparkFun Electronics, May 2019
9 | #
10 | # This python library supports the SparkFun Electroncis qwiic
11 | # qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12 | # board computers.
13 | #
14 | # More information on qwiic is at https:# www.sparkfun.com/qwiic
15 | #
16 | # Do you like this library? Help support SparkFun. Buy a board!
17 | #
18 | #==================================================================================
19 | # Copyright (c) 2019 SparkFun Electronics
20 | #
21 | # Permission is hereby granted, free of charge, to any person obtaining a copy
22 | # of this software and associated documentation files (the "Software"), to deal
23 | # in the Software without restriction, including without limitation the rights
24 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 | # copies of the Software, and to permit persons to whom the Software is
26 | # furnished to do so, subject to the following conditions:
27 | #
28 | # The above copyright notice and this permission notice shall be included in all
29 | # copies or substantial portions of the Software.
30 | #
31 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 | # SOFTWARE.
38 | #==================================================================================
39 | # Example 4 - port of the Arduino example 4
40 | #
41 |
42 | import qwiic_bme280
43 | import time
44 | import sys
45 |
46 | def runExample():
47 |
48 | print("\nSparkFun BME280 Sensor Example 4\n")
49 | mySensor = qwiic_bme280.QwiicBme280()
50 |
51 | if mySensor.connected == False:
52 | print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \
53 | file=sys.stderr)
54 | return
55 |
56 | mySensor.begin()
57 |
58 | # setup the sensor
59 | mySensor.filter = 1 # 0 to 4 is valid. Filter coefficient. See 3.4.4
60 | mySensor.standby_time = 0 # 0 to 7 valid. Time between readings. See table 27.
61 |
62 | mySensor.tempature_oversample = 1 # 0 to 16 are valid. 0 disables temp sensing. See table 24.
63 | mySensor.pressure_oversample = 1 # 0 to 16 are valid. 0 disables pressure sensing. See table 23.
64 | mySensor.humidity_oversample = 1 # 0 to 16 are valid. 0 disables humidity sensing. See table 19.
65 | mySensor.mode = mySensor.MODE_NORMAL # MODE_SLEEP, MODE_FORCED, MODE_NORMAL is valid. See 3.3
66 |
67 | while True:
68 | print("Humidity:\t%.3f" % mySensor.humidity)
69 |
70 | print("Pressure:\t%.3f" % mySensor.pressure)
71 |
72 | print("Altitude:\t%.3f" % mySensor.altitude_feet)
73 |
74 | print("Temperature:\t%.2f\n" % mySensor.temperature_fahrenheit)
75 |
76 | time.sleep(1)
77 |
78 |
79 | if __name__ == '__main__':
80 | try:
81 | runExample()
82 | except (KeyboardInterrupt, SystemExit) as exErr:
83 | print("\nEnding Example 4")
84 | sys.exit(0)
85 |
86 |
87 |
--------------------------------------------------------------------------------
/examples/qwiic_bme280_ex5.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | #-----------------------------------------------------------------------------
3 | # qwiic_env_bme280_ex5.py
4 | #
5 | # Simple Example for the Qwiic BME280 Device
6 | #------------------------------------------------------------------------
7 | #
8 | # Written by SparkFun Electronics, May 2019
9 | #
10 | # This python library supports the SparkFun Electroncis qwiic
11 | # qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12 | # board computers.
13 | #
14 | # More information on qwiic is at https:# www.sparkfun.com/qwiic
15 | #
16 | # Do you like this library? Help support SparkFun. Buy a board!
17 | #
18 | #==================================================================================
19 | # Copyright (c) 2019 SparkFun Electronics
20 | #
21 | # Permission is hereby granted, free of charge, to any person obtaining a copy
22 | # of this software and associated documentation files (the "Software"), to deal
23 | # in the Software without restriction, including without limitation the rights
24 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 | # copies of the Software, and to permit persons to whom the Software is
26 | # furnished to do so, subject to the following conditions:
27 | #
28 | # The above copyright notice and this permission notice shall be included in all
29 | # copies or substantial portions of the Software.
30 | #
31 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 | # SOFTWARE.
38 | #==================================================================================
39 | # Example 5 - port of the Arduino example 5
40 | #
41 |
42 | import qwiic_bme280
43 | import time
44 | import sys
45 |
46 | def runExample():
47 |
48 | print("\nSparkFun BME280 Sensor Example 5\n")
49 | mySensor = qwiic_bme280.QwiicBme280()
50 |
51 | if mySensor.connected == False:
52 | print("The Qwiic BME280 device isn't connected to the system. Please check your connection", \
53 | file=sys.stderr)
54 | return
55 |
56 | mySensor.begin()
57 |
58 | print("ID(0xD0): 0x%.2x" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_CHIP_ID_REG))
59 | print("Reset register(0xE0): 0x%.2x" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_RST_REG))
60 | print("ctrl_meas(0xF4): 0x%.2x" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_CTRL_MEAS_REG))
61 | print("ctrl_hum(0xF2): 0x%.2x\n" % mySensor._i2c.readByte(mySensor.address, mySensor.BME280_CTRL_HUMIDITY_REG))
62 |
63 | print("Displaying all regs:")
64 | memCounter = 0x80
65 | for row in range(8,16):
66 | print("0x%.2x 0:" % row, end='')
67 | for column in range(0,16):
68 | tempReadData = mySensor._i2c.readByte(mySensor.address, memCounter)
69 | print("0x%.2x " % tempReadData, end='')
70 |
71 | memCounter += 1
72 | print("")
73 |
74 |
75 | print("Displaying concatenated calibration words:")
76 | print("dig_T1, uint16: %d" % mySensor.calibration["dig_T1"])
77 | print("dig_T2, int16: %d" % mySensor.calibration["dig_T2"])
78 | print("dig_T3, int16: %d" % mySensor.calibration["dig_T3"])
79 | print("dig_P1, uint16: %d" % mySensor.calibration["dig_P1"])
80 | print("dig_P2, int16: %d" % mySensor.calibration["dig_P2"])
81 | print("dig_P3, int16: %d" % mySensor.calibration["dig_P3"])
82 | print("dig_P4, int16: %d" % mySensor.calibration["dig_P4"])
83 | print("dig_P5, int16: %d" % mySensor.calibration["dig_P5"])
84 | print("dig_P6, int16: %d" % mySensor.calibration["dig_P6"])
85 | print("dig_P7, int16: %d" % mySensor.calibration["dig_P7"])
86 | print("dig_P8, int16: %d" % mySensor.calibration["dig_P8"])
87 | print("dig_P9, int16: %d" % mySensor.calibration["dig_P9"])
88 | print("dig_H1, uint8: %d" % mySensor.calibration["dig_H1"])
89 | print("dig_H2, int16: %d" % mySensor.calibration["dig_H2"])
90 | print("dig_H3, uint8: %d" % mySensor.calibration["dig_H3"])
91 | print("dig_H4, int16: %d" % mySensor.calibration["dig_H5"])
92 | print("dig_H6, int8: %d" % mySensor.calibration["dig_H6"])
93 | while True:
94 |
95 | print("Humidity:\t%.3f" % mySensor.humidity)
96 |
97 | print("Pressure:\t%.3f" % mySensor.pressure)
98 |
99 | print("Altitude:\t%.3f" % mySensor.altitude_feet)
100 |
101 | print("Temperature:\t%.2f\n" % mySensor.temperature_fahrenheit)
102 |
103 | time.sleep(1)
104 |
105 | if __name__ == '__main__':
106 | try:
107 | runExample()
108 | except (KeyboardInterrupt, SystemExit) as exErr:
109 | print("\nEnding Example 5")
110 | sys.exit(0)
111 |
112 |
113 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "urls": [
3 | ["qwiic_bme280.py", "github:sparkfun/Qwiic_BME280_Py/qwiic_bme280.py"]
4 | ],
5 | "deps": [
6 | ["github:sparkfun/Qwiic_I2C_Py", "master"]
7 | ],
8 | "version": "2.0.0"
9 | }
10 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [build-system]
2 | requires = ["setuptools"]
3 |
4 | [project]
5 | name = "sparkfun_qwiic_bme280"
6 | version = "2.0.0"
7 | description = "SparkFun Electronics Qwiic BME280"
8 | readme = "DESCRIPTION.rst"
9 |
10 | authors = [{name="Sparkfun Electronics", email="info@sparkfun.com"}]
11 | dependencies = ["sparkfun_qwiic_i2c"]
12 |
13 | classifiers=[
14 | # How mature is this project? Common values are
15 | # 3 - Alpha
16 | # 4 - Beta
17 | # 5 - Production/Stable
18 | "Development Status :: 5 - Production/Stable",
19 |
20 | # Indicate who your project is intended for
21 | "Intended Audience :: Developers",
22 | "Topic :: Software Development :: Build Tools",
23 |
24 | # Pick your license as you wish (should match "license" above)
25 | "License :: OSI Approved :: MIT License",
26 |
27 | # Specify the Python versions you support here. In particular, ensure
28 | # that you indicate whether you support Python 2, Python 3 or both.
29 | "Programming Language :: Python :: 3.5",
30 | "Programming Language :: Python :: 3.6",
31 | "Programming Language :: Python :: 3.7",
32 | "Programming Language :: Python :: 3.8",
33 | "Programming Language :: Python :: 3.9",
34 | "Programming Language :: Python :: 3.10",
35 | "Programming Language :: Python :: 3.11",
36 | "Programming Language :: Python :: 3.12",
37 | "Programming Language :: Python :: 3.13",
38 | "Programming Language :: Python :: 3.14",
39 | "Programming Language :: Python :: Implementation :: MicroPython",
40 | # NOTE: CircuitPython is also supported, but no classifier exists for it
41 | ]
42 |
43 | keywords = ["electronics, maker"]
44 |
45 | [project.urls]
46 | homepage = "https://www.sparkfun.com/products/14348"
47 |
48 | [tool.setuptools]
49 | py-modules = ["qwiic_bme280"]
50 |
--------------------------------------------------------------------------------
/qwiic_bme280.py:
--------------------------------------------------------------------------------
1 | #-----------------------------------------------------------------------------
2 | # qwiic_bme280.py
3 | #
4 | # Python library for the SparkFun qwiic BME280 sensor.
5 | #
6 | # This sensor is available on the SparkFun Environmental Combo Breakout board.
7 | # https://www.sparkfun.com/products/14348
8 | #
9 | #------------------------------------------------------------------------
10 | #
11 | # Written by SparkFun Electronics, May 2019
12 | #
13 | # This python library supports the SparkFun Electroncis qwiic
14 | # qwiic sensor/board ecosystem
15 | #
16 | # More information on qwiic is at https:// www.sparkfun.com/qwiic
17 | #
18 | # Do you like this library? Help support SparkFun. Buy a board!
19 | #==================================================================================
20 | # Copyright (c) 2019 SparkFun Electronics
21 | #
22 | # Permission is hereby granted, free of charge, to any person obtaining a copy
23 | # of this software and associated documentation files (the "Software"), to deal
24 | # in the Software without restriction, including without limitation the rights
25 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 | # copies of the Software, and to permit persons to whom the Software is
27 | # furnished to do so, subject to the following conditions:
28 | #
29 | # The above copyright notice and this permission notice shall be included in all
30 | # copies or substantial portions of the Software.
31 | #
32 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 | # SOFTWARE.
39 | #==================================================================================
40 | #
41 | # This is mostly a port of existing Arduino functionaly, so pylint is sad.
42 | # The goal is to keep the public interface pthonic, but internal is internal
43 | #
44 | # pylint: disable=line-too-long, bad-whitespace, invalid-name, too-many-public-methods
45 | #
46 |
47 | """!
48 | qwiic_bme280
49 | ============
50 | Python module for the qwiic bme280 sensor, which is part of the [SparkFun Qwiic Environmental Combo Breakout](https://www.sparkfun.com/products/14348)
51 |
52 | This python package is a port of the existing [SparkFun BME280 Arduino Library](https://github.com/sparkfun/SparkFun_BME280_Arduino_Library)
53 |
54 | This package can be used in conjunction with the overall [SparkFun qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
55 |
56 | New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).
57 | """
58 |
59 | #-----------------------------------------------------------------------------
60 | import math
61 | import time
62 | import qwiic_i2c
63 |
64 | # Define the device name and I2C addresses. These are set in the class defintion
65 | # as class variables, making them avilable without having to create a class instance.
66 | # This allows higher level logic to rapidly create a index of qwiic devices at
67 | # runtine
68 | #
69 | # The name of this device
70 | _DEFAULT_NAME = "Qwiic BME280"
71 |
72 | # Some devices have multiple availabel addresses - this is a list of these addresses.
73 | # NOTE: The first address in this list is considered the default I2C address for the
74 | # device.
75 | _AVAILABLE_I2C_ADDRESS = [0x77, 0x76]
76 |
77 | # Default Setting Values
78 | _settings = {"runMode" : 3, \
79 | "tStandby" : 0, \
80 | "filter" : 0, \
81 | "tempOverSample" : 1, \
82 | "pressOverSample" : 1, \
83 | "humidOverSample" : 1, \
84 | "tempCorrection" : 0.0}
85 |
86 | # define our valid chip IDs
87 | _validChipIDs = [0x58, 0x60]
88 |
89 | # define the class that encapsulates the device being created. All information associated with this
90 | # device is encapsulated by this class. The device class should be the only value exported
91 | # from this module.
92 |
93 | class QwiicBme280(object):
94 | """!
95 | QwiicBme280
96 |
97 | @param address: The I2C address to use for the device.
98 | If not provided, the default address is used.
99 | @param i2c_driver: An existing i2c driver object. If not provided
100 | a driver object is created.
101 |
102 | @return **Object** The BME280 device object.
103 | """
104 | # Constructor
105 | device_name =_DEFAULT_NAME
106 | available_addresses = _AVAILABLE_I2C_ADDRESS
107 |
108 | # mode flags for the device - user exposed
109 | MODE_SLEEP = 0b00
110 | MODE_FORCED = 0b01
111 | MODE_NORMAL = 0b11
112 |
113 | # Register names for the BME280
114 | BME280_DIG_T1_LSB_REG = 0x88
115 | BME280_DIG_T1_MSB_REG = 0x89
116 | BME280_DIG_T2_LSB_REG = 0x8A
117 | BME280_DIG_T2_MSB_REG = 0x8B
118 | BME280_DIG_T3_LSB_REG = 0x8C
119 | BME280_DIG_T3_MSB_REG = 0x8D
120 | BME280_DIG_P1_LSB_REG = 0x8E
121 | BME280_DIG_P1_MSB_REG = 0x8F
122 | BME280_DIG_P2_LSB_REG = 0x90
123 | BME280_DIG_P2_MSB_REG = 0x91
124 | BME280_DIG_P3_LSB_REG = 0x92
125 | BME280_DIG_P3_MSB_REG = 0x93
126 | BME280_DIG_P4_LSB_REG = 0x94
127 | BME280_DIG_P4_MSB_REG = 0x95
128 | BME280_DIG_P5_LSB_REG = 0x96
129 | BME280_DIG_P5_MSB_REG = 0x97
130 | BME280_DIG_P6_LSB_REG = 0x98
131 | BME280_DIG_P6_MSB_REG = 0x99
132 | BME280_DIG_P7_LSB_REG = 0x9A
133 | BME280_DIG_P7_MSB_REG = 0x9B
134 | BME280_DIG_P8_LSB_REG = 0x9C
135 | BME280_DIG_P8_MSB_REG = 0x9D
136 | BME280_DIG_P9_LSB_REG = 0x9E
137 | BME280_DIG_P9_MSB_REG = 0x9F
138 | BME280_DIG_H1_REG = 0xA1
139 | BME280_CHIP_ID_REG = 0xD0 # Chip ID
140 | BME280_RST_REG = 0xE0 # Softreset Reg
141 | BME280_DIG_H2_LSB_REG = 0xE1
142 | BME280_DIG_H2_MSB_REG = 0xE2
143 | BME280_DIG_H3_REG = 0xE3
144 | BME280_DIG_H4_MSB_REG = 0xE4
145 | BME280_DIG_H4_LSB_REG = 0xE5
146 | BME280_DIG_H5_MSB_REG = 0xE6
147 | BME280_DIG_H6_REG = 0xE7
148 | BME280_CTRL_HUMIDITY_REG = 0xF2 # Ctrl Humidity Reg
149 | BME280_STAT_REG = 0xF3 # Status Reg
150 | BME280_CTRL_MEAS_REG = 0xF4 # Ctrl Measure Reg
151 | BME280_CONFIG_REG = 0xF5 # Configuration Reg
152 | BME280_PRESSURE_MSB_REG = 0xF7 # Pressure MSB
153 | BME280_PRESSURE_LSB_REG = 0xF8 # Pressure LSB
154 | BME280_PRESSURE_XLSB_REG = 0xF9 # Pressure XLSB
155 | BME280_TEMPERATURE_MSB_REG = 0xFA # Temperature MSB
156 | BME280_TEMPERATURE_LSB_REG = 0xFB # Temperature LSB
157 | BME280_TEMPERATURE_XLSB_REG = 0xFC # Temperature XLSB
158 | BME280_HUMIDITY_MSB_REG = 0xFD # Humidity MSB
159 | BME280_HUMIDITY_LSB_REG = 0xFE # Humidity LSB
160 |
161 | # Constructor
162 | def __init__(self, address=None, i2c_driver=None):
163 |
164 | # Did the user specify an I2C address?
165 | if address in self.available_addresses:
166 | self.address = address
167 | else:
168 | self.address = self.available_addresses[0]
169 |
170 | # load the I2C driver if one isn't provided
171 |
172 | if i2c_driver is None:
173 | self._i2c = qwiic_i2c.getI2CDriver()
174 | if self._i2c is None:
175 | print("Unable to load I2C driver for this platform.")
176 | return
177 | else:
178 | self._i2c = i2c_driver
179 |
180 |
181 | # create a dictionary to stash our calibration data for the sensor
182 | self.calibration={}
183 |
184 | self.t_fine=0
185 |
186 | self._referencePressure = 101325.0
187 |
188 | # ----------------------------------
189 | # is_connected()
190 | #
191 | # Is an actual board connected to our system?
192 |
193 | def is_connected(self):
194 | """!
195 | Determine if a BME280 device is conntected to the system..
196 |
197 | @return **bool** True if the device is connected, otherwise False.
198 | """
199 | return self._i2c.isDeviceConnected(self.address)
200 |
201 | connected = property(is_connected)
202 |
203 | # ----------------------------------
204 | # begin()
205 | #
206 | # Initialize the system/validate the board.
207 | def begin(self):
208 | """!
209 | Initialize the operation of the BME280 module
210 |
211 | @return **bool** Returns true of the initializtion was successful, otherwise False.
212 | """
213 | # are we who we need to be?
214 | chipID = self._i2c.readByte(self.address, self.BME280_CHIP_ID_REG)
215 | if chipID not in _validChipIDs:
216 | print("Invalid Chip ID: 0x%.2X" % chipID)
217 | return False
218 |
219 |
220 | def unsigned_short_to_signed_short(n):
221 | """!
222 | Converts a 2 byte unsigned short to signed short (uint16_t -> int16_t)
223 |
224 | @param n: Unsigned short (uint16_t) to convert
225 |
226 | @return **int** Signed short (int16_t)
227 | """
228 | assert n >= 0, "Expected number to be unsigned (uint16_t), n>=0"
229 | assert n <= 2**16 - 1,"Expected number to be short (int16_t), n <= 2^16 - 1"
230 |
231 | return n - 2**16 if n >= 2**15 else n
232 |
233 | def unsigned_char_to_signed_char(n):
234 | """!
235 | Converts an unsigned char to signed char (uint8_t -> int8_t)
236 |
237 | @param n: Unsigned char (uint8_t) to convert
238 |
239 | @return **int** Signed char (int8_t)
240 | """
241 | assert n >= 0, "Expected number to be unsigned (uint8_t), n>=0"
242 | assert n <= 2**8 - 1,"Expected number to be char (int8_t), n <= 2^8 - 1"
243 |
244 | return n - 2**8 if n >= 2**7 else n
245 |
246 | # Reading all compensation data, range 0x88:A1, 0xE1:E7
247 | self.calibration["dig_T1"] = (self._i2c.readByte(self.address, self.BME280_DIG_T1_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_T1_LSB_REG)
248 | self.calibration["dig_T2"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_T2_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_T2_LSB_REG))
249 | self.calibration["dig_T3"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_T3_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_T3_LSB_REG))
250 |
251 | self.calibration["dig_P1"] = (self._i2c.readByte(self.address, self.BME280_DIG_P1_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P1_LSB_REG)
252 | self.calibration["dig_P2"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P2_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P2_LSB_REG))
253 | self.calibration["dig_P3"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P3_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P3_LSB_REG))
254 | self.calibration["dig_P4"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P4_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P4_LSB_REG))
255 | self.calibration["dig_P5"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P5_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P5_LSB_REG))
256 | self.calibration["dig_P6"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P6_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P6_LSB_REG))
257 | self.calibration["dig_P7"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P7_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P7_LSB_REG))
258 | self.calibration["dig_P8"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P8_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P8_LSB_REG))
259 | self.calibration["dig_P9"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_P9_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_P9_LSB_REG))
260 |
261 | self.calibration["dig_H1"] = self._i2c.readByte(self.address, self.BME280_DIG_H1_REG)
262 | self.calibration["dig_H2"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_H2_MSB_REG) << 8) + self._i2c.readByte(self.address, self.BME280_DIG_H2_LSB_REG))
263 | self.calibration["dig_H3"] = self._i2c.readByte(self.address, self.BME280_DIG_H3_REG)
264 | self.calibration["dig_H4"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_H4_MSB_REG) << 4) + (self._i2c.readByte(self.address, self.BME280_DIG_H4_LSB_REG) & 0x0F))
265 | self.calibration["dig_H5"] = unsigned_short_to_signed_short((self._i2c.readByte(self.address, self.BME280_DIG_H5_MSB_REG) << 4) + ((self._i2c.readByte(self.address, self.BME280_DIG_H4_LSB_REG) >> 4) & 0x0F))
266 | self.calibration["dig_H6"] = unsigned_char_to_signed_char(self._i2c.readByte(self.address, self.BME280_DIG_H6_REG))
267 |
268 | # Most of the time the sensor will be init with default values
269 | # But in case user has old/deprecated code, use the _settings.x values
270 |
271 | self.set_standby_time(_settings["tStandby"])
272 | self.set_filter(_settings["filter"])
273 | self.set_pressure_oversample(_settings["pressOverSample"]) # Default of 1x oversample
274 | self.set_humidity_oversample(_settings["humidOverSample"]) # Default of 1x oversample
275 | self.set_tempature_oversample(_settings["tempOverSample"]) # Default of 1x oversample
276 |
277 | self.set_mode(self.MODE_NORMAL) #Go!
278 |
279 | # Wait for first measurement to come through. According to the second
280 | # equation in section 9.1 of the datasheet, it should take no longer
281 | # than 9.3ms to finish the first measurement at 1x oversampling on all
282 | # sensors. So wat 10ms to be safe
283 | time.sleep(0.01)
284 |
285 | return True
286 |
287 | #----------------------------------------------------------------
288 | # Mode of the sensor
289 |
290 | def set_mode(self, mode):
291 | """!
292 | Set the operational mode of the sensor.
293 |
294 | @param mode: One of the class constant values.
295 | MODE_SLEEP, MODE_FORCED, MODE_NORMAL
296 |
297 | @return No return value
298 | """
299 | if mode > 0b11:
300 | mode = 0 # Error check. Default to sleep mode
301 |
302 | controlData = self._i2c.readByte(self.address, self.BME280_CTRL_MEAS_REG)
303 | controlData &= (~( (1<<1) | (1<<0) ) ) & 0xFF # Clear the mode[1:0] bits - note we just want a byte
304 | controlData |= mode # Set
305 | self._i2c.writeByte(self.address, self.BME280_CTRL_MEAS_REG, controlData)
306 |
307 |
308 | def get_mode(self):
309 | """!
310 | Returns the operational mode of the sensor.
311 |
312 | @return **MODE_SLEEP, MODE_FORCED, or MODE_NORMAL** The current operational mode
313 | """
314 | controlData = self._i2c.readByte(self.address, self.BME280_CTRL_MEAS_REG)
315 | return controlData & 0b00000011
316 |
317 | # Make the mode a property of this object
318 | mode = property(get_mode, set_mode)
319 |
320 | #----------------------------------------------------------------
321 | # Set the standby bits in the config register
322 | # tStandby can be:
323 | # 0, 0.5ms
324 | # 1, 62.5ms
325 | # 2, 125ms
326 | # 3, 250ms
327 | # 4, 500ms
328 | # 5, 1000ms
329 | # 6, 10ms
330 | # 7, 20ms
331 | def set_standby_time(self, timeSetting):
332 | """!
333 | Set the standby bits in the BME280s config register
334 |
335 | @param timeSetting: The standby time bits - acceptable values
336 | 0 = 0.5ms
337 | 1 = 62.5ms
338 | 2 = 125ms
339 | 3 = 250ms
340 | 4 = 500ms
341 | 5 = 1000ms
342 | 6 = 10ms
343 | 7 = 20ms
344 |
345 | @return No return value
346 | """
347 |
348 | if timeSetting > 0b111 :
349 | timeSetting = 0 # Error check. Default to 0.5ms
350 |
351 | controlData = self._i2c.readByte(self.address, self.BME280_CONFIG_REG)
352 | controlData &= ( ~( (1<<7) | (1<<6) | (1<<5) )) & 0xff # Clear the 7/6/5 bits
353 | controlData |= (timeSetting << 5) # Align with bits 7/6/5
354 | self._i2c.writeByte(self.address, self.BME280_CONFIG_REG, controlData)
355 |
356 | # Make standby time a property
357 | standby_time = property()
358 | standby_time = standby_time.setter(set_standby_time)
359 |
360 | #----------------------------------------------------------------
361 | # Set the filter bits in the config register
362 | # filter can be off or number of FIR coefficients to use:
363 | # 0, filter off
364 | # 1, coefficients = 2
365 | # 2, coefficients = 4
366 | # 3, coefficients = 8
367 | # 4, coefficients = 16
368 | def set_filter(self, filterSetting):
369 | """!
370 | Set the filter bits in the BME280s config register
371 |
372 | @param filterSetting: The filter bits for the BME280. Acceptable values
373 | 0 = filter off
374 | 1 = coefficients = 2
375 | 2 = coefficients = 4
376 | 3 = coefficients = 8
377 | 4 = coefficients = 16
378 |
379 | @return No return value
380 | """
381 | if filterSetting > 0b111 :
382 | filterSetting = 0 # Error check. Default to filter off
383 |
384 | controlData = self._i2c.readByte(self.address, self.BME280_CONFIG_REG)
385 | controlData &= (~( (1<<4) | (1<<3) | (1<<2) ) ) & 0xFF # Clear the 4/3/2 bits
386 | controlData |= (filterSetting << 2) # Align with bits 4/3/2
387 | self._i2c.writeByte(self.address, self.BME280_CONFIG_REG, controlData)
388 |
389 | filter = property()
390 | filter = filter.setter(set_filter)
391 |
392 | #----------------------------------------------------------------
393 | # Set the temperature oversample value
394 | # 0 turns off temp sensing
395 | # 1 to 16 are valid over sampling values
396 | def set_tempature_oversample(self, overSampleAmount):
397 | """!
398 | Set the temperature oversample value
399 |
400 | @param overSampleAmount: The temperature oversample value. Acceptable values
401 | 0 = turns off temp sensing
402 | 1 to 16 are valid over sampling values
403 |
404 | @return No return value
405 | """
406 | overSampleAmount = self.check_sample_value(overSampleAmount) # Error check
407 |
408 | originalMode = self.get_mode() # Get the current mode so we can go back to it at the end
409 |
410 | self.set_mode(self.MODE_SLEEP) # Config will only be writeable in sleep mode, so first go to sleep mode
411 |
412 | # Set the osrs_t bits (7, 6, 5) to overSampleAmount
413 | controlData = self._i2c.readByte(self.address, self.BME280_CTRL_MEAS_REG)
414 | controlData &= (~( (1<<7) | (1<<6) | (1<<5) )) & 0xFF # Clear bits 765
415 | controlData |= overSampleAmount << 5 # Align overSampleAmount to bits 7/6/5
416 | self._i2c.writeByte(self.address, self.BME280_CTRL_MEAS_REG, controlData)
417 |
418 | self.set_mode(originalMode) # Return to the original user's choice
419 |
420 | tempature_oversample = property()
421 | tempature_oversample = tempature_oversample.setter(set_tempature_oversample)
422 |
423 | # Set the pressure oversample value
424 | # 0 turns off pressure sensing
425 | # 1 to 16 are valid over sampling values
426 | def set_pressure_oversample(self, overSampleAmount):
427 | """!
428 | Set the pressure oversample value
429 |
430 | @param overSampleAmount: The pressure oversample value. Acceptable values
431 | 0 = turns off pressure sensing
432 | 1 to 16 are valid over sampling values
433 |
434 | @return No return value
435 | """
436 | overSampleAmount = self.check_sample_value(overSampleAmount) # Error check
437 |
438 | originalMode = self.get_mode() # Get the current mode so we can go back to it at the end
439 |
440 | self.set_mode(self.MODE_SLEEP) # Config will only be writeable in sleep mode, so first go to sleep mode
441 |
442 | # Set the osrs_p bits (4, 3, 2) to overSampleAmount
443 | controlData = self._i2c.readByte(self.address, self.BME280_CTRL_MEAS_REG)
444 | controlData &= (~( (1<<4) | (1<<3) | (1<<2) )) & 0xFF # Clear bits 432
445 | controlData |= overSampleAmount << 2 # Align overSampleAmount to bits 4/3/2
446 | self._i2c.writeByte(self.address, self.BME280_CTRL_MEAS_REG, controlData)
447 |
448 | self.set_mode(originalMode) # Return to the original user's choice
449 |
450 | pressure_oversample = property()
451 | pressure_oversample = pressure_oversample.setter(set_pressure_oversample)
452 |
453 | #----------------------------------------------------------------
454 | # Set the humidity oversample value
455 | # 0 turns off humidity sensing
456 | # 1 to 16 are valid over sampling values
457 | def set_humidity_oversample(self, overSampleAmount):
458 | """!
459 | Set the humidity oversample value
460 |
461 | @param overSampleAmount: The humidity oversample value. Acceptable values
462 | 0 = turns off humidity sensing
463 | 1 to 16 are valid over sampling values
464 |
465 | @return No return value
466 | """
467 | overSampleAmount = self.check_sample_value(overSampleAmount) # Error check
468 |
469 | originalMode = self.get_mode() # Get the current mode so we can go back to it at the end
470 |
471 | self.set_mode(self.MODE_SLEEP) # Config will only be writeable in sleep mode, so first go to sleep mode
472 |
473 | # Set the osrs_h bits (2, 1, 0) to overSampleAmount
474 | controlData = self._i2c.readByte(self.address, self.BME280_CTRL_HUMIDITY_REG)
475 | controlData &= (~( (1<<2) | (1<<1) | (1<<0) )) & 0xFF # Clear bits 2/1/0
476 | controlData |= overSampleAmount << 0 # Align overSampleAmount to bits 2/1/0
477 | self._i2c.writeByte(self.address, self.BME280_CTRL_HUMIDITY_REG, controlData)
478 |
479 | self.set_mode(originalMode) # Return to the original user's choice
480 |
481 | humidity_oversample = property()
482 | humidity_oversample = humidity_oversample.setter(set_humidity_oversample)
483 |
484 | #----------------------------------------------------------------
485 | # Validates an over sample value
486 | # Allowed values are 0 to 16
487 | # These are used in the humidty, pressure, and temp oversample functions
488 | #
489 | # pylint: disable=no-self-use
490 | def check_sample_value(self, userValue):
491 | """!
492 | Validates an over sample value
493 |
494 | @param userValue: The oversample value to check.
495 | Allowed values are 0 to 16
496 | These are used in the humidty, pressure, and temp oversample functions
497 |
498 | @return **int** Valid oversample value
499 | """
500 | _valueMap = { 0: 0, 1: 1, 2: 2, 4: 3, 8: 4, 16: 5}
501 |
502 | return _valueMap[userValue] if userValue in _valueMap.keys() else 1
503 |
504 | # pylint: enable=no-self-use
505 | # Check the measuring bit and return true while device is taking measurement
506 | def is_measuring(self):
507 | """!
508 | Return if the sensor is measuring or not
509 |
510 | @return True if the sensor is measuring, else False
511 | :rvalue: boolean
512 | """
513 |
514 | stat = self._i2c.readByte(self.address, self.BME280_STAT_REG)
515 | return True if stat & (1<<3) else False # If the measuring bit (3) is set, return true
516 |
517 |
518 | # Strictly resets. Run .begin() afterwards
519 | def reset( self ):
520 | """!
521 | Resets the sensor. If called, the begin method must be called before
522 | using the sensor.
523 | """
524 | self._i2c.writeByte(self.address, self.BME280_RST_REG, 0xB6)
525 |
526 | # ****************************************************************************#
527 | #
528 | # Pressure Section
529 | #
530 | # ****************************************************************************#
531 | def read_pressure( self ):
532 | """!
533 | Returns pressure in Pa.
534 |
535 | @return **float** Pressure in Pa
536 | """
537 |
538 | # Read temperature to update t_fine
539 | self.get_temperature_celsius()
540 |
541 | data_buffer = self._i2c.readBlock(self.address, self.BME280_PRESSURE_MSB_REG, 3)
542 | adc_P = (data_buffer[0] << 12) | (data_buffer[1] << 4) | ((data_buffer[2] >> 4) & 0x0F)
543 |
544 | var1 = self.t_fine - 128000
545 | var2 = var1 * var1 * self.calibration["dig_P6"]
546 | var2 = var2 + ((var1 * self.calibration["dig_P5"])<<17)
547 | var2 = var2 + (self.calibration["dig_P4"] <<35)
548 | var1 = ((var1 * var1 * self.calibration["dig_P3"])>>8) + ((var1 * self.calibration["dig_P2"])<<12)
549 | var1 = ( (1 << 47) + var1 )*(self.calibration["dig_P1"])>>33
550 |
551 | if var1 == 0:
552 | return 0 # avoid exception caused by division by zero
553 |
554 | p_acc = 1048576 - adc_P
555 | p_acc = (((p_acc<<31) - var2)*3125)//var1
556 |
557 | var1 = ((self.calibration["dig_P9"]) * (p_acc>>13) * (p_acc>>13)) >> 25
558 | var2 = ((self.calibration["dig_P8"]) * p_acc) >> 19
559 | p_acc = ((p_acc + var1 + var2) >> 8) + ((self.calibration["dig_P7"])<<4)
560 |
561 | return p_acc / 256.0
562 |
563 | pressure = property(read_pressure)
564 |
565 | #----------------------------------------------------------------
566 | # Sets the internal variable _referencePressure so the
567 | def set_reference_pressure(self, refPressure):
568 | """!
569 | Sets the referance pressure for the sensor measurments.
570 |
571 | @param refPressure: The referance pressure to use.
572 |
573 | @return No return value
574 | """
575 | self._referencePressure = float(refPressure)
576 |
577 | # Return the local reference pressure
578 | def get_reference_pressure(self):
579 | """!
580 | Get the current reference pressure for the sensor.
581 |
582 | @return **float** The current reference pressure.
583 | """
584 | return self._referencePressure
585 |
586 | reference_pressure = property(get_reference_pressure, set_reference_pressure)
587 |
588 | #----------------------------------------------------------------
589 | def get_altitude_meters( self ):
590 | """!
591 | Return the current Altitude in meters
592 |
593 | @return **float** The current altitude in meters
594 | """
595 |
596 | return (-44330.77)*(math.pow((self.pressure/self._referencePressure), 0.190263) - 1.0) # Corrected, see issue 30
597 |
598 | altitude_meters = property(get_altitude_meters)
599 |
600 | #----------------------------------------------------------------
601 | def get_altitude_feet( self ):
602 | """!
603 | Return the current Altitude in feet
604 |
605 | @return **float** The current altitude in feets
606 | """
607 | return self.get_altitude_meters() * 3.28084
608 |
609 | altitude_feet = property(get_altitude_feet)
610 |
611 |
612 | # ****************************************************************************#
613 | #
614 | # Humidity Section
615 | #
616 | # ****************************************************************************#
617 | def read_humidity( self ):
618 | """!
619 | Returns humidity in %RH.
620 |
621 | @return **float** The current humidity value
622 | """
623 |
624 | # Read temperature to update t_fine
625 | self.get_temperature_celsius()
626 |
627 | data_buffer = self._i2c.readBlock(self.address, self.BME280_HUMIDITY_MSB_REG, 2)
628 | adc_H = (data_buffer[0] << 8) | data_buffer[1]
629 |
630 | var1 = (self.t_fine - 76800)
631 | var1 = (((((adc_H << 14) - ((self.calibration["dig_H4"]) << 20) - ((self.calibration["dig_H5"]) * var1)) + \
632 | (16384)) >> 15) * (((((((var1 * (self.calibration["dig_H6"])) >> 10) * (((var1 * (self.calibration["dig_H3"])) >> 11) + (32768))) >> 10) + (2097152)) * \
633 | (self.calibration["dig_H2"]) + 8192) >> 14))
634 | var1 = (var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * (self.calibration["dig_H1"])) >> 4))
635 | var1 = 0 if var1 < 0 else var1
636 | var1 = 419430400 if var1 > 419430400 else var1
637 |
638 |
639 | return (var1>>12) / 1024.0
640 |
641 | humidity = property(read_humidity)
642 |
643 | # ****************************************************************************#
644 | #
645 | # Temperature Section
646 | #
647 | # ****************************************************************************#
648 |
649 | def get_temperature_celsius( self ):
650 | """!
651 | Returns temperature in DegC and updates t_fine (needed for accurate
652 | pressure and humidity measurements)
653 |
654 | @return **float** The current temperature in C.
655 | """
656 |
657 | # get the reading (adc_T);
658 |
659 | data_buffer = self._i2c.readBlock(self.address, self.BME280_TEMPERATURE_MSB_REG, 3)
660 | adc_T = (data_buffer[0] << 12) | (data_buffer[1] << 4) | ((data_buffer[2] >> 4) & 0x0F)
661 |
662 | # By datasheet, calibrate
663 |
664 | var1 = ((((adc_T>>3) - (self.calibration["dig_T1"]<<1))) * (self.calibration["dig_T2"])) >> 11
665 | var2 = (((((adc_T>>4) - (self.calibration["dig_T1"])) * ((adc_T>>4) - (self.calibration["dig_T1"]))) >> 12) * \
666 | (self.calibration["dig_T3"])) >> 14
667 | self.t_fine = var1 + var2
668 | output = (self.t_fine * 5 + 128) >> 8
669 |
670 | return output / 100 + _settings["tempCorrection"]
671 |
672 | temperature_celsius = property(get_temperature_celsius)
673 |
674 | #----------------------------------------------------------------
675 | def get_temperature_fahrenheit( self ):
676 | """!
677 | Returns temperature in Deg F and updates t_fine (needed for accurate
678 | pressure and humidity measurements)
679 |
680 | @return **float** The current temperature in F.
681 | """
682 | output = self.temperature_celsius
683 | return (output * 9) / 5 + 32
684 |
685 | temperature_fahrenheit = property(get_temperature_fahrenheit)
686 |
687 | # ****************************************************************************#
688 | #
689 | # Dew point Section
690 | #
691 | # ****************************************************************************#
692 | # Returns Dew point in DegC
693 |
694 | def get_dewpoint_celsius(self):
695 | """!
696 | Returns the Dew point in degrees C.
697 |
698 | @return **float** The current dewpoint in C.
699 | """
700 | celsius = self.get_temperature_celsius()
701 | humidity = self.read_humidity()
702 | # (1) Saturation Vapor Pressure = ESGG(T)
703 |
704 | RATIO = 373.15 / (273.15 + celsius)
705 | RHS = -7.90298 * (RATIO - 1)
706 | RHS += 5.02808 * math.log10(RATIO)
707 | RHS += -1.3816e-7 * (math.pow(10, (11.344 * (1 - 1/RATIO ))) - 1)
708 | RHS += 8.1328e-3 * (math.pow(10, (-3.49149 * (RATIO - 1))) - 1)
709 | RHS += math.log10(1013.246)
710 | # factor -3 is to adjust units - Vapor Pressure SVP * humidity
711 | VP = math.pow(10, RHS - 3) * humidity
712 | # (2) DEWPOINT = F(Vapor Pressure)
713 | T = math.log(VP/0.61078) # temp var
714 | return (241.88 * T) / (17.558 - T)
715 |
716 | dewpoint_celsius = property(get_dewpoint_celsius)
717 |
718 | #----------------------------------------------------------------
719 | # Returns Dew point in DegF
720 | def get_dewpoint_fahrenheit(self):
721 | """!
722 | Returns the Dew point in degrees F.
723 |
724 | @return **float** The current dewpoint in F.
725 | """
726 | return self.get_dewpoint_celsius() * 1.8 + 32 # Convert C to F
727 |
728 | dewpoint_fahrenheit = property(get_dewpoint_fahrenheit)
729 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | qwiic_i2c
--------------------------------------------------------------------------------