nguyenhung1903

Untitled

Oct 3rd, 2021
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2.  
  3. import Adafruit_SSD1306
  4.  
  5. from PIL import Image
  6. from PIL import ImageDraw
  7. from PIL import ImageFont
  8.  
  9. import subprocess
  10. import datetime
  11. from psutil import cpu_percent
  12.  
  13. # 128x32 display with hardware I2C:
  14. disp = Adafruit_SSD1306.SSD1306_128_64(rst=None, i2c_bus=1, gpio=1) # setting gpio to 1 is hack to avoid platform detection
  15.  
  16. # Initialize library.
  17. disp.begin()
  18.  
  19. # Clear display.
  20. disp.clear()
  21. disp.display()
  22.  
  23. # Create blank image for drawing.
  24. # Make sure to create image with mode '1' for 1-bit color.
  25. width = disp.width
  26. height = disp.height
  27. image = Image.new('1', (width, height))
  28.  
  29. # Get drawing object to draw on image.
  30. draw = ImageDraw.Draw(image)
  31.  
  32. # Draw a black filled box to clear the image.
  33. draw.rectangle((0,0,width,height), outline=0, fill=0)
  34.  
  35. # Draw some shapes.
  36. # First define some constants to allow easy resizing of shapes.
  37. padding = -2
  38. top = padding
  39. bottom = height-padding
  40. # Move left to right keeping track of the current x position for drawing shapes.
  41. x = 0
  42.  
  43. # Load default font.
  44. font = ImageFont.load_default()
  45.  
  46. while True:
  47.     # Draw a black filled box to clear the image.
  48.     draw.rectangle((0,0,width,height), outline=0, fill=0)
  49.  
  50.     cmd = "hostname -I | cut -d\' \' -f1"
  51.     IP = subprocess.check_output(cmd, shell = True )
  52.     CPU = cpu_percent()
  53.     cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.1f%%\", $3,$2,$3*100/$2 }'"
  54.     MemUsage = subprocess.check_output(cmd, shell = True )
  55.     cmd = "free -m | awk 'NR==3{printf \"Swap: %s/%sMB %.1f%%\", $3,$2,$3*100/$2 }'"
  56.     SwapUsage = subprocess.check_output(cmd, shell = True )
  57.     cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
  58.     Disk = subprocess.check_output(cmd, shell = True )
  59.     Date = datetime.datetime.now()
  60.     # Write two lines of text.
  61.     draw.text((x, top),       "IP: " + str(IP.decode("utf-8")),  font=font, fill=255)
  62.     draw.text((x, top+8),     "CPU: " +  str(CPU) + "%", font=font, fill=255)
  63.   #draw.text((x + 65, top+8),     "GPU: " + "100.0%", font=font, fill=255)
  64.     draw.text((x, top+16),    str(MemUsage.decode("utf-8")),  font=font, fill=255)
  65.     draw.text((x, top+24),    str(SwapUsage.decode("utf-8")),  font=font, fill=255)
  66.     draw.text((x, top+32),    str(Disk.decode("utf-8")),  font=font, fill=255)
  67.     draw.text((x, top+40),    str(Date.strftime("%b %d %Y %H:%M:%S")),  font=font, fill=255)
  68.     # Display image.
  69.     disp.image(image)
  70.     disp.display()
  71.     time.sleep(.5)
  72.  
RAW Paste Data