-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,16 @@ | ||
from carlos.edge.interface.device import AnalogInput, GpioConfig, IoFactory | ||
from carlos.edge.interface.device import GpioConfig, IoFactory | ||
|
||
from ._dhtxx import DHT, DHTType | ||
from ._dhtxx import DHTXX, DHTType | ||
|
||
|
||
class DHT11(AnalogInput): | ||
class DHT11(DHTXX): | ||
"""DHT11 Temperature and Humidity Sensor.""" | ||
|
||
def __init__(self, config: GpioConfig): | ||
|
||
super().__init__(config=config) | ||
|
||
self._dht: DHT | None = None | ||
|
||
def setup(self): | ||
"""Sets up the DHT11 sensor.""" | ||
|
||
self._dht = DHT(dht_type=DHTType.DHT11, pin=self.config.pin) | ||
|
||
def read(self) -> dict[str, float]: | ||
"""Reads the temperature and humidity.""" | ||
|
||
assert self._dht is not None, "The DHT sensor has not been initialized." | ||
|
||
# Reading the DHT sensor is quite unreliable, as the device is not a real-time | ||
# device. Thus, we just try it a couple of times and fail if it does not work. | ||
for i in range(16): | ||
try: | ||
temperature, humidity = self._dht.read() | ||
return { | ||
"temperature": temperature, | ||
"humidity": humidity, | ||
} | ||
except RuntimeError: | ||
pass | ||
|
||
raise RuntimeError("Could not read DHT11 sensor.") | ||
self._dht_type = DHTType.DHT11 | ||
|
||
|
||
IoFactory().register(ptype=__name__, config=GpioConfig, factory=DHT11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from carlos.edge.interface.device import GpioConfig, IoFactory | ||
|
||
from ._dhtxx import DHTXX, DHTType | ||
|
||
|
||
class DHT22(DHTXX): | ||
"""DHT22 Temperature and Humidity Sensor.""" | ||
|
||
def __init__(self, config: GpioConfig): | ||
|
||
super().__init__(config=config) | ||
|
||
self._dht_type = DHTType.DHT22 | ||
|
||
|
||
IoFactory().register(ptype=__name__, config=GpioConfig, factory=DHT22) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters