2

I need to read the temperature using the python program. I am reading the CPU temperature of the Raspberry Pi using psutil module. When I call psutil.sensors_temperatures() it is returning the temperature as 59.75, and when I run vcgencmd measure_temp it is giving 73.1 which is same as the temperature shown in CPU Temperature Monitor. Which function I should use to get the actual CPU Temperature and which temperature is the psutil returning.

I will get the following output when I run psutil.sensors_temperatures():

{'ds3231': [shwtemp(label='', current=62.0, high=None, critical=None)]}

As it is indicating ds3231 is this the temperature of RTC? If so how can I read the CPU Temperature using python.

2
  • I do not have a spare Pi at the mo - could you add the output of psutil.sensors_temperatures() please to your question? Its possibly one core running hot vs an average...
    – user115418
    Apr 14, 2020 at 15:57
  • @Andyroo Added the output of psutil.sensors_temperatures().
    – Prudhvi
    Apr 15, 2020 at 6:13

3 Answers 3

4

I didn't understand why the temperature measured by the psutil is not matching with the temperature measured by the vcgencmd. To get the CPU Temperature which is reported by vcgencmd using python I can use the following code.

from gpiozero import CPUTemperature

cpu = CPUTemperature()
print(cpu.temperature)

or

def get_cpu_temp():
    tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
    cpu_temp = tempFile.read()
    tempFile.close()
    return round(float(cpu_temp)/1000, 2)
2
  • An explanation why this is the right method would be useful here. Apr 15, 2020 at 16:43
  • 1
    @DmitryGrigoryev Added the explanation.
    – Prudhvi
    Apr 16, 2020 at 6:02
0

I personally have not used 'psutil' but since vcgencmd comes with Raspbian and is used by Rasbperry Pi engineers (vcgencmd docs) I would depend on that command.

To add to Andyroo's comment if you use the 'htop' command in terminal you can see what each core is doing and if you have an issue.

I have a script that I run when I know my Pi will be under heavy load:

#!/bin/bash
while true 
do
if [[ $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -gt 40 && $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -le 65 ]];
then
                tempC=$(vcgencmd measure_temp | egrep -o '[0-9][0-9]' | bc)
                tempF=$(echo "scale=2; 1.8 *$tempC + 32" | bc)
                timestamp=$(date)
                printf "$timestamp --- Temp Is: $tempC'C And $tempF'F\n"
                sleep 60
elif [[ $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -gt 66 && $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -le 70 ]];
then
                tempC=$(vcgencmd measure_temp | egrep -o '[0-9][0-9]' | bc)
                tempF=$(echo "scale=2; 1.8 *$tempC + 32" | bc)
                timestamp=$(date)
                printf "$timestamp --- Temp Is: $tempC'C And $tempF'F\n" | tee -a "/home/pi/Desktop/TempLog.log"
                top -b -n 1 >>  "/home/pi/Desktop/TempLog.log"
                sleep 30
elif [[ $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -gt 71  && $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -le 75 ]];
then
                tempC=$(vcgencmd measure_temp | egrep -o '[0-9][0-9]' | bc)
                tempF=$(echo "scale=2; 1.8 *$tempC + 32" | bc)
                timestamp=$(date)
                printf "$timestamp --- Temp Is: $tempC'C And $tempF'F\n" | tee -a "/home/pi/Desktop/TempLog.log"
                top -b -n 1 >>  "/home/pi/Desktop/TempLog.log"
                zenity --warning --title "OVERHEAT SOON" --text "Overheat Warning Temp Is: $tempC'C And $tempF'F" --timeout 5  --width=200 --height=200
                sleep 10
elif [[ $(vcgencmd measure_temp | egrep -o '[0-9][0-9]') -gt 76 ]];
then
                tempC=$(vcgencmd measure_temp | egrep -o '[0-9][0-9]' | bc)
                tempF=$(echo "scale=2; 1.8 *$tempC + 32" | bc)
                timestamp=$(date)
                printf "$timestamp --- Temp Is: $tempC'C And $tempF'F\n" | tee -a "/home/pi/Desktop/TempLog.log"
                top -b -n 1 >>  "/home/pi/Desktop/TempLog.log"
                zenity --warning --title "OVERHEATING" --text "Overheating!! Throttle Back!! Temp Is: $tempC'C And $tempF'F" --timeout 5 --width=200 --height=200
                sleep 1
else
        echo "error"
fi
done

NOTE: I am aware that the Pi4 auto throttles at 85'C. However, with my setup under stress tests my unit does not go above 76'C so this is to tell me that there is an issue outside of the norm

0

I don't have an answer, but I may be familiar with the issue that you are seeing with regard to the psutil command not showing the CPU temp correctly. I recently found a possible bug with the psutil command, which in my situation was reporting the temperature of the Dallas DS18B20 temp sensor that I had connected to my raspberry pi's GPIO (as an enclosure sensor for my Octoprint setup). This was causing an issue where a particular Octoprint plugin was reporting the CPU temp as "0" due the the way that it was coded.

This is a link to the issue I filed on github with the plugin developer: https://github.com/StefanCohen/OctoPrint-Dashboard/issues/148

He isn't planning to fix the issue at this time, since he believed that it is a bug in the way that the PSUTIL command is implemented. I may submit an issue on this at some point, but right now I am trying to hack together a fix for the plugin myself (essentially using the vcgencmd method of checking the CPU temp).

As a reference, this is what I get when I run the pusutil.sensors_temperature(fahrenheit=False) command:

>>> import psutil
>>> print(psutil.sensors_temperatures(fahrenheit=False))
{'w1_slave_temp': [shwtemp(label='', current=27.187, high=None, critical=None)]}
>>> 

While the following is what I get when I run the same command on a test pi that DOES NOT have the Dallas temperature sensor connected (this is the response that should have come):

>>> import psutil
>>> print(psutil.sensors_temperatures(fahrenheit=False))
{'cpu-thermal': [shwtemp(label='', current=51.54, high=None, critical=None)]}
>>> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.