mirror of
https://github.com/cmur2/python-bme680.git
synced 2024-12-22 12:54:29 +01:00
added set_temp_offset function and example
This commit is contained in:
parent
40b4b63b93
commit
1c9ba41306
46
examples/temp-offset.py
Normal file
46
examples/temp-offset.py
Normal file
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user