@@ -83,8 +83,6 @@ def __init__(self, settings):
83
83
GPIO .setmode (GPIO .BCM )
84
84
GPIO .setwarnings (False )
85
85
if settings .sensors : # is there at least one sensor in yaml file
86
- global W1ThermSensor # for DS18B20 temperature sensor
87
- from w1thermsensor import W1ThermSensor
88
86
self .setup_sensors (settings )
89
87
if settings .lights : # is there at least one light in yaml file
90
88
self .setup_lights (settings )
@@ -451,6 +449,10 @@ def __init__(self, sensor, sensors, settings, tiny_image, send_q):
451
449
self .type = sensors [sensor ]['type' ]
452
450
else :
453
451
self .type = 'Unknown'
452
+ if 'unit' in sensors [sensor ]:
453
+ self .unit = sensors [sensor ]['unit' ].upper ()
454
+ else :
455
+ self .unit = 'F'
454
456
if 'read_interval_minutes' in sensors [sensor ]:
455
457
self .interval = sensors [sensor ]['read_interval_minutes' ]
456
458
else :
@@ -469,27 +471,62 @@ def __init__(self, sensor, sensors, settings, tiny_image, send_q):
469
471
# self.event_text will have self.current_reading appended when events are sent
470
472
self .event_text = '|' .join ([settings .nodename , self .name ]).strip ()
471
473
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
473
480
if self .type == 'DS18B20' :
481
+ global W1ThermSensor # for DS18B20 temperature sensor
482
+ from w1thermsensor import W1ThermSensor
474
483
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 :
476
494
self .check_temperature () # check one time, then start interval_timer
477
495
threading .Thread (daemon = True ,
478
496
target = lambda : interval_timer (
479
497
self .interval , self .check_temperature )).start ()
480
498
481
499
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
483
501
"""
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 :
486
515
# temperature has changed from last reported temperature, therefore
487
516
# send an event message reporting temperature by appending to send_q
488
- temp_text = str (temperature ) + " F"
517
+ temp_text = str (temperature ) + " " + self . unit
489
518
text = '|' .join ([self .event_text , temp_text ])
490
519
text_and_image = (text , self .tiny_image )
491
520
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
493
530
494
531
class Light :
495
532
""" Methods and attributes of a light controlled by an RPi GPIO pin
0 commit comments