Skip to content

Instantly share code, notes, and snippets.

@leventyalcin
Created January 17, 2021 21:18
Show Gist options
  • Save leventyalcin/da96d3ebb4db2487274b254f584dfdb5 to your computer and use it in GitHub Desktop.
Save leventyalcin/da96d3ebb4db2487274b254f584dfdb5 to your computer and use it in GitHub Desktop.
DHT data as Prometheus metrics
#!/usr/bin/env python3
import sys
import Adafruit_DHT
from prometheus_client import start_http_server, Gauge
import time
import logging
import logging.handlers
logging.basicConfig(level=logging.NOTSET)
t = Gauge('temperature', 'Temperature', ['room', 'location'])
h = Gauge('humidity', 'Humidity', ['room', 'location'])
logging.info('Starting exporter daemon')
start_http_server(9000)
while True:
logging.info('Reading the sensor')
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
t.labels(room='living', location='home').set(temperature)
h.labels(room='living', location='home').set(humidity)
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment