라즈베리파이 5

라즈베리파이 CPU 온도 및 IP 주소 LCD Display

지구빵집 2024. 6. 26. 07:38
반응형

 

 

저는 라즈베리 파이를 사용하는 동안 LCD1602 디스플레이 화면을 사용하여 IP 주소와 CPU 온도를 표시하여 라즈베리 파이가 튀지 않도록 하고 싶었습니다. LCD 화면은 16열에 두 줄의 문자만 표시할 수 있기 때문에 제한이 있어서 저는 "CPU" 텍스트만 표시하기로 했습니다: " 및 "IP:"와 해당 데이터만 표시하기로 했습니다. 디스플레이는 I2C 인터페이스를 사용하여 직렬 입력 및 병렬 출력 모듈을 화면에 연결하므로 LCD를 작동하는 데 필요한 전선은 4개뿐입니다. 직렬-병렬 IC 칩은 코드에서 참조할 수 있는 기본 I2C 주소(0x27)를 가진 PCF8574T입니다. 브레드보드, RGB LED, 전선, 저항기, LCD, 라즈베리파이가 필요합니다. 

 

 

기사 이미지 출처: https://www.instructables.com/Display-CPU-Temperature-and-IP-Address-for-Raspber/

 

 

LCD의 전선을 Pi의 올바른 GPIO 핀에 연결합니다: GND - Pi GND 5V - Pi 5V SDA - Pi SDA1 SCL - Pi SCL1 2단계: 파이썬 코드를 작성하여 LCD 디스플레이에서 메시지 실행하기 

 

파이선 코드

 

from PCF8574 import PCF8574_GPIO

from Adafruit_LCD1602 import Adafruit_CharLCD

def get_cpu_temp():

tmp = open('/sys/class/thermal/thermal_zone0/temp')

cpu = tmp.read()

tmp.close()

return '{:.2f}'.format( float(cpu)/1000 ) + ' C'

def loop():

mcp.output(3,1)

lcd.begin(16,2)

while(True):

lcd.setCursor(0,0)
lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )

PCF8574_address = 0x27

mcp = PCF8574_GPIO(PCF8574_address)

lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)

if __name__ == '__main__':

try:

loop()

except KeyboardInterrupt: destroy()

 

 

LCD 화면이 작동하는지 테스트합니다. 메시지가 표시되지 않으면 LCD 보드 뒷면의 다이얼을 텍스트가 보일 때까지 돌립니다. 

 

RGB를 필요한 핀에 연결하고 저항을 사용하여 흐름을 제어합니다. GPIO17 GPIO27 3V3 - 더 긴 핀 GPIO18 

 

 

from PCF8574 import PCF8574_GPIO from Adafruit_LCD1602 import Adafruit_CharLCD

from time import sleep, strftime from datetime import datetime

import subprocess

import RPi.GPIO as GPIO import time import random

pins = [15, 11, 13]

def setup2():
global pwmRed,
pwmGreen,
pwmBlue
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.HIGH)
pwmRed = GPIO.PWM(pins[0], 2000)
pwmGreen = GPIO.PWM(pins[1], 2000)
pwmBlue = GPIO.PWM(pins[2], 2000)
pwmRed.start(0)
pwmGreen.start(0)
pwmBlue.start(0)

def setColor(r_val,g_val,b_val):
pwmRed.ChangeDutyCycle(r_val)
pwmGreen.ChangeDutyCycle(g_val)
pwmBlue.ChangeDutyCycle(b_val)

def get_cpu_temp():
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu = tmp.read()
tmp.close()
return '{:.2f}'.format( float(cpu)/1000 ) + ' C'

def get_cpu_temp2():
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu2 = tmp.read()
tmp.close()
return cpu2

def get_time_now():
return datetime.now().strftime(' %H:%M:%S')

def get_IP():
IP = subprocess.check_output(["hostname", "-I"]).split()[0]
IP2 = 'IP' + str(IP)
IP3 = (IP2[4:-1])
return IP3

def loop():
mcp.output(3,1)
lcd.begin(16,2)
while(True):
lcd.setCursor(0,0)
lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )
lcd.message('IP: ' + get_IP())
if int(get_cpu_temp2()) > 55500:
r=(62) g=(75) b=(100)
setColor(r,g,b)
print(get_cpu_temp2())
elif int(get_cpu_temp2()) < 55400 and int(get_cpu_temp2()) < 54000: r=(10)
setColor(r,g,b) print(get_cpu_temp2()) else: r=(10)
setColor(r,g,b) print(get_cpu_temp2()) sleep(1)

def destroy():
lcd.clear()
PCF8574_address = 0x27
PCF8574A_address = 0x3F
try: mcp = PCF8574_GPIO(PCF8574_address)
except:
try: mcp = PCF8574_GPIO(PCF8574A_address)
except:
print ('I2C Address Error !')
exit(1)
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)

if __name__ == '__main__':
setup2()
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
destroy()
반응형