-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_sensor.py
77 lines (61 loc) · 2.62 KB
/
read_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import time
from collections import deque
import Adafruit_DHT
from prometheus_client import Gauge, start_http_server
DEBUG = os.getenv('DEBUG', 'false') == 'true'
SENSOR_PIN = int(os.getenv('SENSOR_PIN', '4'))
TIME_BETWEEN_READINGS = os.getenv('TIME_BETWEEN_READINGS')
TEMPERATURES = deque()
HUMIDITIES = deque()
SENSOR = Adafruit_DHT.DHT22
TEMPERATURE_GAUGE = Gauge('ambient_temperature', 'Ambient temperature')
HUMIDITY_GAUGE = Gauge('ambient_humidity', 'Ambient humidity')
def set_average_readings(temperature, humidity):
"""
Sets the temperature and humidity gauges with the average of the last 5 readings
when more than 5 readings have been made.
The function drops the minimum and maximum values, and averages the remaining.
"""
try:
if 0 <= humidity <= 100:
TEMPERATURES.appendleft(temperature)
HUMIDITIES.appendleft(humidity)
average_temperature = temperature
average_humidity = humidity
if len(TEMPERATURES) > 5:
TEMPERATURES.pop()
tmp_temperatures = list(TEMPERATURES)
# Remove min/max values
tmp_temperatures.remove(max(tmp_temperatures))
tmp_temperatures.remove(min(tmp_temperatures))
# Get the average
average_temperature = sum(tmp_temperatures)/len(tmp_temperatures)
if len(HUMIDITIES) > 5:
HUMIDITIES.pop()
tmp_humidities = list(HUMIDITIES)
# Remove min/max values
tmp_humidities.remove(max(tmp_humidities))
tmp_humidities.remove(min(tmp_humidities))
# Get the average
average_humidity = sum(tmp_humidities)/len(tmp_humidities)
TEMPERATURE_GAUGE.set(average_temperature)
HUMIDITY_GAUGE.set(average_humidity)
else:
if DEBUG:
print(f'Ignoring reading outside expected range: {temperature}°C, {humidity}%')
except TypeError:
if DEBUG:
print(f'Ignoring bad reading: {temperature}°C, {humidity}%')
if __name__ == '__main__':
start_http_server(1006)
while True:
try:
HUMIDITY, TEMPERATURE = Adafruit_DHT.read_retry(SENSOR, SENSOR_PIN)
set_average_readings(TEMPERATURE, HUMIDITY)
except (ImportError, RuntimeError) as exception:
TEMPLATE = 'An exception of type {0} occurred. Arguments:\n{1!r}'
MESSAGE = TEMPLATE.format(type(exception).__name__, exception.args)
if DEBUG:
print(MESSAGE)
time.sleep(int(TIME_BETWEEN_READINGS))