stats.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (c) 2017 Adafruit Industries
  2. # Author: Tony DiCola & James DeVito
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. # This example is for use on (Linux) computers that are using CPython with
  22. # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
  23. # not support PIL/pillow (python imaging library)!
  24. import time
  25. import subprocess
  26. from board import SCL, SDA
  27. import busio
  28. from PIL import Image, ImageDraw, ImageFont
  29. import adafruit_ssd1306
  30. # Create the I2C interface.
  31. i2c = busio.I2C(SCL, SDA)
  32. # Create the SSD1306 OLED class.
  33. # The first two parameters are the pixel width and pixel height. Change these
  34. # to the right size for your display!
  35. disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
  36. # Clear display.
  37. disp.fill(0)
  38. disp.show()
  39. # Create blank image for drawing.
  40. # Make sure to create image with mode '1' for 1-bit color.
  41. width = disp.width
  42. height = disp.height
  43. image = Image.new("1", (width, height))
  44. # Get drawing object to draw on image.
  45. draw = ImageDraw.Draw(image)
  46. # Draw a black filled box to clear the image.
  47. draw.rectangle((0, 0, width, height), outline=0, fill=0)
  48. # Draw some shapes.
  49. # First define some constants to allow easy resizing of shapes.
  50. padding = -2
  51. top = padding
  52. bottom = height - padding
  53. # Move left to right keeping track of the current x position for drawing shapes.
  54. x = 0
  55. # Load default font.
  56. font = ImageFont.load_default()
  57. # Alternatively load a TTF font. Make sure the .ttf font file is in the
  58. # same directory as the python script!
  59. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  60. # font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)
  61. while True:
  62. # Draw a black filled box to clear the image.
  63. draw.rectangle((0, 0, width, height), outline=0, fill=0)
  64. # Shell scripts for system monitoring from here:
  65. # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
  66. cmd = "hostname -I | cut -f 1 -d ' '"
  67. IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
  68. cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
  69. CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
  70. cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'"
  71. MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
  72. cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB %s", $3,$2,$5}\''
  73. Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
  74. #cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
  75. cmd ="vcgencmd measure_temp |cut -f 2 -d '='"
  76. Temp = subprocess.check_output(cmd, shell = True )
  77. # Write four lines of text.
  78. draw.text((x, top + 0), "IP: " + IP, font=font, fill=255)
  79. draw.text((x, top + 8), CPU, font=font, fill=255)
  80. draw.text((x, top + 16), MemUsage, font=font, fill=255)
  81. draw.text((x, top + 25), Disk, font=font, fill=255)
  82. draw.text((x, top + 34),"Temp: ", font=font, fill=255)
  83. draw.text((30, top + 34), Temp, font=font, fill=255)
  84. # Display image.
  85. disp.image(image)
  86. disp.show()
  87. time.sleep(0.1)