Python Forum
monitoring the temperature of the CPU with Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
monitoring the temperature of the CPU with Python
#1
dear fellows,  Wink

 

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.

how to do this 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:
            pass
    return temp, msg

temp, msg = check_CPU_temp()

print "temperature (" + u'\xb0' + "C): ", temp
print "full message:    ", msg
 #which returns uns a floating point value  and furtermore additionally the original message in which it is contained.
 
this will return the following output

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-su...s-examples

 Your Apollo Smile


on a sidenote: with psutil i can do much much more. i know. But this is my first step in gathering some data out of the system. with psutil i am able to monitor much much better the status of my raspberry pi. We re able to monitor the parameters of the CPU, the memory size and also the disk.

in the next step i will use psutil
Reply
#2
You stop using Python 2(dead💀),that you use Raspberry Pi is no excuse as most of there stuff is updated.
So in Python 3.9,see that Unicode can now be used in string one biggest update moving from 2 to 3.
>>> temp = 24.5
>>> print(f"temperature {temp}°C)")
temperature 24.5°C)
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}")
apollo likes this post
Reply
#3
dear Snippsat Smile

many thanks for the headsup

glad that you have had a look at this.


i will check all my scripts and will do all of them in Python 3.9


Many thanks for your help.

Have a great day Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,568 May-07-2023, 12:33 PM
Last Post: deanhystad
  Help gathering Temperature from API response road102 5 1,019 Dec-16-2022, 08:30 PM
Last Post: road102
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,797 Apr-16-2021, 07:49 PM
Last Post: apollo
  IoT Air Monitoring System project_science 0 1,901 Mar-26-2021, 08:14 PM
Last Post: project_science
  print CPU temperature samuelbachorik 12 5,922 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,043 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  OOP and module approaches in a simple app monitoring list of servers hjzxxzjcz 1 41,344 Nov-01-2019, 04:30 PM
Last Post: nilamo
  Show real time temperature and storage with Python linkxxx 4 2,702 Aug-28-2019, 02:06 PM
Last Post: linkxxx
  Read temperature once mada72 2 2,739 Apr-28-2019, 07:18 PM
Last Post: mada72
  PYTHON - UNRAR : how can I build a thread to monitoring the downloading status Palerm0_24 2 2,564 Mar-18-2019, 02:25 PM
Last Post: Palerm0_24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020