Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

monitoring the CPU temp in Python

Featured Replies

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

 

 

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

 

cat /sys/class/hwmon/hwmon0/temp1_input

 

give exactly the same result than 'vcgencmd measure_temp'

 

Thanks to tparys : I h

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) ?!?

Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.