Homebrew Temperature Monitoring

I love technology and I love to homebrew. Recently, I converted our old freezer chest to a keezer to store my homebrew kegs and commercial ones I had bought through local craft brewers. I use an Inkbird temperature controller to maintain the perfect temperature for my kegs and an Eva Dry Mini Dehumidifier to keep things from getting too wet from condensation. Despite having an AcuRite Digital Hygrometer sitting in the keezer to display temperature and humidity, I wanted to chart that data over time. Enter the Raspberry Pi Zero W!

Raspberry Pi Zero and Sensor

At only $10, the Raspberry Pi Zero W is a formidable solution for many project needs. I needed a cheaper, simpler Raspberry Pi that would connect to wireless and the Zero W was a perfect match. And it also has the GPIO pins to solder a temperature and humidity sensor, such as the AM2302 sensor, to it.

I purchased the Raspberry Pi Zero W and AM2302 sensor (because it had longer wires than the DHT22, allowing me to place the sensor in the keezer and keep the Pi outside) from AdaFruit. Here is everything you’ll need hardware-wise:

  • Raspberry Pi Zero W, $10
  • AM2302, $15
  • 8GB MicroSD Card, $10
  • Micro-USB Power Cable, free b/c who doesn’t have a few lying around?

Configuring the Pi Zero

The first step is to download the latest Raspberry Pi OS. These instructions on downloading and installing the OS on a MicroSD card are much more detailed than I intend to be here: https://www.raspberrypi.org/downloads/.

The Pi Zero has a mini-HDMI port and mini-USB on it, so instead of buying adapters to interact with it, I opted to go the easy route by enabling SSH and connecting it to wireless in a headless setup.

Now that you have Raspberry Pi OS installed on your MicroSD card, plug the card into your computer and open the removable drive in your Windows Explorer / Finder window. Create an empty file named ssh. That will enable SSH on the Pi when it boots. Next, create another file named wpa_supplicant.conf. Open this file with your preferred text editor and add the following:

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

network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}

Change the WIFI_SSID and WIFI_PASSWORD to match yours. This will automatically connect the Pi to your wireless network when it boots. More details on this process can be found here: https://linuxhint.com/rasperberry_pi_wifi_wpa_supplicant/.

Soldering the Sensor

I’m not very experienced at soldering, so this was the hardest part for me in building the sensor. I used some super thin solder I had from the RadioShack days and managed to solder the wires without too much of a mess.

Red (pin 2) and black (pin 6) will be used for power and yellow (pin 8) will be used for sending the sensor data to the Pi. Note that pin 8 on the board maps to GPIO 14 which we’ll use in the script later on.

Reading the Sensor

Python is a simple, yet powerful, language to carry out tasks in the form of scripts. Fortunately, AdaFruit has a developed the libraries that we can use to poll the DHT sensor. No need to reinvent the wheel!

First, install the necessary software on the Pi Zero; this includes Python (now version 3) and the DHT sensor Python libraries:

sudo apt-get update
sudo apt-get install python3-pip libgpiod2
sudo python3 -m pip install --upgrade pip setuptools wheel
pip3 install adafruit-circuitpython-dht

Next, either play with the AdaFruit dht_simpletest.py Python script or use the one below. The only real difference is formatting on output because I wanted mine a specific way to work with my Splunk log server. Note that I soldered the sensor to GPIO pin 14, which is referenced by board.D14 in the script below.

#!/usr/bin/python

import sys
import datetime
import board
import adafruit_dht

# Log the date and time
timestamp = datetime.datetime.now()

# Initialize the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D14)

# Read temperature, convert the temperature to Fahrenheit.
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32

# Read humidity
humidity = dhtDevice.humidity

# Open log file
f = open('log/temperature-humidity.log', 'a')

if humidity is not None and temperature_f is not None:
    f.write( str(timestamp) + "," + '{0:0.1f},{1:0.1f}'.format(temperature_f,humidity) + "\n")
else:
    f.write( str(timestamp) + ",fail,fail\n" )

# Close log file and exit
f.close()
sys.exit(1)

That script will write the output to the ~/log/temperature-humidity.log file so I have historical data. The next step is to run the script every minute via CRON. CRON is a scheduler and can be edited with the crontab -e command. Add this line to the crontab to run the Python script every minute:

# m h  dom mon dow   command
* * * * * python3 /home/pi/keezer-pi.py

Now we’re polling our sensor and writing the output to file like this (timestamp, temperature in F, humidity):

2020-08-02 09:33:02.351738,48.6,57.8
2020-08-02 09:34:02.815244,48.6,61.7
2020-08-02 09:35:02.276850,48.6,65.3
2020-08-02 09:36:02.730676,48.6,68.0
2020-08-02 09:37:02.187699,48.9,70.4
2020-08-02 09:38:02.635279,49.1,72.2
2020-08-02 09:40:02.520967,49.6,73.6
2020-08-02 09:41:01.981788,49.8,75.3
2020-08-02 09:42:02.442896,50.0,74.9
2020-08-02 09:43:02.901351,50.0,75.4

Sending Sensor Logs to Splunk

Depending on your setup, you may be satisfied with stopping at this point and just scrolling through the log file or opening it as a CSV in Excel. But I already have a Splunk server in my home lab and sending these logs to Splunk to search and visualize was a no brainer for me. Splunk is a log aggregation and data visualization system that can run on a computer and the free version supports 500MB / day logs which is more than enough for projects like the Keezer Pi sensor logs.

First, edit /etc/rsyslog.conf to specify the Splunk server IP address and port (this assumes you’ve already configured the syslog data input on Splunk):

# Specify our hostname
$LocalHostName keezer-pi

# Ship logs to Splunk
*.* @192.168.2.13:1514

# Rules for the keezer logs
$ModLoad imfile
$InputFileName /var/log/temperature-humidity.log
$InputFileTag temperature-humidity
$InputFileSeverity info
$InputFileFacility local7
$InputRunFileMonitor

Now that the data is going to Splunk, modifying the following search should generate a table of time, temperature and humidity:

host=“keezer-pi" "temperature-humidity"
| rex "(?i) temperature-humidity (?P<Time>[^,]+),(?P<Temp>[^,]+),(?P<Humidity>[^,]+)"
| table Time,Temp,Humidity

If you click over on the Visualization tab, you can easily create a chart of your temperature and humidity like this:

What I love about this is that I can see the temperature and humidity without opening the keezer to look at the AcuRite Hygrometer and messing up the readings by opening the keezer lid.

You can even setup alerts in Splunk so if your temperature gets too high or too low, Splunk can email you an alert. Pretty cool!

Good luck in your homebrewing and let me know in the comments how you monitor your beer!

Add a Comment

Your email address will not be published.