樹莓派cpu溫度及相關硬件簡單信息拾取

樹莓派相關硬件的查詢有專有命令,但cpu溫度用的比較少,這裏分享兩個方法查詢派cpu溫度和硬件簡單信息的方法。
1、centos安裝lm_sensors。
yum install lm_sensors -y 安裝完成就可以用sensors來查詢溫度。在raspbian系統裏不是這個,我還不知道是什麼。
2、用內置命令vcgencmd來查詢。這個centos和raspbian上都可以用,而且參數衆多。我在網上找了一個python的程序可以實現部分功能,其實可以改改顯示更多內容,但我懶得改,有興趣的可以嘗試一下。原代碼如下 :
import os
 
# Return CPU temperature as a character string                                     
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("'C\n",""))
 
                                                            
def getRAMinfo():
    p = os.popen('free')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:
            return(line.split()[1:4])
            
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
 def getDiskSpace():
    p = os.popen("df -h /")
    i = 0
    while 1:
        i = i +1
        line = p.readline()
        if i==2:
            return(line.split()[1:5])
 
 
# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()
 
# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)
 
# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]
if __name__ == '__main__':
    print('')
    print('CPU Temperature = '+CPU_temp+"℃")
    print('CPU Use = '+CPU_usage)
    print('')
    print('RAM Total = '+str(RAM_total)+' MB')
    print('RAM Used = '+str(RAM_used)+' MB')
    print('RAM Free = '+str(RAM_free)+' MB')
    print('') 
    print('DISK Total Space = '+str(DISK_total)+'B')
    print('DISK Used Space = '+str(DISK_used)+'B')
    print('DISK Used Percentage = '+str(DISK_perc))
                                   
先說明上面的代碼不是我的,因爲時間太久我也不知道在哪找到的,在此感謝原作者。python2和2都可以用。
上面代碼運行結果:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章