Cache the BME280 object instead of initializing it on each get_measurement call
The BME280 needs some warmup time before picking up the results.
This commit is contained in:
parent
e0351421ad
commit
693b38ef51
1 changed files with 13 additions and 5 deletions
|
@ -19,9 +19,21 @@ class GpioSensorBme280Plugin(GpioSensorPlugin):
|
||||||
|
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self._bus = None
|
||||||
|
self._device = None
|
||||||
|
|
||||||
# noinspection PyPackageRequirements
|
# noinspection PyPackageRequirements
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
|
def _get_device(self):
|
||||||
|
if self._device:
|
||||||
|
return self._device
|
||||||
|
|
||||||
|
from smbus import SMBus
|
||||||
|
from bme280 import BME280
|
||||||
|
|
||||||
|
self._bus = SMBus(self.port)
|
||||||
|
self._device = BME280(i2c_dev=self._bus)
|
||||||
|
|
||||||
@action
|
@action
|
||||||
def get_measurement(self):
|
def get_measurement(self):
|
||||||
"""
|
"""
|
||||||
|
@ -35,11 +47,7 @@ class GpioSensorBme280Plugin(GpioSensorPlugin):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from smbus import SMBus
|
device = self._get_device()
|
||||||
from bme280 import BME280
|
|
||||||
|
|
||||||
bus = SMBus(self.port)
|
|
||||||
device = BME280(i2c_dev=bus)
|
|
||||||
return {
|
return {
|
||||||
'temperature': device.get_temperature(),
|
'temperature': device.get_temperature(),
|
||||||
'pressure': device.get_pressure()*100,
|
'pressure': device.get_pressure()*100,
|
||||||
|
|
Loading…
Reference in a new issue