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.

A Raspberry Pi project

I had not played around with a Raspberry Pi SBC (single board computer) before now, and I’m not really sure why not. Maybe because I have enough computers around the house, but I suspect the real reason is that I just didn’t have a viable use for it. I came up with one, and decided to jump in.

I picked up a Raspberry Pi 4B with 4gb ram from Canakit. It came with a power supply, a 32gb micro-sd card containing NOOBS (New Out Of Box Software), and the raspberry pi itself. Connecting an HDMI monitor and keyboard/mouse was trivial. Inserted the micro-sd card, and powered it up. A few minutes later I had a running computer. I updated the software, which was easy using the available tools from the OS. Not blazingly fast by any measure, but usable. Now toy day is always a good day.

I decided that I wanted a case for it, so I did some digging around and chose the Argon One case. The plus for this case is that the connections are routed out the back of the case, and it has a programmable power switch. There is a very nice program that uses an overlay to manage the fan and the power switch. A long press of the power switch shuts the pi down; a double-press reboots the pi, and a single press will start it if the power is off. You can set the temperature thresholds and desired fan speeds, and the settings survive a reboot.

I decided to use an SSD instead of the micro-sd card to hold the OS and also wanted to boot the pi with this configuration. The micro-sd card would not be needed once this is configured. I picked up this SSD and this adapter to connect it to one of the USB3 ports. Worked great. Except for the small hiccup – you can copy the running OS to the SSD, but you can’t boot an SSD from the NOOBS version of Raspbian. So I downloaded an ISO of the Raspbian OS, loaded it onto the micro-sd card, and powered up the pi. I had to reconfigure everything that I had just finished configuring, but since that was recently done it went fairly quickly.

Copying the OS to the SSD was simple using the “SD Card Copier” utility from the “Accessories” menu, and we were at the precipice. Powered down the pi, removed the micro-sd, and powered up the pi. It booted just fine, and I was now running the pi with a 500gb SSD. Not really a major accomplishment, but a big step forward in performance and reliability.

Now we have a Raspberry Pi in a well-designed case, using a 500gb SSD for storage, and a daemon that manages temperature, fan speed, and the multiple functions of the power button.  A very good start.