Reading CPU Temperature with Python on Windows 10?

Discussion in 'UDOO X86' started by am1982, May 27, 2018.

  1. am1982

    am1982 UDOOer

    Joined:
    Mar 13, 2018
    Messages:
    14
    Likes Received:
    4
    Hello, finally I found a solution, but I am not sure, if the reading of the temperature is right.
    Here is my Code: https://pastebin.com/NJCamhrf

    Maybe you have some improvement suggestions.

    Code:
    """
    You have to run this script as administrator.
    PowerShell is used.
    I am not sure, if the temperature reading is right.
    """
    
    import subprocess
    import os
    import datetime
    
    psscript = """
    $t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace “root/wmi”
    
    while (1) {$t.CurrentTemperature; sleep 5}
    """
    
    si = subprocess.STARTUPINFO()
    si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    
    cmd = ['powershell.exe', '-Command',  psscript]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, startupinfo=si)
    
    with open(r'C:\Users\skyradar\Desktop\temp.txt', 'w') as fd:
        while True:
            tmp = proc.stdout.readline()
            proc.stdout.readline() # hack to prevent output twice
            celsius = int(tmp) / 10 - 273.15
            celsius_str = f'{celsius:.2f};{datetime.datetime.now().isoformat()}'
            print(celsius_str)
            fd.write(celsius_str)
            fd.write(os.linesep)
     
  2. am1982

    am1982 UDOOer

    Joined:
    Mar 13, 2018
    Messages:
    14
    Likes Received:
    4
  3. LDighera

    LDighera UDOOer

    Joined:
    Jan 13, 2014
    Messages:
    206
    Likes Received:
    36
    I appreciate you sharing your code, but I find that RealTemp does all I need.
     
  4. am1982

    am1982 UDOOer

    Joined:
    Mar 13, 2018
    Messages:
    14
    Likes Received:
    4
    No problem. It seems that the value I read with my code is never changing.

    My approach was to save the temperature in a csv file (headless, without a GUI) and make a Graph from it.
    Maybe later together with Fan-Speed and CPU-Load.

    But it seems that I read the wrong Data. Additionally I do have the problem, that there is one "unknown device" in the device manager.
     

Share This Page