Rich's Wordpress

又一个WordPress站点

System information display

Raspberry PI has many models, memory and their own SD card capacity is not the same, so we can create a Raspberry PI system display system, the Raspberry PI CPU temperature, CPU usage, memory, hard disk (SD card) capacity and IP address information through OLED display, especially IP address, In this way, we can know the LAN IP address of the Raspberry PI after networking, which is convenient for VNC desktop control.

Code

import os


#IP lib
import socket, fcntl, struct


#oled lib
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep


#oled initialization
device = ssd1306(port=1, address=0x3c)


#Collects CPU temperature information and returns it
def getCPUtemperature():
	res = os.popen('vcgencmd measure_temp').readline()
	return(res.replace("temp=","").replace("'C n",""))


#Returns the percentage of CPU usage
def getCPUuse():
	return str(os.popen("top b n1 | awk '/Cpu \\(s \\):/ {print$2}'").readline())
	
	
# RAM information is collected, and the return unit is kB
# Index 0: total memory
# Index 1: used memory
# Index 2: remaining memory

def getRAMinfo():
	p = os.popen('free')
	i = 0
	while 1:
		i = i + 1
		line = p.readline()
		if i==2:
			return(line.split()[1:4])

# Returns the memory of the SD card in GB
# Index 0: Total memory
# Index 1: Used memory
# Index 2: Remaining memory
# Index 3: Percentage used
def getDiskSpace():
	p = os.popen("df h /")
	i = 0
	while 1:
		i = i +1
		line = p.readline()
		if i==2:
			return(line.split()[1:5])
			

#Get the local IP address and returns it
def getIP(ifname):
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	try:
		return socket.inet_ntoa(fcntl.ioctl(s.fileno(), \
				0x8915, \
				struct.pack('256s', ifname[:15].encode('utf 8')) )[20:24])
	except OSError:
		return('0.0.0.0')
			

while True:
	#Collect CPU temperature information
	CPU_temp = getCPUtemperature()
	
	
	#Collect the rate of CPU Usage
	CPU_usage = getCPUuse()
	
	
	#Memory information, converted to MB
	RAM_stats = getRAMinfo()
	RAM_total = int(int(RAM_stats[0]) / 1000)
	RAM_used = int(int(RAM_stats[1]) / 1000)
	RAM_free = int(int(RAM_stats[2]) / 1000)
	RAM_perc = str(round((RAM_used/RAM_total*100),1))+'%'
	
	
	#sd card capacity information
	DISK_stats = getDiskSpace()
	DISK_total = DISK_stats[0]
	DISK_used = DISK_stats[1]
	DISK_perc = DISK_stats[3]
	
	
	#To obtain the local IP address information, run the ifconfig command on the terminal to view the NIC name
	IP=getIP("wlan0")
	
	
	#oled display
	with canvas(device) as draw:
		draw.text((0, 0), "CPU Temp:"+CPU_temp, fill="white")
		draw.text((0, 12), 'CPU Used: '+CPU_usage + ' %', fill="white")
		draw.text((0, 24), 'RAM:'+str(RAM_used)+'/'+str(RAM_total)+'M'+' '+RAM_perc, fill="white")
		draw.text((0, 38), 'DISK:'+str(DISK_used)+'/'+str(DISK_total)+' '+ DISK_perc, fill="white")
		draw.text((0, 52),'IP:'+ IP, fill="white")
	
	sleep(0.5) # Delay and update

This code can display the information about CPU, RAM, Disk and IP address

Final Result

System information display
Scroll to top