diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8fe2f47 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..568f21d --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +import setuptools + +with open("README.md", "r", encoding="utf-8") as fh: + long_description = fh.read() + +setuptools.setup( + name="OledPiMonitor", + version="0.0.1", + author="jerome aka jblb", + author_email="jerome@jblb.net", + description="Display IP address on a SSD1306", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://mygit.jblb.net/jerome/OledPiMonitor", + project_urls={ + "jblb": "https:jblb.net", + "Haum": "https:haum.org", + }, + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: GPL", + "Operating System :: OS Independent", + ], + package_dir={"": "src"}, + packages=setuptools.find_packages(where="src"), + python_requires=">=3.6", + install_requires= [ + "RPi.GPIO==0.7.1a4", + "adafruit-circuitpython-ssd1306==2.12.3", + "Pillow==8.4.0", + ], + entry_points={ # Optional + 'console_scripts': [ + 'oled_status=OledPiMonitor.I2C_SSD1306:main', + ], + } + +) diff --git a/src/OledPiMonitor/I2C_SSD1306.py b/src/OledPiMonitor/I2C_SSD1306.py new file mode 100644 index 0000000..1ab93e5 --- /dev/null +++ b/src/OledPiMonitor/I2C_SSD1306.py @@ -0,0 +1,86 @@ +""" Display my IP address on an I2C OLED screen connected over GPIO """ + +import socket +import time +import subprocess + + +from PIL import Image +from PIL import ImageDraw +from PIL import ImageFont + +from board import SCL, SDA +import busio +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, 64, i2c) + +# Clear display. +disp.fill(0) +disp.show() + + +def get_ip(): + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(("9.9.9.9", 80)) + ip_addr = s.getsockname()[0] + s.close() + except: + cmd = "hostname -I | cut -d\' \' -f1" + ip_addr = str(subprocess.check_output(cmd, shell = True), 'utf-8') + + return ip_addr + + +def main(): + + # 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) + font = ImageFont.load_default() + + # Draw a black filled box to clear the image. + draw.rectangle((0, 0, width, height), outline=0, fill=0) + + def draw_text(text, line=0): + draw.text((5, 5 + line * 10), text, font=font, fill=1) + + # Check & redraw IP address and temp + try: + while 1: + cmd = "vcgencmd measure_temp |cut -f 2 -d '='" + temp = subprocess.check_output(cmd, shell = True) + ip_addr = get_ip() + if ip_addr: + draw_text("IP: " + ip_addr) + else: + draw_text("Searching for Wi-Fi...") + + draw_text("Temp " + str(temp, 'utf-8'), 1) + + disp.image(image) + disp.show() + time.sleep(.01) + # Draw a black filled box to clear the image. + draw.rectangle((0, 0, width, height), outline = 0, fill = 0) + + except KeyboardInterrupt: + disp.fill(0) + disp.show() + + +if __name__ == '__main__': + main() diff --git a/src/OledPiMonitor/__init__.py b/src/OledPiMonitor/__init__.py new file mode 100644 index 0000000..e69de29