Prometheus Endpoint, written in Python to read DHT11 1wire sensor and exposes temperature values as a prometheus metric.
I bought the sensor from Amazn which is shipped with the following board attached
We need three Pins from the Raspberry Pi GPIO board:
VCC will be attached to the voltage pin. Sometimes, 3.3V wasn't enough so 5V should be the way to go.
You can refer to the following wiring as an example:
board pin | raspberry pin |
---|---|
VCC | Pin2, 5V Power |
DATA | Pin7, GPIO4 (GPCLK0) |
Ground | Pin6, Ground |
Have a look at the sourcecode for details. Generally, you'll have to download and import the required python libraries. Refer to the official documentation on how to implement a prometheus exporter: https://github.com/prometheus/client_python.
You'll need to install python (I recommend python3) to prepare your local environment:
$ sudo apt-get update
$ sudo apt-get install python3-pip
$ sudo python3 -m pip install --upgrade pip setuptools wheel
$ sudo pip3 install prometheus_client Adafruit_DHT
$ python3 exporter.py --< args[] >
I've used hypriot os with a RaspberryPi 3B+. It works on a Raspberry Pi 2 too, although docker builds might take some time, so be calm to your Pi.
python3 src/exporter.py --help
usage: exporter.py [-h] [-n NODE] [-p PORT] [-i INTERVAL] [-r RETRIES]
[-g GPIOPIN] [-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
Prometheus DHT22 sensor exporter
optional arguments:
-h, --help show this help message and exit
-n NODE, --node NODE The node, the exporter runs on
-p PORT, --port PORT The port, the exporter runs on
-i INTERVAL, --interval INTERVAL
The sleep interval of the exporter
-r RETRIES, --retries RETRIES
The number of read retries for accurate values
-g GPIOPIN, --gpiopin GPIOPIN
The GPIO pin, where the sensor is connected to
-l {DEBUG,INFO,WARNING,ERROR,CRITICAL}, --loglevel {DEBUG,INFO,WARNING,ERROR,CRITICAL}
Set the logging level
A sample local run can be the following: python3 exporter.py --node=localhost --port=9103 --interval=10 --gpiopin=4 --loglevel=DEBUG
You can run the exporter either via python itself or in a docker container. The required commands for running it via python are also in the supplied Makefile. For docker use:
$ docker build -t lukasbahr/raspbi-dht22-exporter:<VERSION> -f Dockerfile .
$ docker tag lukasbahr/raspbi-dht22-exporter:<VERSION> lukasbahr/raspbi-dht22-exporter:<VERSION>
$ docker run -it -p 9103:9103 lukasbahr/raspbi-dht22-exporter:<VERSION>
or refer to the supplied Makefile.
You can also download it from docker hub via docker pull lukasbahr/raspbi-dht22-exporter:<VERSION>
- ❌ Add CI/CD Support. The ci process must detect the underlying system as a raspberry pi in order to install AdafruitDHT22
- ❌ Add unit tests
- ❌ use buildx to create the proper image
- ❌ Add health metric, error metric, scrape interval, general information about exporter etc.
DHT22 sensors are hard to read. If your scrape interval is too high, you might encounter the following error. This error indicates that the socket connection has been closed before the client did. Increase your scrape interval to 60 or 120 seconds - we all hope, that the temperature does not rapidly change its value within this time.
dht22-exporter-4zzlm dht22-exporter ----------------------------------------
dht22-exporter-4zzlm dht22-exporter Exception happened during processing of request from ('10.42.2.74', 36426)
dht22-exporter-4zzlm dht22-exporter Traceback (most recent call last):
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
dht22-exporter-4zzlm dht22-exporter self.finish_request(request, client_address)
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
dht22-exporter-4zzlm dht22-exporter self.RequestHandlerClass(request, client_address, self)
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
dht22-exporter-4zzlm dht22-exporter self.handle()
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/http/server.py", line 418, in handle
dht22-exporter-4zzlm dht22-exporter self.handle_one_request()
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
dht22-exporter-4zzlm dht22-exporter method()
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/site-packages/prometheus_client/exposition.py", line 159, in do_GET
dht22-exporter-4zzlm dht22-exporter self.wfile.write(output)
dht22-exporter-4zzlm dht22-exporter File "/usr/lib/python3.6/socketserver.py", line 803, in write
dht22-exporter-4zzlm dht22-exporter self._sock.sendall(b)
dht22-exporter-4zzlm dht22-exporter BrokenPipeError: [Errno 32] Broken pipe