Display CPU Temperature and IP Address for Raspberry Pi

by SJ333 in Circuits > Raspberry Pi

1336 Views, 2 Favorites, 0 Comments

Display CPU Temperature and IP Address for Raspberry Pi

IP address 5.jpg
IP address 8.jpg
IP address 6.jpg
IP address 7.jpg
8031C91A-E0B8-419F-B34F-E152A9F3FE4E.jpeg

I wanted to use the LCD1602 display screen to show my IP address and CPU temperature whilst I was using the Raspberry Pi so I did not end up frying it. The LCD screen displays only two lines of characters in 16 columns as such, due to the limits, I chose to only display the text "CPU: " and "IP: " with the corresponding data.

The display uses the I2C interface to connect the serial-input and parallel-output module to the screen which allows only 4 wires to be needed to operate the LCD.

The serial-to-parallel IC chip is PCF8574T with default I2C address (0x27) which you will see referenced in the code.

You will nee a breadboard, RGB led, wires, resistors, LCD, and the raspberry Pi.

Wire Up the LCD

IP address 4.jpg
Ip address 2.jpg
IP address.jpg

Attach the wires from the LCD to the the correct GPIO pins on the Pi:

  • GND to Pi GND
  • 5V to Pi 5V
  • SDA to Pi SDA1
  • SCL to Pi SCL1

Write the Python Code to Run Your Message on Your LCD Display

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()

Test the LCD screen works.

If there is no messaging then turn the dial on the back of the LCD board until you can see the text.

Hook Up the RGB Light to the Pi

IP address 3.jpg

Hook the RGB up to the required pins and use the resistors to control the flow.
GPIO17
GPIO27
3V3 - longer pin
GPIO18

Write the Python Code to Set the Color to Your RGB Led

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()

Enjoy

IP address 5.jpg

Run the code, your light will now change colours based on the CPU temperature. Enjoy!