完整代码:
importtime
importAdafruit_SSD1306
fromPILimportImage
fromPILimportImageDraw
fromPILimportImageFont
fromjetbot.utils.utilsimportget_ip_address
importsubprocess
#128x32displaywithhardwareI2C:
disp=Adafruit_SSD1306.SSD1306_128_32(rst=None,i2c_bus=1,gpio=1)#settinggpioto1ishacktoavoidplatformdetection
#Initializelibrary.
disp.begin()
#Cleardisplay.
disp.clear()
disp.display()
#Createblankimagefordrawing.
#Makesuretocreateimagewithmode‘1’for1-bitcolor.
width=disp.width
height=disp.height
image=Image.new(‘1’,(width,height))
#Getdrawingobjecttodrawonimage.
draw=ImageDraw.Draw(image)
#Drawablackfilledboxtocleartheimage.
draw.rectangle((0,0,width,height),outline=0,fill=0)
#Drawsomeshapes.
#Firstdefinesomeconstantstoalloweasyresizingofshapes.
padding=-2
top=padding
bottom=height-padding
#Movelefttorightkeepingtrackofthecurrentxpositionfordrawingshapes.
x=0
#Loaddefaultfont.
font=ImageFont.load_default()
whileTrue:
#Drawablackfilledboxtocleartheimage.
draw.rectangle((0,0,width,height),outline=0,fill=0)
#Shellscriptsforsystemmonitoringfromhere:https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd=“top-bn1|grepload|awk‘{printf\”CPULoad:%.2f\“,$(NF-2)}’”
CPU=subprocess.check_output(cmd,shell=True)
cmd=“free-m|awk‘NR==2{printf\”Mem:%s/%sMB%.2f%%\“,$3,$2,$3*100/$2}’”
MemUsage=subprocess.check_output(cmd,shell=True)
cmd=“df-h|awk‘$NF==\”/\“{printf\”Disk:%d/%dGB%s\“,$3,$2,$5}’”
Disk=subprocess.check_output(cmd,shell=True)
#Writetwolinesoftext.
draw.text((x,top),“eth0:”+str(get_ip_address(‘eth0’)),font=font,fill=255)
draw.text((x,top+8),“wlan0:”+str(get_ip_address(‘wlan0’)),font=font,fill=255)
draw.text((x,top+16),str(MemUsage.decode(‘utf-8’)),font=font,fill=255)
draw.text((x,top+25),str(Disk.decode(‘utf-8’)),font=font,fill=255)
#Displayimage.
disp.image(image)
disp.display()
time.sleep(1