/thlogger

Temperature and humidity logger

Primary LanguagePythonApache License 2.0Apache-2.0

Build Status codecov Code style: black

thlogger

Data logger for use with Raspberry Pi and DHT sensors.

Logs temperature and humidity to InfluxDB. Visualization with Grafana.

Runs on any Raspberry Pi model.

Installation

Setup InfluxDB and Grafana

You'll need an always on computer to run your database. I'd recommend a Linux server. If you don't have one already, you could use an old repurposed laptop, or a VPS (these start at around $5/month).

To make this as easy as possible, we'll use Docker.

git clone https://github.com/viis/thlogger.git
cd thlogger/docker
  • Create a directory for the database files (shared between the host and the container)
mkdir vol
  • Start the docker containers
docker-compose up -d

InfluxDB is now accessible on your server's port 8086 and Grafana on 8080.

Setup your RPi

Hardware

NOTE There are a few different variants of the DHT sensors. This guide assumes you are using one with an internal pull up resistor. If you aren't, Google is your friend :)

DHT sensors have 3 pins, labelled +, out and -. Connect them to 5V, a data pin and Ground, respectively. I used pins 2 (5V), 11 (data), and 6 (Ground). Note that pin 11 is GPIO pin number 17 (you'll need that number in your config later). Pinout.xyz is a useful reference for the RPi GPIO layout. If you are using a RPi 1, you'll only have the first 26 pins, but that is enough for this project.

IMAGE

Operating system

Wireless networking

If your RPi has a wired network connection, skip this step

  • When the image is loaded, remove the SD card from your computer and re-insert it

  • In the root if the SD card, create an empty file called ssh

  • In the root if the SD card, create a file called wpa_supplicant.conf with the following contents:

country=DK
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NETWORK-NAME"
    psk="NETWORK-PASSWORD"
}

REMEMBER to replace NETWORK-NAME and NETWORK-PASSCODE with your network name and code

You can also replace DK with the country you're in

Boot and access your RPi

  • Insert the SD card into the Pi and boot it by connecting it to a power source

  • Find the RPi on your network with nmap (replace 192.168.1 with your network's address)

sudo nmap -sS -p 22 192.168.1.1/24

The RPi should show up with the Raspberry Pi Foundation next to its MAC address. Note the IP address.

  • Connect via ssh

The default password is raspberry, you should change it when your log in for the first time.

ssh pi@IP_ADDRESS

Upgrade your RPi and install required packages

sudo apt update
sudo apt upgrade
sudo apt install git python3-venv

Install thlogger

You'll need to update the thlogger.conf before starting the service.

  • Clone this repository and edit the config file
git clone https://github.com/viis/thlogger.git
cd thlogger
cp thlogger.conf.example thlogger.conf
# edit thlogger.conf

The config consists of the following:

"SENSOR_MODEL": 11, 22, or 2302 (depending on which sensor you have)
"GPIO_PIN": The GPIO pin the sensor is connected to (17 if you followed the guide above)
"HOST": InfluxDB server IP address
"PORT": InfluxDB port (usually 8086)
"ORG": InfluxDB org (isberg)
"BUCKET": InfluxDB database name (thlogger)
"TOKEN": InfluxDB username (generate this yourself)
"LOCATION": Location of your logger (garage, attic, etc)
"SLEEP_BETWEEN_READINGS": In seconds (60 for a measurement every minute)
  • Install thlogger as a service
sudo sh ./INSTALL

The logger is now running as a service on your RPi, and is logging temperature and humidity to your database. The service starts automatically on boot.

You can view the log file with

tail /var/log/thlogger/thlogger.log

Remove thlogger

sudo sh ./REMOVE

NOTE! This removes all associated files that are outside of the git repo, also log files.

Visualize your measurements with Grafana

Access you Grafana installation on http://your-server-ip:8080

The default admin user/password is admin/admin. You should change the password when you log in for the first time.

Use the setup wizard to setup a data source with the following information:

URL: http://influxdb:8086
Database: thlogger
User: thlogger
Password: the password you wrote in influxdb.env above

Then setup a dashboard. If you select your newly created data source as the source, you should have measurements for temperature and humidity available.