Display CPU/GPU Temperature your Raspberry Pi LCD16x2



#1. RASPBERRY PI Find I2C address of your device
https://youtu.be/3WfOP_0kU1g

#2. HOWTO Raspberry Pi + LCD 16x2 i2c
https://youtu.be/3XLjVChVgec

Python Script


# Import necessary libraries for communication and display use
import lcddriver
import time
import commands
# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = lcddriver.lcd()

def get_cpu_tempC():
tempFile = open("/sys/class/thermal/thermal_zone0/temp")
cpu_temp = tempFile.read()
tempFile.close()
return float(cpu_temp)/1000

def get_gpu_tempC():
gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
return float(gpu_temp)

# Main body of code
try:
while True:
# Remember that your sentences can only be 16 characters long!
print("Writing to display")

cpu_tempC = get_cpu_tempC()
gpu_tempC = get_gpu_tempC()

cpuT = "CPU temp: %.2f" % cpu_tempC
gpuT = "GPU temp: %.2f" % gpu_tempC

display.lcd_display_string(cpuT + "C", 1)
display.lcd_display_string(gpuT + "C", 2)

time.sleep(10)
except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()



ความคิดเห็น