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
#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


Messages In This Thread
RE: monitoring the temperature of the CPU with Python - by snippsat - Apr-13-2021, 04:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,659 May-07-2023, 12:33 PM
Last Post: deanhystad
  Help gathering Temperature from API response road102 5 1,082 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,866 Apr-16-2021, 07:49 PM
Last Post: apollo
  IoT Air Monitoring System project_science 0 1,939 Mar-26-2021, 08:14 PM
Last Post: project_science
  print CPU temperature samuelbachorik 12 6,123 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,103 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  OOP and module approaches in a simple app monitoring list of servers hjzxxzjcz 1 49,002 Nov-01-2019, 04:30 PM
Last Post: nilamo
  Show real time temperature and storage with Python linkxxx 4 2,775 Aug-28-2019, 02:06 PM
Last Post: linkxxx
  Read temperature once mada72 2 2,797 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,612 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