This repository was archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdht22_exporter.py
More file actions
executable file
·64 lines (46 loc) · 2.07 KB
/
dht22_exporter.py
File metadata and controls
executable file
·64 lines (46 loc) · 2.07 KB
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
#!/usr/bin/env python3
# Prometheus exporter for DHT22 running on raspberrypi
# Usage: ./dht22_exporter -g <gpio_pin_number> -i <poll_time_in_seconds> [-a <exposed_address> -p <exposed_port>]
# Ex: ./dht22_exporter -g 4 -i 2
import time
import argparse
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):
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():
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--gpio", dest="gpio", type=int,
required=True, help="GPIO pin number on which the sensor is plugged in")
parser.add_argument("-a", "--address", dest="addr", type=str, default=None,
required=False, help="Address that will be exposed")
parser.add_argument("-i", "--interval", dest="interval", type=int,
required=True, help="Interval sampling time, in seconds")
parser.add_argument("-p", "--port", dest="port", type=int, default=8001,
required=False, help="Port that will be exposed")
args = parser.parse_args()
if args.addr is not None:
start_http_server(args.port, args.addr)
else:
start_http_server(args.port)
while True:
read_sensor(pin=args.gpio)
time.sleep(args.interval)
main()