forked from clintjedwards/dht22_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht22_exporter.py
executable file
·51 lines (34 loc) · 1.24 KB
/
dht22_exporter.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
#!/usr/bin/env python
# Prometheus exporter for DHT22 running on raspberrypi
# Usage: ./dht22_exporter <pin_number> <poll_time_in_seconds>
# Ex: ./dht22_exporter 4 2
import sys
import time
from prometheus_client import Gauge, start_http_server
import Adafruit_DHT
# Create a metric to track time spent and requests made.
dht22_temperature_celsius = Gauge(
'dht22_temperature_celsius', 'Temperature in celsius provided by dht sensor')
dht22_temperature_fahrenheit = Gauge(
'dht22_temperature_fahrenheit', 'Temperature in fahrenheit provided by dht sensor')
dht22_humidity = Gauge(
'dht22_humidity', 'Humidity in percents provided by dht sensor')
SENSOR = Adafruit_DHT.DHT22
def read_sensor():
pin = sys.argv[1]
humidity, temperature = Adafruit_DHT.read_retry(SENSOR, pin)
if humidity is None or temperature is None:
return
if humidity > 200 or temperature > 200:
return
dht22_humidity.set('{0:0.1f}'.format(humidity))
dht22_temperature_fahrenheit.set(
'{0:0.1f}'.format(9.0/5.0 * temperature + 32))
dht22_temperature_celsius.set(
'{0:0.1f}'.format(temperature))
def main():
start_http_server(8001)
while True:
read_sensor()
time.sleep(int(sys.argv[2]))
main()