Raspberry Pi取得CPU温度
获取树莓派的CPU和GPU温度
先找准设备名:
import commands def get_cpu_temp(): tempFile = open( "/sys/class/thermal/thermal_zone0/temp" ) cpu_temp = tempFile.read() tempFile.close() return float(cpu_temp)/1000 # Uncomment the next line if you want the temp in Fahrenheit #return float(1.8*cpu_temp)+32 def get_gpu_temp(): gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' ) return float(gpu_temp) # Uncomment the next line if you want the temp in Fahrenheit # return float(1.8* gpu_temp)+32 def main(): print "CPU temp: ", str(get_cpu_temp()) print "GPU temp: ", str(get_gpu_temp()) if __name__ == '__main__': main()
这里面有两个方法:get_cpu_temp
和get_gpu_temp
。它们俩都返回一个float型的
摄氏温度值。(如果你想使用华氏温度,打开里面的两行注释)。现在,让我们把所有信息
输出到树莓派LCD屏上吧。
原作者已经把完整的Python程序上传到了dropbox上: https://www.dropbox.com/s/e04v8vnus1hwrm0/rpy_cpu_gpu_temp.py
全部代码:
# Rollcode.com import commands import os import RPi.GPIO as GPIO import time LCD_RS = 25 LCD_E = 24 LCD_D4 = 23 LCD_D5 = 17 LCD_D6 = 18 LCD_D7 = 22 LCD_WIDTH = 16 LCD_CHR = True LCD_CMD = False LCD_LINE_1 = 0x80 LCD_LINE_2 = 0xC0 E_PULSE = 0.00005 E_DELAY = 0.00005 def get_cpu_temp(): tempFile = open( "/sys/class/thermal/thermal_zone0/temp" ) cpu_temp = tempFile.read() tempFile.close() return float(cpu_temp)/1000 # Uncomment the next line if you want the temp in Fahrenheit #return (1.8*cpu_temp)+32 def get_gpu_temp(): gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' ) return float(gpu_temp) # Uncomment the next line if you want the temp in Fahrenheit # return (1.8* gpu_temp)+32 def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(LCD_E, GPIO.OUT) GPIO.setup(LCD_RS, GPIO.OUT) GPIO.setup(LCD_D4, GPIO.OUT) GPIO.setup(LCD_D5, GPIO.OUT) GPIO.setup(LCD_D6, GPIO.OUT) GPIO.setup(LCD_D7, GPIO.OUT) lcd_init() # Here we show the message on the LCD screen str_cpu = str(round(get_cpu_temp(), 2)) str_gpu = str(get_gpu_temp()) lcd_byte(LCD_LINE_1, LCD_CMD) lcd_string("CPU: " + str_cpu + " C",1) lcd_byte(LCD_LINE_2, LCD_CMD) lcd_string("GPU: " + str_gpu + " C",1) def lcd_init(): lcd_byte(0x33,LCD_CMD) lcd_byte(0x32,LCD_CMD) lcd_byte(0x28,LCD_CMD) lcd_byte(0x0C,LCD_CMD) lcd_byte(0x06,LCD_CMD) lcd_byte(0x01,LCD_CMD) def lcd_string(message,style): # Send string to display # style=1 Left justified # style=2 Centered # style=3 Right justified if style==1: message = message.ljust(LCD_WIDTH," ") elif style==2: message = message.center(LCD_WIDTH," ") elif style==3: message = message.rjust(LCD_WIDTH," ") for i in range(LCD_WIDTH): lcd_byte(ord(message[i]),LCD_CHR) def lcd_byte(bits, mode): GPIO.output(LCD_RS, mode) GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x10==0x10: GPIO.output(LCD_D4, True) if bits&0x20==0x20: GPIO.output(LCD_D5, True) if bits&0x40==0x40: GPIO.output(LCD_D6, True) if bits&0x80==0x80: GPIO.output(LCD_D7, True) time.sleep(E_DELAY) GPIO.output(LCD_E, True) time.sleep(E_PULSE) GPIO.output(LCD_E, False) time.sleep(E_DELAY) GPIO.output(LCD_D4, False) GPIO.output(LCD_D5, False) GPIO.output(LCD_D6, False) GPIO.output(LCD_D7, False) if bits&0x01==0x01: GPIO.output(LCD_D4, True) if bits&0x02==0x02: GPIO.output(LCD_D5, True) if bits&0x04==0x04: GPIO.output(LCD_D6, True) if bits&0x08==0x08: GPIO.output(LCD_D7, True) time.sleep(E_DELAY) GPIO.output(LCD_E, True) time.sleep(E_PULSE) GPIO.output(LCD_E, False) time.sleep(E_DELAY) if __name__ == "__main__": main()