Skip to content
Snippets Groups Projects
Commit fd21ca7d authored by Laszlo Magyar's avatar Laszlo Magyar Committed by Ellis Percival
Browse files

Add DHT sensor functionality and sensor options framework (#61)

* add DHT sensor functionality

* Add digital input options, to the config
parent 3f8c2b94
Branches
Tags
No related merge requests found
......@@ -16,6 +16,11 @@ I2C Sensors
- LM75 i2c temperature sensor (`lm75`)
DHT22 Sensors
-----------
- DHT11 DHT22 AM2302 temperature/humidity sensor (`lm75`)
Installation
------------
......@@ -112,6 +117,29 @@ sensor_inputs:
module: lm75
interval: 15 #interval in seconds, that a value is read from the sensor and a update is published
digits: 4 # number of digits to be round
sensor_modules:
- name: dht22
module: dht22
sensor_type: AM2302 # can be DHT11, DHT22 or AM2302
pin: 4
sensor_inputs:
- name: dht22_temperature
module: dht22
interval: 10 #interval in seconds, that a value is read from the sensor and a update is published
digits: 4 # number of digits to be round
options:
type: temperature # Can be temperature or humidity
- name: dht22_humidity
module: dht22
interval: 10 #interval in seconds, that a value is read from the sensor and a update is published
digits: 4 # number of digits to be round
options:
type: humidity # Can be temperature or humidity
```
#### SSL/TLS
......
......@@ -173,6 +173,15 @@ digital_inputs:
type: boolean
required: no
default: no
options:
type: dict
default: {}
required: no
allow_unknown: yes
schema:
type:
type: string
required: no
digital_outputs:
type: list
......
......@@ -248,6 +248,15 @@ sensor_inputs:
required: no
default: 2
min: 0
options:
type: dict
default: {}
required: no
allow_unknown: yes
schema:
type:
type: string
required: no
logging:
type: dict
......
......@@ -61,7 +61,7 @@ class GenericSensor(object):
pass
@abc.abstractmethod
def get_value(self, sensor):
def get_value(self, sensor, options):
pass
def cleanup(self):
......
from pi_mqtt_gpio.modules import GenericSensor
REQUIREMENTS = ("Adafruit_DHT",)
CONFIG_SCHEMA = {
"pin": {"type": "integer", "required": True, "empty": False},
"sensor_type": {"type": "string", "required": True, "empty": False}
}
class Sensor(GenericSensor):
"""
Implementation of Sensor class for the DHT22 temperature sensor.
"""
def __init__(self, config):
import Adafruit_DHT as DHTsensor
sensor_type = config["sensor_type"].lower()
if sensor_type == 'dht22':
self.sensor_type = DHTsensor.DHT22
elif sensor_type == 'dht11':
self.sensor_type = DHTsensor.dht11
elif sensor_type == 'am2302':
self.sensor_type = DHTsensor.AM2302
else:
raise Exception('Supported sensor types: DHT22, DHT11, AM2302')
self.pin = config["pin"]
self.sensor = DHTsensor
def setup_sensor(self, config):
return True # nothing to do here
def get_value(self, sensor, options):
"""get the temperature or humidity value from the sensor"""
humidity, temperature = self.sensor.read_retry(self.sensor_type, self.pin)
if humidity is not None and temperature is not None:
if options["type"] == "temperature" :
return temperature
if options["type"] == "humidity" :
return humidity
return float('NAN')
......@@ -504,7 +504,7 @@ def sensor_timer_thread(SENSOR_MODULES, sensor_inputs, topic_prefix):
sensor = SENSOR_MODULES[sens_conf["module"]]
try:
value = round(sensor.get_value(sensor), sens_conf["digits"])
value = round(sensor.get_value(sensor,sens_conf["options"]), sens_conf["digits"])
_LOG.info(
"sensor_timer_thread: reading sensor '%s' value %r",
......@@ -526,8 +526,9 @@ def sensor_timer_thread(SENSOR_MODULES, sensor_inputs, topic_prefix):
)
# schedule next call
next_call = next_call + cycle_time # every cycle_time sec
sleep(next_call - time())
sleep(max(0, next_call - time()))
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment