A Prometheus exporter for DHT22/AM2302 temperature and humidity sensors designed for Raspberry Pi g This repository contains a production-ready Prometheus exporter for the DHT22/AM2302 temperature and humidity sensors, optimized for use on Raspberry Pi devices. Built with Go, it provides reliable metrics collection with proper error handling and retry logic.
Before you begin, ensure you have the necessary tools and dependencies installed:
- Go 1.16 or later: Required for building the project with Go modules:
sudo apt-get install golang- Make: Required for building the source code:
sudo apt install makeFollow these steps to install the DHT Prometheus Exporter on your Raspberry Pi:
- Clone the repository:
git clone https://github.com/guivin/dht-prometheus-exporter.git
cd dht-prometheus-exporter- Build the project:
make buildOr build manually:
go build -o dht-prometheus-exporter ./cmd/dht-prometheus-exporter- Install the binary (optional):
make install- Create a dedicated system user that belongs to the gpio group (for GPIO pin access):
useradd --user-group --groups gpio --no-create-home --system --shell /usr/sbin/nologin dht-prometheus-exporter
- Set up the configuration file. Copy the example configuration file and modify it according to your needs:
sudo cp examples/dht-prometheus-exporter.yml /etc/dht-prometheus-exporter.yml
sudo chown dht-prometheus-exporter:dht-prometheus-exporter /etc/dht-prometheus-exporter.yml
sudo chmod 0640 /etc/dht-prometheus-exporter.ymlEdit /etc/dht-prometheus-exporter.yml to configure:
name: Sensor name for metrics labelsgpio_pin: GPIO pin number where DHT22 is connectedmax_retries: Number of retry attempts for sensor readslisten_port: HTTP port for metrics endpoint (default: 8080)log_level: Logging level (debug, info, warn, error)temperature_unit: celsius or fahrenheit
- Integrate with systemd for easy service management:
sudo cp examples/dht-prometheus-exporter.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable dht-prometheus-exporter
sudo systemctl start dht-prometheus-exporterCheck service status:
sudo systemctl status dht-prometheus-exporterThe exporter exposes the following HTTP endpoints:
| Endpoint | Description |
|---|---|
/metrics |
Prometheus metrics endpoint |
/health |
Health check endpoint (returns 200 OK) |
/ready |
Readiness check endpoint (returns 200 OK) |
Retrieve the metrics from the exporter by querying the designated HTTP endpoint (adjust the port if your configuration differs):
curl http://localhost:8080/metricsThis command will output the current readings from your DHT22/AM2302 sensors, making the data available for Prometheus scraping and subsequent analysis or visualization.
Add the following to your prometheus.yml to scrape metrics from the exporter:
scrape_configs:
- job_name: 'dht'
static_configs:
- targets: ['raspberry-pi:8080']
scrape_interval: 30s| Metric | Type | Description | Labels |
|---|---|---|---|
dht_temperature_degree |
Gauge | Current temperature reading | dht_name, hostname, gpio, unit |
dht_humidity_percent |
Gauge | Current humidity reading | dht_name, hostname, gpio |
Run the test suite:
make testGenerate test coverage report:
make test-coverageThis will create coverage.html that you can open in a browser to see detailed coverage information.
The project follows standard Go conventions:
dht-prometheus-exporter/
├── cmd/
│ └── dht-prometheus-exporter/ # Application entry point
├── internal/ # Internal packages
│ ├── config/ # Configuration management
│ ├── sensor/ # DHT sensor interface and implementation
│ ├── collector/ # Prometheus collector
│ └── logger/ # Logging configuration
├── examples/ # Example configuration files
│ ├── dht-prometheus-exporter.yml # Example config file
│ └── dht-prometheus-exporter.service # Example systemd service
├── testdata/ # Test data files
├── go.mod # Go module definition
└── Makefile # Build automation
# Build the binary
make build
# Run tests
make test
# Clean build artifacts
make clean
# Tidy Go modules
make mod-tidyIf you see "permission denied" errors when accessing GPIO pins:
# Ensure the user running the exporter is in the gpio group
sudo usermod -a -G gpio dht-prometheus-exporter
# Restart the service
sudo systemctl restart dht-prometheus-exporterDHT22 sensors can occasionally fail to read. The exporter has built-in retry logic controlled by max_retries in the configuration. If you see frequent failures:
- Check the wiring connections between the Raspberry Pi and the DHT22 sensor
- Ensure proper pull-up resistor (4.7k-10k ohm) on the data line
- Increase
max_retriesin the configuration - Check the sensor is receiving proper 3.3V power
Check the service logs for errors:
sudo journalctl -u dht-prometheus-exporter -fCommon issues:
- Configuration file not found or invalid YAML
- Port already in use (change
listen_portin config) - Missing GPIO permissions
If /metrics returns no DHT metrics:
- Verify the sensor is properly connected
- Check GPIO pin number matches your wiring
- Review logs for sensor read errors
- Test with debug log level: set
log_level: debugin config
