summaryrefslogtreecommitdiff
path: root/temperature_humidity_prometheus_exporter.py
blob: 06d2bec22e6696e6dc1c4b3f086899bb75bbfb80 (plain)
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
import time
import board

import adafruit_dht
import prometheus_client

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D4)


def get_humidity() -> int:
    while True:
        try:
            humidity_r = dhtDevice.humidity
            return humidity_r
        except RuntimeError as error:  # Errors happen fairly often, DHT's are hard to read, just keep going
            print(error.args[0])


def get_temperature() -> int:
    while True:
        try:
            temperature_c = dhtDevice.temperature
            return temperature_c
        except RuntimeError as error:  # Errors happen fairly often, DHT's are hard to read, just keep going
            print(error.args[0])


prometheus_client.REGISTRY.unregister(prometheus_client.GC_COLLECTOR)
prometheus_client.REGISTRY.unregister(prometheus_client.PLATFORM_COLLECTOR)
prometheus_client.REGISTRY.unregister(prometheus_client.PROCESS_COLLECTOR)

temperature_guage = prometheus_client.Gauge('temperature', 'Current temperature, degrees centigrade')
humidity_guage = prometheus_client.Gauge('humidity', 'Current humidity, relative percentage')

temperature_guage.set_function(get_temperature)
humidity_guage.set_function(get_humidity)

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    prometheus_client.start_http_server(8000)
    while True:
        time.sleep(10)