I've built a few projects that kept track of temperature, humidity and barometric pressure in the past couple of years. Each of them used one of the Raspberry Pi variants... well, there was one that used the Adafruit Feather M0 WiFi (Link to the Feather M0 with WiFi) and a BME280 that ran a web server... Maybe I'll write that one up next...
My first project used a Pi 3 B and the DHT10 sensor.
It sits in a container and keeps track of the temperature and humidity for me, logging it to a file every 15 minutes. It has been doing a pretty good job until last year when the humidity reading went wacko. It reads high all the time. It's also used to access the internet (complete with mouse, monitor and keyboard).
My next project used a Pi 3 A+ with a BMP390 (Temperature and Barometric pressure) and a SHT31 (Temperature and Humidity). Plus a Real Time Clock and a 2x16 LCD Display.
It, too, lived in the container monitoring temperature and humidity and logging it every 15 minutes. The log is archived daily on a thumb drive.
My third project saw me trying to combine the tasks both were doing. I wanted something to access the internet and to track Temperature and Humidity. For this task I pulled a Pi 400 out of the box where it has been sitting unused and pressed it into service. For monitoring the Temperature and Humidity I chose the AHT10. This part is readily available and less expensive than the BMP390.
I connected wires to the AHT10 with pin jacks on the ends, then connected the wires to the 40 pin header on the rear. Here is where the wires are connected:
Caveat - There are extra steps here I did not need in earlier incarnations of this project. This is because the Pi 5 has the 'bookworm' release of the Pi OS. With the new release any libraries loaded for Python that are not part of the regular release path (ie loaded using the apt command) require you to install them in a 'virtual environment'. I did not delve into the whys and what fors. I just read the information and implemented it.
The following are the steps I used, as taken from the Adafruit web page, to get this to work on my Pi 400:
1 - Install python3 full:
$ python -m venv env –system-site-packages
2 - Make sure the virtual environment python is installed using these commands:
$ sudo apt install python3-full -y
$ sudo apt install python3-venv
3 - The virtual environment needs to be activated:
4 - Then run the following commands to be sure the pi is correctly configured and to install blinka:$ source env/bin/activate
$ cd~
$ pip3 install –upgrade adafruit-python-shell
$ wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py$ sudo -E env PATH=$PATH python3 raspi-blinka.py
5 - Then activate the virtual environment
$ source env/bin/activate
6 - Then install the adafruit library for the AHT10 using this command
$ pip3 install adafruit-circuitpython-ahtx0
7 - Now the python program temp-humidity.py will run (source at the end of this page).
$ python3 temp-humidty.py
I need the program to run every 15 minutes. For this I use cron.
To get the python script to run within the virtual environment using cron you need to put the following line into your /etc/crontab file (the following should be one long line):
*/15 * * * * piuser source env/bin/activate ; python3 /home/piuser/temp-humidity.py >> /var/tmp/temperature.log 2>&1 ; deactivate
- The */15 tells cron to run this every 15 minutes on the quarter hours (ie 00, 15, 30 and 45).
- The next four * tells cron to run this every hour, day of month, month of year, and day of week (google cron examples to learn aboout cron)
- The pisuer is the user running the program (it is run from /home/piuser by cron).
- The three commands used are as follows:
- source env/bin/activate (this command activates the virtual environment)
- python3 /home/piuser/temp-humidity.py >> /var/tmp/temperature.log 2>&1 (this command runs the program and sends an stdout or stderr messages to a log file in /var/tmp)
- deactivate (this command deactivates the virtual environment)
- And they are strung together using semicolons (the following should be one long line):
source env/bin/activate ; python3 /home/piuser/temp-humidity.py >> /var/tmp/temperature.log 2>&1 ; deactivate
Last, here is the Source code for the temp-humidity.py program:
from time import sleep, strftime, time
import board
import adafruit_ahtx0
i2c = board.I2C()
sensor = adafruit_ahtx0.AHTx0(i2c)
filename = "/home/piuser/temperature.csv"
running=True
# Write the values to the file
def write_file(filename,f,h):
log = open(filename, "a")
log.write(strftime("%Y-%m-%d") + ', ' + strftime("%H:%M:%S") + ', Temperature: ' + str(round(f,1)) + ', Humidity: ' + str(round(h,2)) + '\n')
log.close()
while running:
try:
# Get the temperature from the AHT10
temperature_f = (sensor.temperature * 9/5) + 32
# temperature_f = temperature_c * (9 / 5) + 32
# Get the humidity from the AHT10
humidity = sensor.relative_humidity
#print the values to stdout
print(
"Temperature: {:.1f} F Humidity: {:.2f}% ".format(
temperature_f, humidity
)
)
# Write the values to the logging file
write_file(filename,temperature_f,humidity)
running=False
except RuntimeError as error:
print(error.args[0])
continue
except Exception as error:
running=False
raise error