Jump to content

monitoring the CPU temp in Python


malaga

Recommended Posts

dear fellows, 

 

what is aimed: on Raspberry Pi i want monitor the CPU temp at a command line, there is the vcgencmd command.


Well  We can do this in bash:

To read CPU temp at a command line, we can use the vcgencmd command.

Example in bash:

 

echo "The CPU is at $(vcgencmd measure_temp) degrees."

The CPU is at temp=45.5'C degrees.


i think that we can do this with Python as well: 

 

import re, subprocess
 
def check_CPU_temp():
    temp = None
    err, msg = subprocess.getstatusoutput('vcgencmd measure_temp')
    if not err:
        m = re.search(r'-?\d\.?\d*', msg)   # a solution with a  regex
        try:
            temp = float(m.group())
        except ValueError: # catch only error needed
            pass
    return temp, msg
 
temp, msg = check_CPU_temp()
print(f"temperature {temp}°C")
print(f"full message {msg}")

 

and this will return temperatures  - like so
 

temperature (°C):  67.0
full message:     temp=67.6'C

 

I hopefully have ceated the the code right - with usage of supprocess 

 

cf: Python 3 Subprocess Examples
https://queirozf.com/entries/python-3-subprocess-examples

 

 

Link to comment
Share on other sites

There are some strong opinions on the Raspberry Pi here, and many are not positive, mostly due to the closed hardware platform.

 

If you need support with Raspberry Pi temperature sensing, you'd find better results asking on their forums.

 

More standard SBC boards would use "sensors" from lm-sensors package, or just read the thermal sensors directly

 

tparys@hobbes:/sys/class/hwmon$ ls -l /sys/class/hwmon/*/temp*_input
-r--r--r-- 1 root root 4096 Apr 14 00:17 /sys/class/hwmon/hwmon0/temp1_input
-r--r--r-- 1 root root 4096 Apr 14 00:17 /sys/class/hwmon/hwmon1/temp1_input

 

Link to comment
Share on other sites

Just now, arox said:

cat /sys/class/hwmon/hwmon0/temp1_input

 

give exactly the same result than 'vcgencmd measure_temp'

 

 

Thanks to tparys : I had forgotten the /sys path. I **never** check the cpu temp : with the aluminium cases availables for RPI4 which press  the cpu, the RPI4 provide an excellent passive cooling solution.

 

On the contrary, I wish raspbian would not corrupt the root file system (on USB3 bus) when there is a USB error (on USB2 bus) ?!?

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines