Skip to content

Commit 9a2200c

Browse files
authored
Update imaging.py
Added DHT11 and DHT22 Temp/Humidity Sensors to file. Added 'unit' parameter to 'sensors' section of YAML file. User can select Centigrade or Fahrenheit output. Note: There is currently an issue with these sensors and the adafruit_dht module "Error: A full buffer was not returned. Try again" Refn: adafruit/Adafruit_CircuitPython_DHT#33
1 parent c7f06a8 commit 9a2200c

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

imagenode/tools/imaging.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ def __init__(self, settings):
8383
GPIO.setmode(GPIO.BCM)
8484
GPIO.setwarnings(False)
8585
if settings.sensors: # is there at least one sensor in yaml file
86-
global W1ThermSensor # for DS18B20 temperature sensor
87-
from w1thermsensor import W1ThermSensor
8886
self.setup_sensors(settings)
8987
if settings.lights: # is there at least one light in yaml file
9088
self.setup_lights(settings)
@@ -451,6 +449,10 @@ def __init__(self, sensor, sensors, settings, tiny_image, send_q):
451449
self.type = sensors[sensor]['type']
452450
else:
453451
self.type = 'Unknown'
452+
if 'unit' in sensors[sensor]:
453+
self.unit = sensors[sensor]['unit'].upper()
454+
else:
455+
self.unit = 'F'
454456
if 'read_interval_minutes' in sensors[sensor]:
455457
self.interval = sensors[sensor]['read_interval_minutes']
456458
else:
@@ -469,27 +471,62 @@ def __init__(self, sensor, sensors, settings, tiny_image, send_q):
469471
# self.event_text will have self.current_reading appended when events are sent
470472
self.event_text = '|'.join([settings.nodename, self.name]).strip()
471473

472-
# TODO add other sensor types as testing is completed for each sensor type
474+
# Initialize last_reading and temp_sensor variables
475+
self.last_reading_temp = -999 # will ensure first temp reading is a change
476+
self.last_reading_humidity = -999 # will ensure first humidity reading is a change
477+
self.temp_sensor = None
478+
479+
# Sensor types
473480
if self.type == 'DS18B20':
481+
global W1ThermSensor # for DS18B20 temperature sensor
482+
from w1thermsensor import W1ThermSensor
474483
self.temp_sensor = W1ThermSensor()
475-
self.last_reading = -999 # will ensure first reading is a change
484+
485+
if (self.type == 'DHT11') or (self.type == 'DHT22'):
486+
global adafruit_dht # for DHT11 & DHT22 temperature sensor
487+
import adafruit_dht
488+
if self.type == 'DHT11':
489+
self.temp_sensor = adafruit_dht.DHT11(self.gpio)
490+
if self.type == 'DHT22':
491+
self.temp_sensor = adafruit_dht.DHT22(self.gpio)
492+
493+
if self.temp_sensor != None:
476494
self.check_temperature() # check one time, then start interval_timer
477495
threading.Thread(daemon=True,
478496
target=lambda: interval_timer(
479497
self.interval, self.check_temperature)).start()
480498

481499
def check_temperature(self):
482-
""" adds temperature value from a sensor to senq_q message queue
500+
""" adds temperature & humidity (if available) value from a sensor to senq_q message queue
483501
"""
484-
temperature = int(self.temp_sensor.get_temperature(W1ThermSensor.DEGREES_F))
485-
if abs(temperature - self.last_reading) >= self.min_difference:
502+
if self.type == 'DS18B20':
503+
if self.unit == 'C':
504+
temperature = int(self.temp_sensor.get_temperature(W1ThermSensor.DEGREES_C))
505+
else:
506+
temperature = int(self.temp_sensor.get_temperature(W1ThermSensor.DEGREES_F))
507+
humidity = -999
508+
if (self.type == 'DHT11') or (self.type == 'DHT22'):
509+
if self.unit == 'C':
510+
temperature = self.temp_sensor.temperature
511+
else:
512+
temperature = self.temp_sensor.temperature * (9 / 5) + 32
513+
humidity = self.temp_sensor.humidity
514+
if abs(temperature - self.last_reading_temp) >= self.min_difference:
486515
# temperature has changed from last reported temperature, therefore
487516
# send an event message reporting temperature by appending to send_q
488-
temp_text = str(temperature) + " F"
517+
temp_text = str(temperature) + " " + self.unit
489518
text = '|'.join([self.event_text, temp_text])
490519
text_and_image = (text, self.tiny_image)
491520
self.send_q.append(text_and_image)
492-
self.last_reading = temperature
521+
self.last_reading_temp = temperature
522+
if abs(humidity - self.last_reading_humidity) >= self.min_difference:
523+
# humidity has changed from last reported humidity, therefore
524+
# send an event message reporting humidity by appending to send_q
525+
humidity_text = str(humidity) + " %"
526+
text = '|'.join([self.event_text, humidity_text])
527+
text_and_image = (text, self.tiny_image)
528+
self.send_q.append(text_and_image)
529+
self.last_reading_humidity = humidity
493530

494531
class Light:
495532
""" Methods and attributes of a light controlled by an RPi GPIO pin

0 commit comments

Comments
 (0)