Skip to content

Commit

Permalink
upgrade DHT11/DHT22 backing library (#297)
Browse files Browse the repository at this point in the history
* upgrade DHT11/DHT22 backing library

Old DHT11/DHT22 backing library Adafruit_DHT is out of maintained and reporting error on my rpi4+python311.
` ImportError: cannot import name 'Raspberry_Pi_2_Driver' from 'Adafruit_DHT'`
And it has CPU usage hogging problem (~25%).

Upgrade DHT11/DHT22 backing library to adafruit-circuitpython-dht

* Suppport sensor bmp085/bmp180
  • Loading branch information
pansila authored Jun 2, 2024
1 parent 25943d4 commit 98bb92e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
51 changes: 51 additions & 0 deletions mqtt_io/modules/sensor/bmp085.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
BMP085 temperature and pressure sensor
"""

from typing import cast

from ...types import CerberusSchemaType, ConfigType, SensorValueType
from . import GenericSensor

REQUIREMENTS = ("Adafruit_BMP",)
CONFIG_SCHEMA: CerberusSchemaType = {
"chip_addr": dict(type="integer", required=True, empty=False),
}
DATA_READER = {
"temperature": lambda bmp: bmp.read_temperature(),
"pressure": lambda bmp: bmp.read_pressure(),
"altitude": lambda bmp: bmp.read_altitude(),
}


class Sensor(GenericSensor):
"""
Implementation of Sensor class for the BME280 sensor.
"""

SENSOR_SCHEMA: CerberusSchemaType = {
"type": dict(
type="string",
required=False,
empty=False,
default="temperature",
allowed=["temperature", "pressure", "altitude"],
)
}

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,attribute-defined-outside-init
# pylint: disable=import-error,no-member
from Adafruit_BMP.BMP085 import BMP085

self.address: int = self.config["chip_addr"]
self.bmp = BMP085(address=self.address)

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
"""
Get the temperature, humidity or pressure value from the sensor
"""
sens_type = sens_conf["type"]
data = DATA_READER[sens_type](self.bmp)
return cast(float, data)

20 changes: 10 additions & 10 deletions mqtt_io/modules/sensor/dht22.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ...types import CerberusSchemaType, ConfigType, PinType, SensorValueType
from . import GenericSensor

REQUIREMENTS = ("Adafruit_DHT",)
REQUIREMENTS = ("adafruit-circuitpython-dht",)
ALLOWED_TYPES = ["dht11", "dht22", "am2302"]
CONFIG_SCHEMA: CerberusSchemaType = {
"pin": dict(type="integer", required=True, empty=False),
Expand Down Expand Up @@ -36,30 +36,30 @@ class Sensor(GenericSensor):

def setup_module(self) -> None:
# pylint: disable=import-outside-toplevel,import-error
import Adafruit_DHT as DHTsensor # type: ignore
import adafruit_dht # type: ignore
from microcontroller import Pin

sensor_type: str = self.config["type"].lower()

self.sensor_type: int
if sensor_type == "dht22":
self.sensor_type = DHTsensor.DHT22
self.sensor = adafruit_dht.DHT22
elif sensor_type == "dht11":
self.sensor_type = DHTsensor.DHT11
self.sensor = adafruit_dht.DHT11
elif sensor_type == "am2302":
self.sensor_type = DHTsensor.AM2302
self.sensor = adafruit_dht.DHT21
else:
raise RuntimeConfigError("Supported sensor types: DHT22, DHT11, AM2302")
raise RuntimeConfigError("Supported sensor types: DHT22, DHT11 and AM2302/DHT21")

self.pin: PinType = self.config["pin"]
self.sensor = DHTsensor
self.pin: PinType = Pin(self.config["pin"])

def get_value(self, sens_conf: ConfigType) -> SensorValueType:
"""
Get the temperature or humidity value from the sensor
"""
humidity: SensorValueType
temperature: SensorValueType
humidity, temperature = self.sensor.read_retry(self.sensor_type, self.pin)
dht_device = self.sensor(self.pin, use_pulseio=False)
humidity, temperature = dht_device.humidity, dht_device.temperature
if sens_conf["type"] == "temperature":
return temperature
if sens_conf["type"] == "humidity":
Expand Down

0 comments on commit 98bb92e

Please sign in to comment.