OLED Display am Raspi - Python Script

Moderatoren: seppy, udo1toni

Antworten
neuling10
Beiträge: 56
Registriert: 26. Mär 2022 18:15

OLED Display am Raspi - Python Script

Beitrag von neuling10 »

Guten Abend an die Runde,

ich möchte meinem Raspi im Desk-Pi ein OLED Display zur Anzeige von IP, Temperatur, Speicherauslastung, RAM und CPU-Auslastung gönnen. Im Internet gibt es in diversen Tutorials zur Anzeige der Parameter ein für mich grundsätzlich perfekt geeignetes Python Script.

Ich würde jedoch gerne in der dritten Display-Zeile "Mem" die prozentuelle Memory-Anzeige ohne Nachkommastellen darstellen, da die letzten beiden Ziffern aufgrund der Displaygröße abgeschnitten werden. Aktuell sind es 2 Nachkommastellen.

Was Python betrifft, bin ich leider völlig blank. In der (vermutlich zutreffenden) Code-Zeile

Code: Alles auswählen

cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
habe ich bereits alles mögliche versucht umzustellen und habe versucht mich an der Code-Zeile der Disk-Speicherauslastung zu orientieren. Leider habe ich jedoch noch kein brauchbares Ergebnis außer ein flackerndes Display erzielt. Kann mir ein Python-Kenner unter Euch mitteilen, wie der Code zu ändern ist, um die Memory-Prozentangabe ohne Nachkommastellen darzustellen? Unterhalb das verwendete original-Script.

Danke vielmals für Eure Hilfe :)

Code: Alles auswählen

# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
# SPDX-FileCopyrightText: 2017 James DeVito for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example is for use on (Linux) computers that are using CPython with
# Adafruit Blinka to support CircuitPython libraries. CircuitPython does
# not support PIL/pillow (python imaging library)!

import time
import subprocess

from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306


# Create the I2C interface.
i2c = busio.I2C(SCL, SDA)

# Create the SSD1306 OLED class.
# The first two parameters are the pixel width and pixel height.  Change these
# to the right size for your display!
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)

# Clear display.
disp.fill(0)
disp.show()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new("1", (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height - padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0


# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the
# same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
# font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)

while True:

    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)

    # Shell scripts for system monitoring from here:
    # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "hostname -I | cut -d\' \' -f1"
    #cmd = "hostname -I |cut -f 2 -d ' '"
    IP = subprocess.check_output(cmd, shell = True )
    cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.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 )
    cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
    temp = subprocess.check_output(cmd, shell = True )

    #cmd = "hostname -I | cut -d' ' -f1"
    #IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
    #cmd = 'cut -f 1 -d " " /proc/loadavg'
    #CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
    #cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB  %.2f%%\", $3,$2,$3*100/$2 }'"
    #MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
    #cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB  %s", $3,$2,$5}\''
    #Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")

    # Write four lines of text.
    draw.text((x, top),  "IP: " + str(IP,'utf-8'), font=font, fill=255)
    draw.text((x, top+8),  str(CPU,'utf-8') + " " + str(temp,'utf-8') , font=font, fill=255)
    draw.text((x, top+16), str(MemUsage,'utf-8'), font=font, fill=255)
    draw.text((x, top+25), str(Disk,'utf-8'), font=font, fill=255)
    #draw.text((x, top),       "IP: " + str(IP),  font=font, fill=255)
    #draw.text((x, top+8),     str(CPU), font=font, fill=255)
    #draw.text((x, top+16),    str(MemUsage),  font=font, fill=255)
    #draw.text((x, top+25),    str(Disk),  font=font, fill=255)


    #draw.text((x, top + 0), "IP: " + IP, font=font, fill=255)
    #draw.text((x, top + 8), "CPU load: " + CPU, font=font, fill=255)
    #draw.text((x, top + 16), MemUsage, font=font, fill=255)
    #draw.text((x, top + 25), Disk, font=font, fill=255)

    # Display image.
    disp.image(image)
    disp.show()
    time.sleep(0.1)
IMG_8493.jpg
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.

Benutzeravatar
udo1toni
Beiträge: 13856
Registriert: 11. Apr 2018 18:05
Answers: 222
Wohnort: Darmstadt

Re: OLED Display am Raspi - Python Script

Beitrag von udo1toni »

Der Teil %.2f%% bedeutet "die nächste variable einsetzen und als Float WErt formatieren, mit 2 Nachkommastellen und einem %-Zeichen (das % ist ein Special Character und muss deshalb mit sich selbst escaped werden) Ohne Nachkommastellen also so: %.0f%%
openHAB4.1.2 stable in einem Debian-Container (bookworm) (Proxmox 8.1.5, LXC), mit openHABian eingerichtet

neuling10
Beiträge: 56
Registriert: 26. Mär 2022 18:15

Re: OLED Display am Raspi - Python Script

Beitrag von neuling10 »

Dankeschön, läuft nun wie gewünscht :)

neuling10
Beiträge: 56
Registriert: 26. Mär 2022 18:15

Re: OLED Display am Raspi - Python Script

Beitrag von neuling10 »

Nun doch noch eine Verständnisfrage zur CPU Last:
Das Python Script holt sich die CPU-Load Werte mittels

Code: Alles auswählen

top -bn1
. Hier erfolgt eine prozentuelle Ausgabe der CPU Last. Wenn ich das Script richtig gelesen und verstanden habe, rechnet es die prozentuelle CPU-Auslastung in absolute Werte um und stellt diese dar. Ist es demzufolge korrekt, dass ein nicht übertakteter Raspberry 4B (CPU: Quad Core mit 1,5GHz) somit absolut maximal 6GHz leisten könnte?

Hintergrund meiner Frage:
Mir ist aufgefallen, dass manchmal während hoher CPU Last (zwischen 4 und 5 lt. Display), z.B. während ein Backup erstellt wird, mein Kamerastream abbricht. Möglicherweise nur Zufall, da die CPU Last ja noch nicht im Grenzbereich nahe 6GHz liegt, allerdings zumindest auffällig...
An der Temperatur (stets <60 Grad aufgrund aktiver Lüftung) und RAM-Auslastung (<50%) sollte es meines Erachtens nicht liegen...

Danke Euch für Anregungen :-)

Grüße
neuling10

Benutzeravatar
udo1toni
Beiträge: 13856
Registriert: 11. Apr 2018 18:05
Answers: 222
Wohnort: Darmstadt

Re: OLED Display am Raspi - Python Script

Beitrag von udo1toni »

Ich denke, die Kalkulation ist da etwas vereinfacht aufgebaut.

Die Pi 4 hat halt vier Kerne, und ein Prozess kann gewöhnlich einen Kern bis 100% auslasten. Die anderen Kerne haben aber vielleicht gerade nichts zu tun, was dann eben eine Auslastung von 25% ergäbe. Dennoch ist der Prozess eben am Limit.
Es hat schon seinen Grund, warum htop die Prozessorauslastung getrennt pro Kern anzeigt :)
openHAB4.1.2 stable in einem Debian-Container (bookworm) (Proxmox 8.1.5, LXC), mit openHABian eingerichtet

verstanden
Beiträge: 7
Registriert: 20. Jan 2023 22:18

Re: OLED Display am Raspi - Python Script

Beitrag von verstanden »

Hi neuling10,

du hast von Problemen während des Kamerastreams während des Backups berichtet - da ist ja die CPU nur eine mögliche Ursache. Laufen Backup und Kamera lokal oder ist da auch noch das Netzwerk beteiligt? Oder - wenn das Backup lokal auf die SD-Card erfolgt - nutzt die Kamera evtl. die SD-Card für eventuelle Caching-Aufgaben?

@udo1toni: top kann auch die CPUs einzeln anzeigen - einfach "1" drücken, während top läuft.

Gruß
Reinhard

Benutzeravatar
udo1toni
Beiträge: 13856
Registriert: 11. Apr 2018 18:05
Answers: 222
Wohnort: Darmstadt

Re: OLED Display am Raspi - Python Script

Beitrag von udo1toni »

verstanden hat geschrieben: 20. Jan 2023 23:19 @udo1toni: top kann auch die CPUs einzeln anzeigen - einfach "1" drücken, während top läuft.
Ja, htop bringt halt noch ein paar weitere Annehmlichkeiten mit. Ich gehe auch davon aus, dass man die Kernlast auch per Parameter aktivieren kann, aber im Display steht ja nun nur ein Wert drin, was hier halt nur bedingt hilfreich ist, aus den genannten Gründen.
openHAB4.1.2 stable in einem Debian-Container (bookworm) (Proxmox 8.1.5, LXC), mit openHABian eingerichtet

Antworten