Logging Temperature and Humidity with a Raspberry Pi Zero W


My house seems to vary a lot when it comes to temperature. Some areas are pretty warm, and others are freezing. Out of curiosity, I wanted to log the average temperature around different parts of my house. Sounds like a job for the Raspberry Pi.

My plan was to have several Raspberry Pi’s around the house connected to temperature and humidity sensors. These would poll the sensors every 15 minutes and request a website located on a webserver with the LAMP stack. The server would grab the data from the query and log that into a MySQL database. I’d then use that data to create a front-end to view pretty graphs.

Ideally, each Pi I set up will have a unique hostname configured, which will be used on the front-end to determine what area of the house each Pi is located.

I did my first tests using a Raspberry Pi 2B, but since my final design requires very little resources, I wanted the smallest form factor possible. In this example, we’ll be using the Raspberry Pi Zero W.

We’ll need a few components to get this to work.

  • Raspberry Pi Zero W
  • 10K ohm resistor
  • DHT-22 sensor (also known as RHT03 or SEN-10167)

With these components, we’ll connect them the following way (the illustration is not to scale):


Since DHT-22 is a digital sensor, we’ll use the 10k ohm resistor as a pull-down resistor to ensure the values returned are either HIGH or LOW (1 or 0).

With everything now connected,  we’ll need to get some things set up to run our Python script. Basically we need some essential Python stuff as well as a library to make HTTP requests.

sudo apt-get update
sudo apt-get -y install python-setuptools python-dev build-essential
sudo easy_install pip
sudo pip install urllib3

Now we need PIGPIO, which is a library that makes it easier to work with GPIO pins using Python.

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
sudo make install
cd ..
rm pigpio.zip

On a Raspberry Pi Zero W, making could take a while. Once installed though, we’ll want to start the Pigpio daemon.

sudo pigpiod

Now we need the DHT22 module. This does all the hard work of interfacing with our sensor.

wget http://abyz.co.uk/rpi/pigpio/code/DHT22_py.zip
unzip DHT22_py.zip
rm DHT22_py.zip
rm DHT22_old.py

Now that we’ve got the necessities, let’s set make our script.

sudo vim poll_temphum.py

And here’s the script:

#!/usr/bin/env python
import urllib3
import subprocess
host = subprocess.check_output('hostname', shell=True)
http = urllib3.PoolManager()
from time import sleep
import os
import pigpio
pi = pigpio.pi()
import DHT22
s = DHT22.sensor(pi, 4)
temp = '{:3.2f}'.format(s.temperature() / 1.)
humi = '{:3.2f}'.format(s.humidity() / 1.)
calibration = -4.22
temp = str(float(temp) + calibration)

query = ""+temp+"&humi="+humi+"&hostname="+host
query = query.replace('\n', ' ').replace('\r', '').replace(' ', '')

page = http.request('GET', query)

Basically, this script does the following:

  1. Import the necessary libraries
  2. Get the hostname of the Raspberry Pi running the script
  3. Set the sensor to use GPIO pin 4
  4. Trigger the reading, and wait 2 seconds for the sensor to do its magic
  5. Store the values in variables
  6. Makes any necessary adjustments for calibration. In this example, my sensor was off by 4 degrees celsius
  7. Request a website with a query string containing our data

Ideally, we wouldn’t hard-code the IP into our script and instead just use DNS to locate our local LAMP server, but I’m just doing it the dumb way. It’s worth mentioning though. When the script runs, it will try to reach a PHP page called push_temp.php, which then takes the GET data received and stores it in a MySQL database.

Now that we have our script ready to go, we’ll need to set the permissions up to allow crontab to execute the Python script.

sudo chmod +x poll_temphum.py

Now we’ll set up crontab to run our script every 15 minutes.

crontab -e

Add this line to crontab.

*/15 * * * * /usr/bin/python /home/pi/poll_temphum.py

We also want to make sure the PIGPIO daemon starts in case the Pi needs to reboot for any reason.

sudo crontab -e

Add this line:

@reboot              /usr/local/bin/pigpiod

Now we’re all set! I’ll likely cover the front end design on a future post.

Leave a Reply