树莓派检测CPU温度

代码是Python2代码,适当修改为长输出。可自行改为python3.

代码附赠了CPU负载和内存使用情况

代码引用地址:
https://gist.github.com/582033/b94dc1d4941f63bb28d837fa9c72f08d

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
显示树莓派cpu温度、使用率及内存使用率
'''

import os
import time

def show_temp():
    file = open("/sys/class/thermal/thermal_zone0/temp")
    temp = float(file.read()) / 1000
    file.close()
    print "Temp: %.1f ℃" %temp


def show_mem():
    mem_cmd = "free -m | grep Mem | awk '{print ($4+$6)/$2}'"
    mem_sur = round(float(os.popen(mem_cmd).read()), 2) * 100
    print "Mem avail: %d%%" % mem_sur


def show_cpu():
    cpu_cmd = "uptime | awk '{print $8,$9,$10,$11,$12}'"
    cpu_used = os.popen(cpu_cmd).read()
    print "Cpu Used: %s" % cpu_used.replace('\n', '')


if __name__ == '__main__':

    while 1:
        show_temp()
        show_cpu()
        show_mem()
        print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
        time.sleep(2)