I don’t claim to be a programmer by any means, my background has been built off of MANY others’ programs that have been shared out. I’ve taken bits and pieces here and there and tweaked them to do what I had in mind of what I wanted to do.
I’ve been playing with my Raspberry Pi 3 test board, getting various sensors to work – the most recent has been a DHT22 temperature & humidity sensor to.. well.. do what it says ti does – measuring the Temp & Humidity. My AllSky box sits outside in the elements 24/7 in a sealed Pelican case; I kow it’s got a great seal on it, but its also a few years old, so I’m setting up a script to measure the humidity inside the box, and if it changes more than a few percent, send me an email to inspect the box as soon as I can to prevent more moisture.
If you want to follow along, I’m going to assume you’ve got your Raspberry Pi setup and running. I’ve tossed a few links to the side in case you’re starting fresh out of the box. VNC isn’t a requirement, but it makes it so you can control your Pi from your desktop or laptop without having to be seated right next to your Pi. The static IP is very helpful if you’re using VNC or SSH while your Pi is in a different part of your home/office while on the same network.
- Enable VNC
- Enable 1-wire

With the Pi setup and running, I started focusing on connecting the sensor – the DHT22 board I have has 3 pins: + (3.3v in), out (signal), – (ground). I ran the connecting wires to the appropriate pins on the Pi, taking note of the physical pin I connected the out/signal pin to (pin 26 in my case). Here’s a link to a great pin-out diagram for the Pi.
With the sensor wired in, I booted up the Pi and started fiddling around with in prep for the code.
sudo apt-get install python3-dev python3-pip
This goes out and downloads/installs Python3 as well as PIP. Depending on the version of the OS you installed, they might both be installed already – it doesn’t hurt to run this line if you’re not sure. If its already installed, it’ll tell you.
Now that Python & PIP are installed, I installed the DHT22 library. It doesn’t take long to install, and once its done, I jumped into some of the basic coding to test it out.
sudo pip3 install Adafruit_DHT
There are various ways on the Raspberry Pi to view/edit/write code – I prefer the already installed ‘Geany’ editor over the command-line editors. The Python programming language is what I used to read the data from the sensor. Python isn’t too hard to understand for the basic commands, but for the more in-depth, Google helped me out a lot for finding different ways to do what I had in mind. I’ve commented the code to help understand what its trying to do.
#!/usr/bin/env python
# sensors_csv.py : runs as both python2 & python3
# add to sudo crontab -e : */5 * * * * python /{path to file}/sensors_csv.py
### tried without the sudo crontab, but it wasn't liking it for some reason
import os # to check if file exists
import Adafruit_DHT # to use the DHT22
from csv import writer # to use the CSV writing capabilities
from datetime import datetime # to set date & time
from gpiozero import CPUTemperature # Calls out the CPU temp sensor
sensor = Adafruit_DHT.DHT22
sensor_pin = 26 # Physical Pin, not GPIO
humidity, temperature = Adafruit_DHT.read_retry(sensor, sensor_pin)
def dht_temp(): #get DHT22 Temp
temp_f = float(temperature) * 9 / 5 + 32
dht_temp = round(temp_f, 2)
return dht_temp
def dht_hum(): #get DHT22 Humidity
hum = float(humidity)
dht_hum = round(hum, 2)
return dht_hum
def cpuTempF(): #get CPU temp in F
cpu = CPUTemperature()
cpuTempL = float((cpu.temperature) * 9 / 5 + 32) # Calls out CPU Temp and converts to Fahrenheit
cpuTempF = round(cpuTempL, 1)
return cpuTempF
def logtime(): # ACQUIRE Current Time
logtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return logtime
def writeCSV(): # Write data to a CSV (could also be .txt)
month = datetime.now().strftime("%Y-%m(%B)") # used to create new log every month.
file = open("/home/pi/test/_data_temps-log-%s.csv" % month,"a")
i = 0
if os.stat("/home/pi/test/_data_temps-log-%s.csv" % month).st_size == 0:
file.write("Time,CPU Temp(f),Temperature(f),Humidity(%)\n")
file.write(str(logtime())+","+str(cpuTempF())+","+str(dht_temp())+","+str(dht_hum())+"\n")
file.close()
r = 1 # r is variable for Run number
while r <= 1: # if run number is less or equal to 1, run
writeCSV () # run function
r +=1 # add 1 to Run number to end script
Here’s what the output looks like, followed by the basic interpreted format :
Time,CPU Temp(f),Temperature(f),Humidity(%) 2021-03-08 23:14:03,125.7,72.5,30.9 2021-03-08 23:15:04,125.7,71.42,31.1 2021-03-08 23:16:03,127.7,71.42,31.1 2021-03-08 23:17:08,128.6,71.24,31.3 2021-03-08 23:18:08,128.6,71.06,31.5 ========================================== And here's the basic interpreted format ========================================== Time CPU Temp(f) Temperature(f) Humidity(%) 2021-03-08 23:14:03 125.7 72.5 30.9 2021-03-08 23:15:04 125.7 71.42 31.1 2021-03-08 23:16:03 127.7 71.42 31.1 2021-03-08 23:17:08 128.6 71.24 31.3 2021-03-08 23:18:08 128.6 71.06 31.5
The next step will be adding this script to the sudo crontab -e so it’ll run automatically every few minutes. After that, write another basic script to read the humidity value and send it to me via email if the value is too high (indicating a potential seal failure).
