Skip to content
Menu
KE5PRL's Blog!
  • KE5PRL Environment Graphs
KE5PRL's Blog!

Raspberry Pi CPU temperature monitoring.

Posted on October 6, 2021October 6, 2021 by Pat Pace

I capture the CPU temperature of my Raspberry Pi via a very short python program detailed below. After getting the temperature I append it to a log file with the time for graphing with another python program. Thanks to the gpiozero python library it is really simple to code this. This program is executed every five minutes using cron because I find the Raspberry Pi can sometimes get really hot for a minute or so and this way I can catch those spikes. See http://www.ke5prl.com/ke5prl-environment-graphs/ for my graphs.

#Import the CPUTemperature class from the gpiozero library.
from gpiozero import CPUTemperature

#Import the datetime module.
import datetime

#Get the temp, convert to f, round to nearest integer and convert to string.
cpu = CPUTemperature()
cpuf = str(round((cpu.temperature * 1.8) + 32))

#Get the current time and convert to string.
timenow = str(datetime.datetime.now())

#Concatenate temp and time with a pipe between them for parsing later then add a return.
logline = cpuf + "|" + timenow + "\n"

#Append the data to a log file
cputemp = open("cputemplog.txt", "a")
cputemp.write(logline)
cputemp.close()

Recent Posts

  • Raspberry Pi CPU temperature monitoring.
  • Installing and testing Docker on Raspberry Pi.
  • Family visit with a bit of radio.

Recent Comments

    Archives

    • October 2021
    • June 2021

    Categories

    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    ©2022 KE5PRL's Blog! | Theme: Wordly by SuperbThemes