The reason for the Raspberry Pi project

I have a cabin in southern Indiana that I don’t get to as often as I’d like.  Since it gets below freezing in the winter, I have to winterize it by turning the pump off, draining the plumbing, and putting antifreeze in the traps.  It would be nice to monitor the indoor temperature to see what actually happens when I’m not there.  Dad, being the engineer that he was, had a mechanical temperature recorder.  You know, the kind that uses a roll of paper and a moving ink pen to make a graph, like an EKG machine in the hospital.  Unfortunately it no longer works so I decided to pay homage to that by building a modern equivalent.

I picked up a pair of DHT22 temperature/humidity sensors and they will connect to the GPIO pins on the pi.  I’ll need a program to read the sensors and log the data values, and it would be great to generate a graph of the values to see the how the values change over time.

Now, a small wrinkle.  The daemon that manages the fan and power switch uses GPIO pin 4, so we’ll need to use a different one.  After powering down the pi, I plugged the “+” to 3.3v, the “-” to ground, and the data to GPIO pin 5.  A single click on the power switch and the pi fired right up.  Since I had two sensors, I connected the second one to GPIO pin 6.  The sensor on GPIO pin 5 is “upstairs” and the one on GPIO pin 6 is “downstairs.  If you accidentally reverse the 3.3v and ground connections, the pi will not boot.

Now we need a little program to test our sensor.  Python seems to be the best tool for this, so here we go. Let’s walk through the code and see how it works.

#!/usr/bin/python3

import time
import Adafruit_DHT as dht

This program has been marked as executable, so the first line tells the OS which interpreter should be used. The other lines include the python module “time” that allows us to use time functions in our script, and the ADAfruit DHT module that talks to the sensors.

DHT_SENSOR = dht.DHT22
U_DHT_PIN = 5
D_DHT_PIN = 6

These lines define constants that we will use later – this allows us to make changes to the code in one place.  We’ve defined two constants for the GPIO pins we’re using, and the sensor type we’re going to query.

while True:

This line starts an endless loop, meaning that this section of code will run until the program is terminated.

    uhm, uth = dht.read_retry(DHT_SENSOR, U_DHT_PIN)

    if uhm is not None and uth is not None:
        uth = uth * 9/5.0 + 32
        print("UPSTAIRS - Temp={0:0.1f}*F  Humidity={1:0.1f}%".format(uth, uhm))
    else:
        print("Failed to retrieve data from upstairs sensor")

This is where the magic happens. First, we query the sensor, specifically the one attached to GPIO pin 5, and place the values in the variables uth (temperature) and uhm (humidity). Then we check to be sure that we actually got values back from the sensor before doing any manipulation of the values. Then we convert the temperature from centigrade to fahrenheit.  This is optional depending on your preference. Now we print the values from the “upstairs” sensor (the one connected to GPIO pin 5). And finally, we handle the condition where the sensor does not return valid values.

    dhm, dth = dht.read_retry(DHT_SENSOR, D_DHT_PIN)

    if dhm is not None and dth is not None:
        print("DOWNSTAIRS - Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(dth, dhm))
    else:
        print("Failed to retrieve data from downstairs sensor")

These lines are essentially a duplicate of the previous section, but these handle the “downstairs” sensor connected to GPIO pin 6.  You’ll notice that I did not include the line to convert centigrade to fahrenheit for this sensor.

    time.sleep(15)

This line causes the program to sleep for 15 seconds before going through the loop again.

Here’s the complete test program. Be aware that python is sensitive to indentation and line spacing.

#!/usr/bin/python3

import time
import Adafruit_DHT as dht

DHT_SENSOR = dht.DHT22
U_DHT_PIN = 5
D_DHT_PIN = 6

while True:
    uhm, uth = dht.read_retry(DHT_SENSOR, U_DHT_PIN)

    if uhm is not None and uth is not None:
        uth = uth * 9/5.0 + 32
        print("UPSTAIRS - Temp={0:0.1f}*F  Humidity={1:0.1f}%".format(uth, uhm))
    else:
        print("Failed to retrieve data from upstairs sensor")

    dhm, dth = dht.read_retry(DHT_SENSOR, D_DHT_PIN)

    if dhm is not None and dth is not None:
        print("DOWNSTAIRS - Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(dth, dhm))
    else:
        print("Failed to retrieve data from downstairs sensor")

    time.sleep(15)

Next – adding a method to save the values into a database, that we can use to visualize the swings of temperature and humidity over time.

Leave a Reply