From 1c9ba41306a90954a3c497160bed88ab3d8bd0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20L=C3=BChr?= Date: Mon, 28 May 2018 21:18:20 +0000 Subject: [PATCH 1/8] added set_temp_offset function and example --- examples/temp-offset.py | 46 ++++++++++++++++++++++++++++++++++++++ library/bme680/__init__.py | 17 ++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 examples/temp-offset.py diff --git a/examples/temp-offset.py b/examples/temp-offset.py new file mode 100644 index 0000000..15bb220 --- /dev/null +++ b/examples/temp-offset.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +import bme680 + +print("""Display Temperature, Pressure and Humidity with different offsets. +""") + +sensor = bme680.BME680() + +# These oversampling settings can be tweaked to +# change the balance between accuracy and noise in +# the data. + +sensor.set_humidity_oversample(bme680.OS_2X) +sensor.set_pressure_oversample(bme680.OS_4X) +sensor.set_temperature_oversample(bme680.OS_8X) +sensor.set_filter(bme680.FILTER_SIZE_3) + +print("Initial readings") +sensor.get_sensor_data() +output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) +print(output) + +print("SET offset 4 degrees celsius") +sensor.set_temp_offset(4) +sensor.get_sensor_data() +output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) +print(output) + +print("SET offset -10 degrees celsius") +sensor.set_temp_offset(-10) +sensor.get_sensor_data() +output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) +print(output) + +print("SET offset -100 degrees celsius") +sensor.set_temp_offset(-100) +sensor.get_sensor_data() +output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) +print(output) + +print("SET offset 0 degrees celsius") +sensor.set_temp_offset(0) +sensor.get_sensor_data() +output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) +print(output) + diff --git a/library/bme680/__init__.py b/library/bme680/__init__.py index ea19373..5937012 100644 --- a/library/bme680/__init__.py +++ b/library/bme680/__init__.py @@ -36,7 +36,7 @@ class BME680(BME680Data): self.set_temperature_oversample(OS_8X) self.set_filter(FILTER_SIZE_3) self.set_gas_status(ENABLE_GAS_MEAS) - + self.set_temp_offset(0) self.get_sensor_data() def _get_calibration_data(self): @@ -56,6 +56,19 @@ class BME680(BME680Data): self._set_regs(SOFT_RESET_ADDR, SOFT_RESET_CMD) time.sleep(RESET_PERIOD / 1000.0) + def set_temp_offset(self, value): + """Set temperature offset in celsius + + If set, the temperature t_fine will be increased by given value in celsius. + :param value: Temperature offset in Celsius, eg. 4, -8 + """ + if value == 0: + self.offset_temp_in_t_fine = 0 + elif value < 0: + self.offset_temp_in_t_fine = -int((((abs(value) * 100) << 8) - 128) / 5) + else: + self.offset_temp_in_t_fine = int((((abs(value) * 100) << 8) - 128) / 5) + def set_humidity_oversample(self, value): """Set humidity oversampling @@ -293,7 +306,7 @@ class BME680(BME680Data): var3 = ((var3) * (self.calibration_data.par_t3 << 4)) >> 14 # Save teperature data for pressure calculations - self.calibration_data.t_fine = (var2 + var3) + self.calibration_data.t_fine = (var2 + var3) + self.offset_temp_in_t_fine calc_temp = (((self.calibration_data.t_fine * 5) + 128) >> 8) return calc_temp From ae004aa62bbd7c5a775db8042b99e761f6ea817a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20L=C3=BChr?= Date: Mon, 28 May 2018 21:38:19 +0000 Subject: [PATCH 2/8] accept float values in set_temp_offset --- library/bme680/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/bme680/__init__.py b/library/bme680/__init__.py index 5937012..865fe40 100644 --- a/library/bme680/__init__.py +++ b/library/bme680/__init__.py @@ -65,9 +65,9 @@ class BME680(BME680Data): if value == 0: self.offset_temp_in_t_fine = 0 elif value < 0: - self.offset_temp_in_t_fine = -int((((abs(value) * 100) << 8) - 128) / 5) + self.offset_temp_in_t_fine = -int((((int(abs(value) * 100)) << 8) - 128) / 5) else: - self.offset_temp_in_t_fine = int((((abs(value) * 100) << 8) - 128) / 5) + self.offset_temp_in_t_fine = int((((int(abs(value) * 100)) << 8) - 128) / 5) def set_humidity_oversample(self, value): """Set humidity oversampling From 8cb0dead6b7f071325112f81ccba8527d0b00d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20L=C3=BChr?= Date: Mon, 28 May 2018 21:41:16 +0000 Subject: [PATCH 3/8] added temp offset float example --- examples/temp-offset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/temp-offset.py b/examples/temp-offset.py index 15bb220..1dd74c2 100644 --- a/examples/temp-offset.py +++ b/examples/temp-offset.py @@ -26,8 +26,8 @@ sensor.get_sensor_data() output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) print(output) -print("SET offset -10 degrees celsius") -sensor.set_temp_offset(-10) +print("SET offset -1.87 degrees celsius") +sensor.set_temp_offset(-1.87) sensor.get_sensor_data() output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) print(output) From fed191e40e9f58a5002ae12d368fd3f2544b9020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20L=C3=BChr?= Date: Thu, 31 May 2018 08:21:14 +0000 Subject: [PATCH 4/8] use math.copysign for temp offset calculation --- library/bme680/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/bme680/__init__.py b/library/bme680/__init__.py index 865fe40..809128f 100644 --- a/library/bme680/__init__.py +++ b/library/bme680/__init__.py @@ -60,14 +60,12 @@ class BME680(BME680Data): """Set temperature offset in celsius If set, the temperature t_fine will be increased by given value in celsius. - :param value: Temperature offset in Celsius, eg. 4, -8 + :param value: Temperature offset in Celsius, eg. 4, -8, 1.25 """ if value == 0: self.offset_temp_in_t_fine = 0 elif value < 0: - self.offset_temp_in_t_fine = -int((((int(abs(value) * 100)) << 8) - 128) / 5) - else: - self.offset_temp_in_t_fine = int((((int(abs(value) * 100)) << 8) - 128) / 5) + self.offset_temp_in_t_fine = int(math.copysign((((int(abs(value) * 100)) << 8) - 128) / 5, value)) def set_humidity_oversample(self, value): """Set humidity oversampling From bc2f729013d9bb1ddc002205970b10b107ea882f Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 1 Jun 2018 16:04:27 +0100 Subject: [PATCH 5/8] Made temp-offset.py executable --- examples/temp-offset.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 examples/temp-offset.py diff --git a/examples/temp-offset.py b/examples/temp-offset.py old mode 100644 new mode 100755 From 7e08c2d7497cd05328a84bc19fa1c4d5d31c37ec Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 1 Jun 2018 16:04:37 +0100 Subject: [PATCH 6/8] Fixed packaging errors --- library/LICENSE.txt | 21 +++++++++++++++++++++ library/MANIFEST.in | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 library/LICENSE.txt diff --git a/library/LICENSE.txt b/library/LICENSE.txt new file mode 100644 index 0000000..b3e25b2 --- /dev/null +++ b/library/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Pimoroni Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/library/MANIFEST.in b/library/MANIFEST.in index 030f39d..e203acd 100644 --- a/library/MANIFEST.in +++ b/library/MANIFEST.in @@ -2,4 +2,4 @@ include CHANGELOG.txt include LICENSE.txt include README.txt include setup.py -include bme680.py +recursive-include bme680 *.py From 4ae50e2f2c90f40e64f6178a6ac66a34d8cd0393 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 1 Jun 2018 16:10:08 +0100 Subject: [PATCH 7/8] Fixed bug in set_temp_offset --- library/bme680/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/bme680/__init__.py b/library/bme680/__init__.py index 809128f..64d9f16 100644 --- a/library/bme680/__init__.py +++ b/library/bme680/__init__.py @@ -64,7 +64,7 @@ class BME680(BME680Data): """ if value == 0: self.offset_temp_in_t_fine = 0 - elif value < 0: + else: self.offset_temp_in_t_fine = int(math.copysign((((int(abs(value) * 100)) << 8) - 128) / 5, value)) def set_humidity_oversample(self, value): From 0f0be7f2c27ab437d47d6d687eaa6660a5e4a01c Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 1 Jun 2018 16:10:34 +0100 Subject: [PATCH 8/8] Tidied up temp-offset.py example --- examples/temp-offset.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/examples/temp-offset.py b/examples/temp-offset.py index 1dd74c2..3f468b4 100755 --- a/examples/temp-offset.py +++ b/examples/temp-offset.py @@ -15,32 +15,25 @@ sensor.set_pressure_oversample(bme680.OS_4X) sensor.set_temperature_oversample(bme680.OS_8X) sensor.set_filter(bme680.FILTER_SIZE_3) +def display_data(offset=0): + sensor.set_temp_offset(offset) + sensor.get_sensor_data() + output = "{0:.2f} C, {1:.2f} hPa, {2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) + print(output) + print("") + print("Initial readings") -sensor.get_sensor_data() -output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) -print(output) +display_data() print("SET offset 4 degrees celsius") -sensor.set_temp_offset(4) -sensor.get_sensor_data() -output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) -print(output) +display_data(4) print("SET offset -1.87 degrees celsius") -sensor.set_temp_offset(-1.87) -sensor.get_sensor_data() -output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) -print(output) +display_data(-1.87) print("SET offset -100 degrees celsius") -sensor.set_temp_offset(-100) -sensor.get_sensor_data() -output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) -print(output) +display_data(-100) print("SET offset 0 degrees celsius") -sensor.set_temp_offset(0) -sensor.get_sensor_data() -output = "{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) -print(output) +display_data(0)