Monitoring Humidity and Temperature with Grafana, InfluxDB and Orange Pi

Alejandro González
Trabe
Published in
5 min readFeb 22, 2021
https://newcastlebeach.org/images/humidity.jpg

Where I’m from, humidity is a frequent problem at home. So, it’s good to know the current humidity and temperature to be able to control these factors and have a healthier and warmer environment.

To get this information, all we need is a single-board computer, a proper sensor and some code. This story explains how to display this information in a dashboardto get a clearer view of it.

What We Need?

This is the hardware that I use, but there are others that could be used as well.

  • DHT22 sensor: It’s a low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, sending the information through a data pin. This sensor does not need calibration.
  • Orange Pi Zero: It’s an open-source single-board computer. This type of board has a series of pins called GPIOS. These pins allow connecting other electronic devices to the board (such as the DHT22 sensor).

Board and Sensor Connections

We have to connect the sensor to specific pins on the board. This case: Data (for example PA06 pin), + 3.3v and ground. In the image below we can see a diagram of the connections.

https://f1atb.fr/index.php/2020/10/03/dht22-and-orange-pi-zero/

There should be a resistor connected between the sensor and the GPIO pin. The lack of the resistor will not damage anything, but it can give wrong readings. We can solve this in code later, but the best solution would be to add the resistor. To assemble this correctly, you can use this guide.

Orange Pi Configuration

To be able to use our Orange Pi, the first thing we need to do is install an operating system. This operating system must be a Unix distribution to be able to use the software we will need (InfluxDB and Grafana).

In our case we use Armbian. Armbian is a Debian and Ubuntu based operating system for ARM development boards. Armbian documentation explains in detail the installation and configuration process.

InfluxDB Configuration

InfluxDB is a database management system written in Go. This DBMS is optimized for fast, high-availability storage and retrieval of time series data.

A few years back, installing InfluxDB was a challenge. Fortunately, nowadays the company behind InfluxDB has simplified the process. The installation can be done by following this installation guide. Once installed, it is time to make the necessary configuration for our case.

In a real case scenario, it would be necessary to properly configure InfluxDB security, but since this is a home project, we will use admin access. To do this, we have to use InfluxDB CLI, and execute the following query (replacing “pass” with a password of our choice).

CREATE USER admin WITH PASSWORD 'pass' WITH ALL PRIVILEGES

Next step is to enable https authentication. For this, we have to open /etc/influxdb/influxdb.conf file with a text editor and set auth-enabled to true.

To apply the new settings we need to restart InfluxDB service with the following command:

sudo service influxdb restart

To finish off, we will create the database. We need to use the InfluxDB CLI again (now providing the credentials of the admin account) and execute the following query:

influx -username admin -password passCREATE DATABASE temperature

Read Data and Fill InfluxDB

Once the InfluxDB configuration is complete, we are ready to set up a script to upload the data from the sensor to the database.

Using some Python libraries (Adafruit, pyA20 and Influxdb) we can get the desired information from the GPIO pins and store it in the database. Sometimes the read data is wrong. It’s caused by the lack of the resistor mentioned above. To avoid storing empty data in the database, we loop through the values until we get real data.

Finally, we will need to automate the script by adding a new entry in the crontab file. In this case, we are going to add the following rule so that the script runs every 15 minutes.

*/15 * * * * python3 /path-to-script/read_data.py

Grafana Configuration

Grafana is an open source visualization and analytics software. It allows us to query, visualize, alert on and explore our metrics no matter where they are stored. In our case, it allows us to display InfluxDB data through customizable dashboards.

We can install Grafana using its official APT repository, by downloading a .deb package, or by downloading a binary .tar.gz file. Once installed, it’s time to make the necessary configuration for our use case.

By default, Grafana runs on port 3000, so we will have to enter the address http://<our-orangepi-ip>:3000. Once we access to the web page, we have to log in with “admin” as username and password.

The first step is to add a new data source to get the data from InfluxDB. To do this, we have to click on “Add data sourceand fill the information following the picture below, where “URL” is our Ip and the default InfluxDB port. This picture shows only the important information, the remaining one can be left with the default values.

Once we create the data source, we need a dashboard to show the data properly. To create a new dashboard, we have to click on “Create > Dashboard”.

After creating the dashboard, we can add a new panel by clicking on “Add new panel”. New panels have a default query that we can edit instead of creating a new one. To edit the default query, we have to click on the “editicon (Toggle text edit mode) as we can see in the image below.

We will create two panels with two queries, one for temperature and one for humidity:

SELECT "temperature" FROM "TemperatureSensor" WHERE $timeFilterSELECT "humidity" FROM "TemperatureSensor" WHERE $timeFilter

This is the simplest dashboard that we can create. By tweaking some panel preferences, we can make it look much prettier.

Humidity and Temperature panels in Grafana Dashboard.

Wrapping up

Following a few single steps we get the desired result: a low cost system that tracks the humidity and temperature in our home.

--

--