KD0YTE's Ham Radio, Linux, and other stuff.

Temperature recording with the pi
17th April 2024

I have a semi sheltered area that I want to put some plants in but springtime in NE MO is hit and miss and I dont want to damage the plants.
I also have a garage that is unheated that i want to monitor how cold it gets this winter.
So I fixed up raspberry pi zeros to keep a temp monitor of those areas.

installed pi os 32 bit lite version

On it installed pyhon3-pip
sudo apt-get install python3-dev python3-pip
sudo python3 -m pip install --upgrade pip setuptools wheel

Also installed the ADafruit library for the DHT11
sudo pip3 install Adafruit_DHT

Then i came up with a script to read from the sensor and report temp in Fahrenheit and humidity
named it tempf.py

import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
tempf = (temperature*1.8)+32
print (today, ",", now, ",", tempf, " ,", humidity )
# print (humidity, "%,")

2nd found a script to output current date and time
named it dateit.py

  1. Import the 'datetime' module to work with date and time
    import datetime
  1. Get the current date and time
    now = datetime.datetime.now()
  1. Create a datetime object representing the current date and time
  1. Display a message indicating what is being printed
    print("Current date and time : ")
  1. Print the current date and time in a specific format
    print(now.strftime("%Y-%m-%d %H:%M:%S"))
  1. Use the 'strftime' method to format the datetime object as a string with the desired format

Then I wrote a bash script to call those 2 and send output to tempout.txt
temprec.sh

#!/bin/bash

python dateit.py >> ftemp.txt
python tempf.py >> ftemp.txt

Finally setup a crontab to run it every 5 minutes
crontab -e

*/5 * * * * /home/pi/temprec.sh

This could probably be done from 1 single python script but I am just not that good with python.
This works well. I might change to a longer interval for readings.

Output looks like this

2024-09-20 , 10:00:02.247606 , 78.80000000000001 , 64.0
2024-09-20 , 11:00:01.831673 , 82.4 , 61.0

Tags: linux, raspberry pi.