Temperature monitor using DHT22 + Grafana + InfluxDB (and a PI) – Part 2

In this part we will first install InfluxDB and use a simple cronjob to save the data in InfluxDB.

This is part 2 of my home made temperature project. Click here.

Install InfluxDB

A few years back, installing InfluxDB on a raspberry pi was a challenge. Fortunately today, the company behind InfluxDB has a repository available that makes life allot easier. First login to the RPi and add the repository with the following commands:

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release
test $VERSION_ID = "7" && echo "deb https://repos.influxdata.com/debian wheezy stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
test $VERSION_ID = "8" && echo "deb https://repos.influxdata.com/debian jessie stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
test $VERSION_ID = "9" && echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

If successful, you should receive an output on one of the test-lines like: deb https://repos.influx…

Next, perform the installation running the apt-get command:

sudo apt-get update && sudo apt-get install influxdb

After the installation we need to enable and start the InfluxDB service:

sudo systemctl enable influxdb
sudo systemctl start influxdb

Configure InfluxDB

⚠ A word of caution, in the next step authentication will be enabled, but this is not a security guide for InfluxDB. If security is something crucial for your project, I advice to read the documentation on the website of InfluxDB

To enable authentication, enter the influx cli with the command influx. Once we are in the influx cli, we need to enter the following query to create the admin account. In this step, you best replace pass in the query with one of your own choice. Keep in mind that we will be using these credentials for the rest of the project.

SHOW users

If the query was successful, exit the influx cli with the command quit. Now that we have configured the credentials we will enable authentication when using http/https. Open the file /etc/influxdb/influxdb.conf with a text editor and set auth-enabled to true

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

 sudo service influxdb restart

In the following step we will create the database. To do this we need to enter the influx cli again, but this time we need to provide the credentials of the admin account:

influx -username admin -password pass

We create the temperature database with following query and exit the cli:

CREATE DATABASE "temperature"

This completes the configuration of the Influx DB and we are ready to setup a script that will upload the data from the sensor to the database

Upload Data

For the upload of the data, a python script will be used. The latest version can be downloaded from GitHub page, but before we dot that, some modules needs to be installed with the following commands:

sudo easy_install pip
sudo pip install influxdb

Now we can go ahead and download the last version of the script. We will first create a directory and then use wget to download the script:

mkdir code && cd code
wget https://raw.githubusercontent.com/BlackC0ffee/RPi-Temperature-Project-V3/master/RaspberryPi/code/read_temp.py

If you open the script in een editor, you wee see that it’s not too big and you also find a part that is used to define the password to connect to the database. For authentication we will use a simple text file called “secretstring” that contains the password. In the following command, we will create this file. Don’t forget to replace password with the admin password for influx:

echo password > secretstring

In this step it is time to test the script. (Optional) add an execute-flag with chmod command:

chmod +x read_temp.py

Normally no output is shown. The result can be viewed by running the following command in the influx cli:

use temperature
SELECT "temperature" FROM "TemperatureSensor" ORDER BY ASC LIMIT 10

As result the last 10 values in the database should return:

Configure Cron job

Finally we will need to automate the script by running it as a cronjob run the following command to open the cronjob editor:

 crontab -e

Add the following line to the file to run the script exactly every 15 minutes:

# m h  dom mon dow   command
0,15,30,45   *    *    *    *  python /home/pi/code/read_temp.py

Once the file is saved, this part of the tutorial is done. Every 15 minutes the script will save the temperature and humidity to an influx DB. In the last part the data will be visualised using Grafana.

5 thoughts on “Temperature monitor using DHT22 + Grafana + InfluxDB (and a PI) – Part 2”

  1. Can you please explain this part:
    echo password > secretstring

    Do I need to add this in read_temp.py?


  2. Not really. It’s just a way for the read_temp.py script to read the password to connect to the influxDB. You could also replace:
    with open(os.path.dirname(os.path.abspath(__file__)) + '/secretstring', 'r') as f:
    influxPasswd = f.readline().strip()

    influxPasswd = "password of influxDB"
    in read_temp.py

  3. Hey,
    When I click on the link with page no. 3 (Grafana) it leads me to a page which seems to have a similar address as the first two, but it says “Oops! That page can’t be found.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.