The main purpose of this article is to demonstrate how to read and display CPU temperature with the help of the pythonnet library in Python.
Python gets CPU temperature
Depending on the type of application you are designing, you may wish to monitor the resources of the machine the program is running on.
This can happen for a number of reasons. Maybe you need your program to behave in a certain way when system resources reach a certain threshold.
Use cases may vary from program to program.
Among these system resources, CPU temperature has significance in specific applications and use cases.
Perhaps your program is overstretching the CPU and using many resources unnecessarily. You may want to take steps to mitigate this problem, so you may need to monitor the temperature of different machine components, CPU being one of them.
To solve this specific problem, we can use the DLL (Dynamic Link Library) provided by OpenHardwareMonitor.
This section is divided into the following sections:
- Install
- Code
- monitor
Install
First, we need to download pythonnet to be able to interact with the DLL. To do this, execute the following command in Terminal.
pip install pythonnet
This will give the following output:
Collecting pythonnet
Downloading pythonnet-3.0.0.post1-py3-none-any.whl (279 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 279.4/279.4 kB 639.3 kB/s eta 0:00:00
Requirement already satisfied: clr-loader<0.3.0,>=0.2.2 in c:\program files\python310\lib\site-packages (from pythonnet) (0.2.4)
Requirement already satisfied: cffi>=1.13 in c:\program files\python310\lib\site-packages (from clr-loader<0.3.0,>=0.2.2->pythonnet) (1.15.1)
Requirement already satisfied: pycparser in c:\program files\python310\lib\site-packages (from cffi>=1.13->clr-loader<0.3.0,>=0.2.2->pythonnet) (2.21)
Installing collected packages: pythonnet
Successfully installed pythonnet-3.0.0.post1
After successfully installing pythonnet, it's time to download OpenHardwareMonitor. Download the software from here.
After the download is complete, install the application. after:
- Change to the directory where OpenHardwareMonitor is installed.
- Locate OpenHardwareMonitorLib.dll from the files in the folder.
- Copy to your desired folder, preferably where your python scripts are stored.
Code
Once the installation process is complete, it's time to implement the actual code.
Consider the following code:
import clr #package pythonnet, not clr
openhardwaremonitor_sensortypes = ['Voltage','Clock','Temperature','Load','Fan','Flow','Control','Level','Factor','Power','Data','SmallData']
def initialize_openhardwaremonitor():
file = 'D:\\Path_TO_DLL\\OpenHardwareMonitorLib.dll'
clr.AddReference(file)
from OpenHardwareMonitor import Hardware
handle = Hardware.Computer()
handle.MainboardEnabled = True
handle.CPUEnabled = True
handle.RAMEnabled = True
handle.GPUEnabled = True
handle.HDDEnabled = True
handle.Open()
return handle
def fetch_stats(handle):
for i in handle.Hardware:
i.Update()
for sensor in i.Sensors:
parse_sensor(sensor)
for j in i.SubHardware:
j.Update()
for subsensor in j.Sensors:
parse_sensor(subsensor)
def parse_sensor(sensor):
hardwaretypes = openhardwaremonitor_hwtypes
if sensor.Value is not None:
if str(sensor.SensorType) == 'Temperature':
print(u"%s %s Temperature Sensor #%i %s - %s\u00B0C" % (hardwaretypes[sensor.Hardware.HardwareType], sensor.Hardware.Name, sensor.Index, sensor.Name, sensor.Value))
if __name__ == "__main__":
print("OpenHardwareMonitor:")
HardwareHandle = initialize_openhardwaremonitor()
fetch_stats(HardwareHandle)
Using the clr module, we can interact with a .NET DLL called OpenHardwareMonitorLib.dll. We can continue to use its functions and properties in our Python code, eventually reading the temperature of the CPU and other components we may wish to view.
For a detailed introduction of what each attribute represents and its function, please refer to the OpenHardwareMonitor documentation on Github.
As with any other custom functionality, it is recommended to review the code to gain a better understanding and insight into the inner workings of the code.
monitor
Once the code is written, it's time to execute the program. Remember that you need to run this script as an administrator; otherwise, the code will not function properly and the necessary readings may not be displayed correctly or at all.
Open a command prompt or any terminal of your choice and execute the script. For the code above, running as administrator produces the following output:
OpenHardwareMonitor: CPU Intel Core i7-4800MQ Temperature Sensor #0 CPU Core #1 - 60.0°C CPU Intel Core i7-4800MQ Temperature Sensor #1 CPU Core #2 - 66.0°C CPU Intel Core i7-4800MQ Temperature Sensor #2 CPU Core #3 - 58.0°C CPU Intel Core i7-4800MQ Temperature Sensor #3 CPU Core #4 - 58.0°C CPU Intel Core i7-4800MQ Temperature Sensor #4 CPU Package - 66.0°C GpuNvidia NVIDIA Quadro K1100M Temperature Sensor #0 GPU Core - 43.0°C HDD ST500LT012-9WS142 Temperature Sensor #0 Temperature - 37.0°C