
anyhoo, i recently scarfed one of those neato 128x64 OLEDs for about a Hamilton off the 'Net and thought it would make for a nice little system monitor on a headless RPi3 (they somehow seem to keep multiplying 'round these parts - dunno why)
it's one of those two-color jobbies that has a yellow area up top and the rest in blue... (too bad i couldn't figure out a way to make a blue LED work with the display)... what are these things, anyway? rejects from an old cell factory? there must be a gazillion of 'em out there! but i digress...
so i sat down this afternoon and crafted up a little Python (i'm a Python n00b, btw) to have the OLED do something useful... here are my pitiful results - i hope this can help someone out there ... it was an interesting diversion until it was '5 o'clock somewhere'...
but first I have to put out a big shout of 'thank you' to rm-hull for SSD1306 code... and of course, the many others who take the time to post examples of Python code to solve simple problems for us n00bs...
here's the code:
Code: Select all
#!/usr/bin/env python
# myssd.py version 0.6 - a simple system monitor for the Raspberry Pi
# adapted from rmhull's wonderful ssd1306 Python lib by [email protected]
# crafted for the dual-color 128x64 OLED w/yellow top area
# 060316 - added date
# added Raspberry Pi's CPU temperature in fahrenheit
# added wlan0 IP address
# added memory used
# added sd card usage
# 060416 - added splash screen
# general code cleanup
# added KB output for memory
import os
import psutil
from lib_oled96 import ssd1306
from time import sleep
from datetime import datetime
from PIL import ImageFont, ImageDraw, Image
#font = ImageFont.load_default()
from smbus import SMBus # These are the only two variant lines !!
i2cbus = SMBus(1) #
oled = ssd1306(i2cbus)
draw = oled.canvas
# set font to 13 for yellow area
font = ImageFont.truetype('FreeSans.ttf', 13)
# show splash screen
draw.text((1, 1), 'RASPBIAN SYSMON', font=font, fill=1)
logo = Image.open('rpi.png')
draw.bitmap((42, 16), logo, fill=1)
oled.display()
sleep(3)
oled.cls()
while True:
# "draw" onto this canvas, then call display() to send the canvas contents to the hardware.
draw = oled.canvas
# set font to 13 for yellow area
font = ImageFont.truetype('FreeSans.ttf', 13)
# get, display date and time
draw.text((1, 1), str(datetime.now().strftime('%a %b %d %H:%M:%S')), font=font, fill=1)
# reset font for four smaller lines
font = ImageFont.truetype('FreeSans.ttf', 10)
# get, return CPU's current temperature
def cpu_temp():
tempF = (((int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000)*9/5)+32)
return "CPU TEMP: %sF" % str(tempF)
# display CPU temp
draw.text((1, 15), cpu_temp(), font=font, fill=1)
# get, return wlan0's current IP address
def wlan_ip():
f = os.popen('ifconfig wlan0 | grep "inet\ addr" | cut -c 21-33')
ip = str(f.read())
# strip out trailing chars for cleaner output
return "WIFI IP: %s" % ip.rstrip('\r\n')
# display the IP address
draw.text((1, 28), wlan_ip(), font=font, fill=1)
# get amount of memory in use
def mem_usage():
usage = psutil.virtual_memory()
return "MEM IN USE: %s KB" % (int(str(usage.used)) / 1024)
# display amount of free memory
draw.text((1, 41), mem_usage(), font=font, fill=1)
# get disk usage
def disk_usage(dir):
usage = psutil.disk_usage(dir)
return "SD CARD IN USE: %.0f%%" % (usage.percent)
# display disk usage
draw.text((1, 53), disk_usage('/'), font=font, fill=1)
oled.display()
sleep(10)
oled.cls() # Oled still on, but screen contents now blacked out
of course, TTIWWP (this thread is worthless without pics), so i've put up a pic of my little display... it will go nicely with my new camera-enabled Pi Zero...
regards,
willie
on the stinkin' hot Gulf of Mexico