Create a Golang Prometheus exporter for DHT22/AM2302 sensor. Configure Prometheus to collect metrics and visualize them in Grafana

In this article, we will set up a DHT22/AM2302 sensor on a Raspberry Pi. This sensor connects to a GPIO pin and reports temperature and humidity data. We’ll see how to connect it to the Raspberry Pi and confirm the data acquisition.

Wouldn’t it be better to centralize all our metrics in one place? Well, we’ll code a Prometheus exporter using the Golang language. Prometheus is an open-source project for monitoring and alerting using a time-series database.

An exporter exposes the metrics via an HTTP server using the expected format. The Prometheus server calls the exporter to collect and stores the metrics. You can then query and define alerts from the metrics. Prometheus is pluggable to other systems as Grafana to create metric visualizations.

Alright! This is exactly what we’ll do here finally. After installing the exporter, we’ll configure Prometheus and Grafana to work together. We’ll able to centralize the metrics and visualize them in a beautiful dashboard.

Configure the AM2302 Sensor on the Raspberry Pi

The AM2302 sensor

The AM2302 is a wired version of the DHT22 sensor.  From the datasheet it presents the following characteristics:

  • Low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 0–100% humidity readings with 2–5% accuracy
  • Good for -40 to 80°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1" spacing

#technology #golang #programming

How to Build a Prometheus Exporter for Sensor Monitoring
5.90 GEEK