I wanted to monitor the temperature (and humidity is nice to know too) of the room where I am now keeping my server.

I’m using a Raspbery Pi 3B+, and a DHT22 sensor.

To do this, I followed Sam McGeown‘s guide for how to do this with regards the temperature sensor, but I used Simon Hearne‘s guide for making the python file executable and setting it to run as a service.

Update the Pi

Update the RPi.

sudo apt-get update && sudo apt-get upgrade -y
sudo rpi-update

Load GPIO Driver on Boot

Run the following command to enter the config menu

sudo raspi-config

In Interfacing Options, enable SPI, I2C and 1-Wire. Then reboot.

sudo reboot

Install Library to Read Sensor Data

Python 2.7 (Default)

Run the following to install Adafruit_DHT library.

sudo apt-get install build-essential python-dev python-openssl -y
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo python setup.py install

Then test it out using the following, where 22 is the driver, and 4 to use GPIO 4.

sudo python Adafruit_Python_DHT/examples/AdafruitDHT.py 22 4

Python 3.7

Run the following to install AdaFruit_DHT library.

pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2

# Get Python 3.7 (3.6 must be done first)
sudo apt-get install python3.6
sudo apt-get install python3.7

# Install PiP
sudo apt install python3-pip

# Make Python 3 the Default
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Install InfluxDB Packages

We want to send to InfluxDB running on my server, so only need packages to send data.

Note this will install the correct version of InfluxDB based on which Python version you have installed.

sudo pip install influxdb

Create a Database for the Temperature Data

Run the following command from an Unraid terminal. It will create a Database called temperature with a retention policy of 7 days.

curl -i -XPOST http://192.168.0.3:8086/query --data-urlencode "q=CREATE DATABASE "temperature" WITH DURATION 7d NAME "one_week_only""

Script to Send Data to InfluxDB

Create a file, I named mine publish.py and edit it.

Python 2

#!/usr/bin/env python

import time
import sys
import datetime
import Adafruit_DHT
from influxdb import InfluxDBClient

# Configure InfluxDB connection variables
host = "192.168.0.3"
port = 8086
user = ""
password = "" 
dbname = "temperature"

# Create the InfluxDB client object
client = InfluxDBClient(host, port, user, password, dbname)

# Enter the sensor details
sensor = Adafruit_DHT.DHT22
sensor_gpio = 4

# think of measurement as a SQL table, it's not...but...
measurement = "rpi-dht22"
# location will be used as a grouping tag later
location = "server-room"

# Read the sensor using the configured driver and gpio
humidiy, temperature = Adafruit_DHT.read_retry(sensor, sensor_gpio)
iso = time.ctime()
# Print for debugging, uncomment the below line
# print("[%s] Temp: %s, Humidity: %s" % (iso, temperature, humidity)) 
# Create the JSON data structure
data = [
        {
          "measurement": measurement,
              "tags": {
                  "location": location,
              },
              "fields": {
                  "temperature" : temperature,
                  "humidity": humidity
              }
          }
        ]
# Send the JSON data to InfluxDB
client.write_points(data)

Python 3

#!/usr/bin/env python

import time
import sys
import datetime
import board
import adafruit_dht
from influxdb import InfluxDBClient

# Configure InfluxDB connection variables
ifhost = "192.168.0.3"
ifport = 8086
ifuser = ""
ifpassword = ""
ifdbname = "temperature"

# Create the InfluxDB client object
ifclient = InfluxDBClient(ifhost, ifport, ifuser, ifpassword, ifdbname)

# DHT Device - With the Pin that is connected
sensor = adafruit_dht.DHT22(board.D4, use_pulseio=False)

# think of measurement as a SQL table, it's not...but...
measurement = "rpi-dht22"
# location will be used as a grouping tag later
location = "server-room"

# Read the sensor using the configured driver and gpio
temperature = sensor.temperature
humidity = sensor.humidity
iso = time.ctime()
# Print for debugging, uncomment the below line
# print("[%s] Temp: %s, Humidity: %s" % (iso, temperature, humidity))
# Create the JSON data structure
data = [
        {
          "measurement": measurement,
              "tags": {
                  "location": location,
              },
              "fields": {
                  "temperature" : temperature,
                  "humidity": humidity,
              }
          }
        ]
# Send the JSON data to InfluxDB
ifclient.write_points(data)

Now save it and try it out by running (enable the printing in the file).

sudo python publish.py

Run Script as Service

Make the script executable by running

chmod +x publish.py

Run crontab -e and add an entry * * * * * /home/pi/publish.py. This will run the script every minute.

Sources:

Leave a Reply