summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--requirements.txt2
-rw-r--r--temperature_humidity_prometheus_exporter.py43
2 files changed, 45 insertions, 0 deletions
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..0f3b91c
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+adafruit-circuitpython-dht~=.0
+prometheus-client~=0.20 \ No newline at end of file
diff --git a/temperature_humidity_prometheus_exporter.py b/temperature_humidity_prometheus_exporter.py
new file mode 100644
index 0000000..06d2bec
--- /dev/null
+++ b/temperature_humidity_prometheus_exporter.py
@@ -0,0 +1,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)